You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  4. #
  5. # Use of this source code is governed by a BSD-style license
  6. # that can be found in the LICENSE file in the root of the source
  7. # tree. An additional intellectual property rights grant can be found
  8. # in the file PATENTS. All contributing project authors may
  9. # be found in the AUTHORS file in the root of the source tree.
  10. import psutil
  11. import sys
  12. import numpy
  13. from matplotlib import pyplot
  14. class CpuSnapshot(object):
  15. def __init__(self, label):
  16. self.label = label
  17. self.samples = []
  18. def Capture(self, sample_count):
  19. print ('Capturing %d CPU samples for %s...' %
  20. ((sample_count - len(self.samples)), self.label))
  21. while len(self.samples) < sample_count:
  22. self.samples.append(psutil.cpu_percent(1.0, False))
  23. def Text(self):
  24. return ('%s: avg=%s, median=%s, min=%s, max=%s' %
  25. (self.label, numpy.average(self.samples),
  26. numpy.median(self.samples),
  27. numpy.min(self.samples), numpy.max(self.samples)))
  28. def Max(self):
  29. return numpy.max(self.samples)
  30. def GrabCpuSamples(sample_count):
  31. print 'Label for snapshot (enter to quit): '
  32. label = raw_input().strip()
  33. if len(label) == 0:
  34. return None
  35. snapshot = CpuSnapshot(label)
  36. snapshot.Capture(sample_count)
  37. return snapshot
  38. def main():
  39. print 'How many seconds to capture per snapshot (enter for 60)?'
  40. sample_count = raw_input().strip()
  41. if len(sample_count) > 0 and int(sample_count) > 0:
  42. sample_count = int(sample_count)
  43. else:
  44. print 'Defaulting to 60 samples.'
  45. sample_count = 60
  46. snapshots = []
  47. while True:
  48. snapshot = GrabCpuSamples(sample_count)
  49. if snapshot is None:
  50. break
  51. snapshots.append(snapshot)
  52. if len(snapshots) == 0:
  53. print 'no samples captured'
  54. return -1
  55. pyplot.title('CPU usage')
  56. for s in snapshots:
  57. pyplot.plot(s.samples, label=s.Text(), linewidth=2)
  58. pyplot.legend()
  59. pyplot.show()
  60. return 0
  61. if __name__ == '__main__':
  62. sys.exit(main())