bandwidth.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # greaseweazle/tools/bandwidth.py
  2. #
  3. # Greaseweazle control script: Measure USB bandwidth.
  4. #
  5. # Written & released by Keir Fraser <keir.xen@gmail.com>
  6. #
  7. # This is free and unencumbered software released into the public domain.
  8. # See the file COPYING for more details, or visit <http://unlicense.org>.
  9. description = "Report the available USB bandwidth for the Greaseweazle device."
  10. import sys
  11. from timeit import default_timer as timer
  12. from greaseweazle.tools import util
  13. from greaseweazle import usb as USB
  14. def measure_bandwidth(usb, args):
  15. print(" Min / Mean / Max")
  16. w_nr = 1000000
  17. start = timer()
  18. usb.sink_bytes(w_nr)
  19. end = timer()
  20. av_bw = (w_nr * 8) / ((end-start) * 1000000)
  21. min_bw, max_bw = usb.bw_stats()
  22. print("Write Bandwidth: %.3f / %.3f / %.3f Mbps"
  23. % (min_bw, av_bw, max_bw))
  24. r_nr = 1000000
  25. start = timer()
  26. usb.source_bytes(r_nr)
  27. end = timer()
  28. av_bw = (r_nr * 8) / ((end-start) * 1000000)
  29. min_bw, max_bw = usb.bw_stats()
  30. print("Read Bandwidth: %.3f / %.3f / %.3f Mbps"
  31. % (min_bw, av_bw, max_bw))
  32. twobyte_us = 249/72 # Smallest time requiring a 2-byte transmission code
  33. min_bw = 16 / twobyte_us # Bandwidth (Mbps) to transmit above time
  34. print("Minimum *consistent* bandwidth required: %.3f Mbps" % min_bw)
  35. def main(argv):
  36. parser = util.ArgumentParser()
  37. parser.add_argument("device", nargs="?", default="auto",
  38. help="serial device")
  39. parser.description = description
  40. parser.prog += ' ' + argv[1]
  41. args = parser.parse_args(argv[2:])
  42. try:
  43. usb = util.usb_open(args.device)
  44. measure_bandwidth(usb, args)
  45. except USB.CmdError as error:
  46. print("Command Failed: %s" % error)
  47. if __name__ == "__main__":
  48. main(sys.argv)
  49. # Local variables:
  50. # python-indent: 4
  51. # End: