Advertisement
Guest User

Plot Sleep Behavior

a guest
Jun 5th, 2015
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # from http://stackoverflow.com/a/30672412
  3. import platform
  4. import matplotlib # $ pip install matplotlib
  5. matplotlib.use('agg')
  6. import pylab
  7.  
  8. import random
  9. from time import sleep
  10. from datetime import datetime, timedelta
  11.  
  12. max_sleep_time = 0.01
  13.  
  14. req_sleep = []
  15. act_sleep = []
  16.  
  17. for samp in xrange(1000):
  18.     sleep_time = random.random() * max_sleep_time
  19.     req_sleep.append(sleep_time * 1000.)
  20.  
  21.     start = datetime.now()
  22.     sleep(sleep_time)
  23.     end = datetime.now()
  24.  
  25.     act_sleep.append((end - start).total_seconds() * 1000.)
  26.  
  27. pylab.figure()
  28.  
  29. pylab.plot(req_sleep, act_sleep, 'r+')
  30. pylab.plot([0, max_sleep_time * 1000], [0, max_sleep_time * 1000.], 'k-')
  31. pylab.plot([0, 0.8 * max_sleep_time * 1000], [0, max_sleep_time * 1000.], 'k--')
  32. pylab.xlabel("Requested Sleep Time ($t_r$; ms)")
  33. pylab.ylabel("Actual Sleep Time ($t_s$; ms)")
  34. pylab.xlim(0, max_sleep_time * 1000.)
  35. pylab.ylim(0, max_sleep_time * 1000.)
  36.  
  37. pylab.annotate(r"$t_s \approx \frac{5}{4} t_r$", xy=(4, 5), xytext=(0.3, 0.7), textcoords='axes fraction',
  38.             arrowprops=dict(arrowstyle="->", connectionstyle="angle3,angleA=-90,angleB=-45"),
  39.             fontsize=14, bbox=dict(fc='w', ec='k', boxstyle='round'),
  40.             ha='center',
  41.             )  
  42.  
  43. platform_id = platform.platform()
  44. pylab.title("Sleep behavior of " + platform_id)
  45. pylab.grid()
  46. pylab.savefig("sleeptest%s.png" % platform_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement