Skip to content
This repository has been archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
p2psim: Remove redundant code and clarify gen_chart
Browse files Browse the repository at this point in the history
  • Loading branch information
sonhv0212 committed Dec 25, 2024
1 parent 5487da5 commit c9753ba
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions p2p/simulations/examples/discovery/gen_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@param prefixes: the prefixes to check
@return True if the node_id has a valid prefix
'''
def node_valid_with_prefixes(node_id, prefixes):
def is_node_valid_with_prefixes(node_id, prefixes):
if prefixes is None:
return True
for prefix in prefixes:
Expand Down Expand Up @@ -60,7 +60,7 @@ def parse_stats(self):
df = df[df['type'] == self.metric]
for node in self.nodes:
# filter node by prefixes
if not node_valid_with_prefixes(node, self.prefixes):
if not is_node_valid_with_prefixes(node, self.prefixes):
continue

df_node = df[df['node'] == node]
Expand Down Expand Up @@ -91,7 +91,7 @@ def parse_stats(self):
@return: results is a dictionary where each key is a timestamp, and its
value is a list of nodes that rolled out at that timestamp
'''
def node_group_by_timestamp(self):
def group_nodes_by_timestamp(self):
node_groups = dict()
for node in self.data_nodes:
ts = int(node.split("-")[1])
Expand Down Expand Up @@ -137,7 +137,7 @@ def calc_p_by_group(self, node_groups, P, reverse=False):

def plot(self):
self.parse_stats()
node_groups = self.node_group_by_timestamp()
node_groups = self.group_nodes_by_timestamp()
results = self.calc_avg_by_group(node_groups) if self.P == -1 else self.calc_p_by_group(node_groups, self.P, reverse=True)
normalized_timestamps = [ts - self.start_time for ts in self.timestamps]

Expand Down Expand Up @@ -179,8 +179,8 @@ def parse_dht_log(self):
continue

# node_id: [peer1, peer2, ...], [peer3, peer4, ...], ...
node_id, peer_list = line.strip().split(':')
if not node_valid_with_prefixes(node_id, self.prefixes):
node_id, peer_list = line.split(':')
if not is_node_valid_with_prefixes(node_id, self.prefixes):
continue

buckets = peer_list.strip().split(',')
Expand Down Expand Up @@ -210,8 +210,8 @@ def parse_peers_log(self, only_outbound=True):
continue

# node_id: (peer1 inbound), (peer2 inbound), ...
node_id, peer_list = line.strip().split(':')
if not node_valid_with_prefixes(node_id, self.prefixes):
node_id, peer_list = line.split(':')
if not is_node_valid_with_prefixes(node_id, self.prefixes):
continue

for ts in peer_list.strip().split(','):
Expand All @@ -229,9 +229,7 @@ def plot(self):
dht_data = self.parse_dht_log()
peer_data = self.parse_peers_log()

x = []
y = []

x, y = [], []
for node_id, peers in peer_data.items():
count_peer_in_dht = 0
for peer in peers:
Expand Down Expand Up @@ -312,11 +310,12 @@ def main():
else:
try:
time_range = [int(x) for x in args.time_range.split(",")]
except:
except Exception as e:
time_range = [-1, -1]
print("Error: Invalid time range format. Using default time")
print(f"Error: Invalid time range format, err: {str(e)}. Using default time")
if args.stats_file is None:
print("Error: Stats file is required for other types.")
return
plotter = StatsPlotter(args.stats_file, args.result_dir, args.type, args.P, time_range, args.prefixes.split(","))
plotter.plot()

Expand Down

0 comments on commit c9753ba

Please sign in to comment.