Repository navigation
Expand file tree
/
Copy pathdata_parser.py
More file actions
137 lines (124 loc) · 4.73 KB
/
Copy pathdata_parser.py
File metadata and controls
137 lines (124 loc) · 4.73 KB
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
#!/usr/bin/python
## Source: standard_deviation()
## https://codeselfstudy.com/blog/how-to-calculate-standard-deviation-in-python/
import os
from math import sqrt
from pprint import pprint
########################################
#### Script configuration ####
########################################
BASE_DIR = "/home/zaca/Dropbox/UFRGS/CH_Selection/results"
FOLDER_BASE_NAME = "%snodes"
FILE_BASE_NAME = "nodes%s.log"
IPERF_BASE_NAME = "iperf_s_n%s_r%s"
NODE_SET = ["05", "10", "15", "20", "25", "30"]
########################################
#### Calculate Avg and Std Dev ####
########################################
def standard_deviation(lst, population=False):
"""Calculates the standard deviation for a list of numbers."""
num_items = len(lst)
mean = sum(lst) / num_items
differences = [x - mean for x in lst]
sq_differences = [d ** 2 for d in differences]
ssd = sum(sq_differences)
# Note: it would be better to return a value and then print it outside
# the function, but this is just a quick way to print out the values along
# the way.
if population is True:
variance = ssd / num_items
else:
variance = ssd / (num_items - 1)
std_dev = sqrt(variance)
# The mean: mean
# The differences: differnces
# The sum of squared differences: ssd
# The variance: variance
# The standard deviation: std_dev
return mean, std_dev
########################################
#### Extract Iperf Measures ####
########################################
def lost_bytes(iperf_file_path=""):
# Open the file
iperf_file = open(iperf_file_path, 'r')
data = iperf_file.readlines()
iperf_file.close()
# Extract data
lost_datagrams = 0
for i in range(len(data) -1, 0, -1):
if data[i].count("Mbits/sec"):
lost_datagrams = int(data[i].split(" ")[16].split("/")[0])
break
datagram_size = int(data[2].split(" ")[1])
return int(lost_datagrams * datagram_size)
########################################
#### Main Function ####
########################################
def get_stats(no_of_nodes = "05"):
# Open the file (read-only)
folder_name = FOLDER_BASE_NAME % no_of_nodes
file_name = FILE_BASE_NAME % no_of_nodes
file_path = os.path.join(BASE_DIR, folder_name, file_name)
f = open(file_path)
line = f.readline()
run_no = 0
iter_list = []
time_list = []
report = []
while line:
if '#####' not in line:
# data line
data = line.split(";")
iter_list.append(int(data[5]))
time_list.append(float(data[7]))
if '#####' in line:
# summarize
if iter_list and time_list:
max_iter = max(iter_list)
max_time = max(time_list)
# increment the run_number
run_no = run_no + 1
# get lost bytes from iperf
iperf_file_name = IPERF_BASE_NAME % (no_of_nodes, str(run_no).zfill(2))
iperf_path = os.path.join(BASE_DIR, folder_name, iperf_file_name)
lost_data = lost_bytes(iperf_path)
# salva os dados do grupo
report.append([run_no, max_iter, max_time, lost_data])
# inicia novo grupo
iter_list = []
time_list = []
# read next line
line = f.readline()
# Add the result of the last group
# sumariza
if iter_list and time_list:
max_iter = max(iter_list)
max_time = max(time_list)
# increment the run_number
run_no = run_no + 1
# get lost bytes from iperf
iperf_file_name = IPERF_BASE_NAME % (no_of_nodes, str(run_no))
iperf_path = os.path.join(BASE_DIR, folder_name, iperf_file_name)
lost_data = lost_bytes(iperf_path)
# salva os dados do grupo
report.append([run_no, max_iter, max_time, lost_data])
# close the file
f.close()
# separate measures in arrays
rounds_arr = ([ rounds_elect for run, rounds_elect, max_time, bytes_lost in report ])
time_arr = ([ max_time for run, rounds_elect, max_time, bytes_lost in report ])
lost_arr = ([ bytes_lost for run, rounds_elect, max_time, bytes_lost in report ])
# process the stats
round_avg, round_sd = standard_deviation(rounds_arr)
time_avg, time_sd = standard_deviation(time_arr)
lost_avg, lost_sd = standard_deviation(lost_arr)
# NodeNo -- rounds(avg, sd) -- time(avg, sd) -- lost (avg, sd)
print "%i \t %.6f \t %.6f \t %.6f \t %.6f \t %.2f \t %.6f" % (
int(no_of_nodes),
round_avg, round_sd,
time_avg, time_sd,
lost_avg, lost_sd)
if __name__ == '__main__':
for node in NODE_SET:
get_stats(node)