forked from ineiti/fledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_data.py
148 lines (106 loc) · 4.27 KB
/
parse_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import re
import json
import os
import yaml
def get_latency(log_file_path, data):
time_pattern = re.compile(r"Total time for request: (\d+)")
try:
with open(log_file_path, 'r') as file:
log_data = file.readlines()
except IOError as e:
print(f"Error opening file {log_file_path}: {e}")
return data
for line in log_data:
match = time_pattern.search(line)
if match:
data["total_times"].append(int(match.group(1)))
return data
def get_latency_data(data_dir, dir):
log_file_path = os.path.join(data_dir, dir, 'logs.txt')
output_json_path = os.path.join(data_dir, dir, 'data.json')
data = {
"total_times": [],
}
data = get_latency(log_file_path, data)
with open(output_json_path, 'w') as json_file:
json.dump(data, json_file, indent=4)
print(f"Log data written to {output_json_path}")
return data
def get_n_storages(data_dir):
pattern = re.compile(r"loopix_storage_\d+\.yaml")
matching_files = [f for f in os.listdir(data_dir) if pattern.match(f)]
count = len(matching_files)
return count
def count_all_messages(data_dir, dir):
path = os.path.join(data_dir, dir)
n_storages = get_n_storages(path)
total_forwarded = 0
total_sent = 0
total_received = 0
for i in range(1, n_storages + 1):
yaml_file_path = os.path.join(path, f"loopix_storage_{i:02}.yaml")
data = count_messages(yaml_file_path)
total_forwarded += data.get("forwarded_messages", 0)
total_sent += data.get("sent_messages", 0)
total_received += data.get("received_messages", 0)
message_data = {
"total_forwarded": total_forwarded,
"total_sent": total_sent,
"total_received": total_received
}
output_json_path = os.path.join(path, 'all_messages.json')
with open(output_json_path, 'w') as json_file:
json.dump(message_data, json_file, indent=4)
print(f"Message data written to {output_json_path}")
return message_data
def count_messages(yaml_file_path):
print(f"Counting messages in {yaml_file_path}")
data = {
"forwarded_messages": 0,
"sent_messages": 0,
"received_messages": 0,
}
try:
with open(yaml_file_path, 'r') as file:
yaml_data = yaml.safe_load(file)
network_storage = yaml_data.get('network_storage', {})
if 'forwarded_messages' in network_storage:
data["forwarded_messages"] = len(network_storage['forwarded_messages'])
else:
print("No 'forwarded_messages' key found in the YAML file.")
if 'sent_messages' in network_storage:
data["sent_messages"] = len(network_storage['sent_messages'])
else:
print("No 'sent_messages' key found in the YAML file.")
if 'received_messages' in network_storage:
data["received_messages"] = len(network_storage['received_messages'])
else:
print("No 'received_messages' key found in the YAML file.")
except FileNotFoundError:
print(f"Error: File not found at {yaml_file_path}")
except yaml.YAMLError as e:
print(f"Error parsing YAML file: {e}")
return data
def get_directories(directory_path):
directories = [d for d in os.listdir(directory_path) if os.path.isdir(os.path.join(directory_path, d))]
return directories
def main():
simul_data_dir = os.path.dirname('./simul_data/')
configs = ["lambda", "max_retrieve", "mean_delay", "path_length", "time_pull"]
aggregated_data = {
"latency": {},
"messages": {}
}
for config in configs:
data_dir = os.path.join(simul_data_dir, config)
aggregated_data["latency"][config] = {}
aggregated_data["messages"][config] = {}
for dir in get_directories(data_dir):
aggregated_data["latency"][config][dir] = get_latency_data(data_dir, dir)
aggregated_data["messages"][config][dir] = count_all_messages(data_dir, dir)
big_json_path = os.path.join(simul_data_dir, 'aggregated_data.json')
with open(big_json_path, 'w') as big_json_file:
json.dump(aggregated_data, big_json_file, indent=4)
print(f"Aggregated data written to {big_json_path}")
if __name__ == "__main__":
main()