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