Added averages plotting
This commit is contained in:
68
lib.py
Normal file
68
lib.py
Normal file
@@ -0,0 +1,68 @@
|
||||
import os
|
||||
import glob
|
||||
import matplotlib.pyplot as plt
|
||||
import math
|
||||
import csv
|
||||
from datetime import datetime
|
||||
from cfspeedtest import CloudflareSpeedtest
|
||||
|
||||
def run_test():
|
||||
suite = CloudflareSpeedtest()
|
||||
results = suite.run_all()
|
||||
tests = results['tests']
|
||||
time = tests['isp'].time
|
||||
latency = tests['latency'].value
|
||||
jitter = tests['jitter'].value
|
||||
h_down = tests['100kB_down_bps'].value
|
||||
o_down = tests['1MB_down_bps'].value
|
||||
t_down = tests['10MB_down_bps'].value
|
||||
q_down = tests['25MB_down_bps'].value
|
||||
np_down = tests['90th_percentile_down_bps'].value
|
||||
h_up = tests['100kB_up_bps'].value
|
||||
o_up = tests['1MB_up_bps'].value
|
||||
t_up = tests['10MB_up_bps'].value
|
||||
np_up = tests['90th_percentile_up_bps'].value
|
||||
return [time, latency, jitter, h_down, o_down, t_down, q_down, h_up, o_up, t_up, np_down, np_up]
|
||||
|
||||
def write_data(filename, data):
|
||||
with open(filename, 'a', newline='\n') as csvfile:
|
||||
writer = csv.writer(csvfile)
|
||||
if os.path.getsize(filename) == 0:
|
||||
writer.writerow(['time','latency','jitter','100kB_down','1MB_down','10MB_down','25MB_down','100kB_up','1MB_up','10MB_up','90th_percentile_down','90th_percentile_up'])
|
||||
writer.writerow(data)
|
||||
|
||||
def plot(time, data1, data2, label1, label2, filename, autofmt=True, xticks=None):
|
||||
fig_length = -11.36 + 5.46 * math.log(len(time))
|
||||
fig_length = max(6.0, fig_length)
|
||||
plt.figure(figsize=[int(fig_length), 6])
|
||||
plt.plot(time, data1)
|
||||
plt.plot(time, data2)
|
||||
plt.legend([label1, label2])
|
||||
if autofmt:
|
||||
plt.gcf().autofmt_xdate()
|
||||
if xticks is not None:
|
||||
plt.xticks(xticks)
|
||||
plt.savefig(filename)
|
||||
|
||||
def import_csv(filenames):
|
||||
time_data = []
|
||||
ping_data = []
|
||||
jitter_data = []
|
||||
up_data = []
|
||||
down_data = []
|
||||
|
||||
for i in range(len(filenames)):
|
||||
files = glob.glob(filenames[i])
|
||||
for j in range(len(files)):
|
||||
with open(files[j], 'r', newline='\n') as csvfile:
|
||||
reader = csv.reader(csvfile)
|
||||
line_count = 0
|
||||
for row in reader:
|
||||
if line_count != 0:
|
||||
time_data.append(datetime.fromtimestamp(float(row[0])))
|
||||
ping_data.append(float(row[1]))
|
||||
jitter_data.append(float(row[2]))
|
||||
down_data.append(int(row[10]) / 1000000)
|
||||
up_data.append(int(row[11]) / 1000000)
|
||||
line_count += 1
|
||||
return time_data, ping_data, jitter_data, up_data, down_data
|
||||
Reference in New Issue
Block a user