Guest User

Untitled

a guest
Jan 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import csv
  4. import argparse
  5. import statistics
  6. from dateutil import parser, tz
  7. from datetime import datetime, timedelta
  8. from subprocess import call
  9. import matplotlib.pyplot as plt
  10. import matplotlib.dates as mdates
  11. import matplotlib.ticker as plticker
  12.  
  13.  
  14. argparser = argparse.ArgumentParser(description="Plot results")
  15. group = argparser.add_mutually_exclusive_group()
  16. group.add_argument("-u", "--up", action="store_true")
  17. group.add_argument("-d", "--down", action="store_true")
  18. args = argparser.parse_args()
  19.  
  20. res = call(["scp", "raspi:~/speedtest/reports.txt", "."])
  21.  
  22. dates = []
  23. download_speeds = []
  24.  
  25. # fields: Server ID,Sponsor,Server Name,Timestamp,Distance,Ping,Download,Upload
  26. # idx: 0 ,1 ,2 ,3 ,4 ,5 ,6 ,7
  27.  
  28. prevdate = None
  29. interval = timedelta(minutes=15)
  30. to_zone = tz.tzlocal()
  31.  
  32.  
  33. class HFormatter(mdates.DateFormatter):
  34. def strftime(self, dt, fmt=None):
  35. if dt.hour % 3 == 0 and dt.hour != 0:
  36. return super().strftime(dt)
  37. else:
  38. return ""
  39.  
  40. with open('reports.txt') as csvfile:
  41. data = csv.reader(csvfile)
  42. for line in data:
  43. if len(line) == 8:
  44. prevdate = parser.parse(line[3])
  45. prevdate.astimezone(to_zone)
  46. dates.append(prevdate)
  47. if args.up:
  48. download_speeds.append(float(line[7]) / 1000000)
  49. else:
  50. download_speeds.append(float(line[6]) / 1000000)
  51. elif prevdate:
  52. prevdate = prevdate + interval
  53. dates.append(prevdate)
  54. download_speeds.append(download_speeds[-1])
  55.  
  56. days = mdates.DayLocator()
  57. hours = mdates.HourLocator()
  58. d_fmt = mdates.DateFormatter("%a, %-d.%-m %H:%M")
  59. h_fmt = HFormatter("%H:%M")
  60.  
  61. fig, ax = plt.subplots()
  62. ax.plot(dates, download_speeds)
  63.  
  64. if args.up:
  65. ones = plticker.MultipleLocator(base=1.0)
  66. ax.yaxis.set_major_locator(ones)
  67. else:
  68. tens = plticker.MultipleLocator(base=10)
  69. ones = plticker.MultipleLocator(base=2)
  70. ax.yaxis.set_major_locator(tens)
  71. ax.yaxis.set_minor_locator(ones)
  72.  
  73. ax.xaxis.set_major_locator(days)
  74. ax.xaxis.set_major_formatter(d_fmt)
  75. ax.xaxis.set_minor_locator(hours)
  76. ax.xaxis.set_minor_formatter(h_fmt)
  77. ax.xaxis.grid(True, which='major')
  78. # ax.yaxis.grid(True, which='major')
  79.  
  80. # datemin = datetime.date(dates.min().year, 1, 1)
  81. # datemax = datetime.date(dates.max().year + 1, 1, 1)
  82. # ax.set_xlim(datemin, datemax)
  83.  
  84. fig.autofmt_xdate(rotation=30, which='both')
  85.  
  86. plt.ylabel("mbit/s")
  87. plt.xlabel("time")
  88. plt.title('%s speed, 15min interval. avg: %.2f; median: %.2f. ' % (('upload' if args.up else 'download'), sum(download_speeds) / len(download_speeds), statistics.median(download_speeds)))
  89. # plt.savefig('%s.png' % (('upload' if args.up else 'download')))
  90. plt.show()
Add Comment
Please, Sign In to add comment