blob: 9a13723ed2d5970d622e027b72e2808d99cd2ff2 [file] [log] [blame]
tommi@webrtc.org5263c582014-12-17 12:35:291#!/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
11
tommi@webrtc.org5263c582014-12-17 12:35:2912import psutil
13import sys
14
15import numpy
16from matplotlib import pyplot
17
18
19class CpuSnapshot(object):
20 def __init__(self, label):
21 self.label = label
22 self.samples = []
23
24 def Capture(self, sample_count):
25 print ('Capturing %d CPU samples for %s...' %
26 ((sample_count - len(self.samples)), self.label))
27 while len(self.samples) < sample_count:
28 self.samples.append(psutil.cpu_percent(1.0, False))
29
30 def Text(self):
31 return ('%s: avg=%s, median=%s, min=%s, max=%s' %
32 (self.label, numpy.average(self.samples),
33 numpy.median(self.samples),
34 numpy.min(self.samples), numpy.max(self.samples)))
35
36 def Max(self):
37 return numpy.max(self.samples)
38
39
40def GrabCpuSamples(sample_count):
41 print 'Label for snapshot (enter to quit): '
42 label = raw_input().strip()
43 if len(label) == 0:
44 return None
45
46 snapshot = CpuSnapshot(label)
47 snapshot.Capture(sample_count)
48
49 return snapshot
50
51
52def main():
53 print 'How many seconds to capture per snapshot (enter for 60)?'
54 sample_count = raw_input().strip()
55 if len(sample_count) > 0 and int(sample_count) > 0:
56 sample_count = int(sample_count)
57 else:
58 print 'Defaulting to 60 samples.'
59 sample_count = 60
60
61 snapshots = []
62 while True:
63 snapshot = GrabCpuSamples(sample_count)
64 if snapshot == None:
65 break
66 snapshots.append(snapshot)
67
68 if len(snapshots) == 0:
69 print 'no samples captured'
70 return -1
71
72 pyplot.title('CPU usage')
73
74 for s in snapshots:
75 pyplot.plot(s.samples, label=s.Text(), linewidth=2)
76
77 pyplot.legend()
78
79 pyplot.show()
80 return 0
81
82if __name__ == '__main__':
83 sys.exit(main())