blob: d89935aeab7c52fbc403e3f0e2229a63bf0b693f [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
tommi@webrtc.org5263c582014-12-17 12:35:29 +000011import psutil
12import sys
13
14import numpy
15from matplotlib import pyplot
16
17
18class CpuSnapshot(object):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010019 def __init__(self, label):
20 self.label = label
21 self.samples = []
tommi@webrtc.org5263c582014-12-17 12:35:29 +000022
Mirko Bonadei8cc66952020-10-30 10:13:45 +010023 def Capture(self, sample_count):
24 print('Capturing %d CPU samples for %s...' %
25 ((sample_count - len(self.samples)), self.label))
26 while len(self.samples) < sample_count:
27 self.samples.append(psutil.cpu_percent(1.0, False))
tommi@webrtc.org5263c582014-12-17 12:35:29 +000028
Mirko Bonadei8cc66952020-10-30 10:13:45 +010029 def Text(self):
30 return ('%s: avg=%s, median=%s, min=%s, max=%s' %
31 (self.label, numpy.average(self.samples),
32 numpy.median(self.samples), numpy.min(
33 self.samples), numpy.max(self.samples)))
tommi@webrtc.org5263c582014-12-17 12:35:29 +000034
Mirko Bonadei8cc66952020-10-30 10:13:45 +010035 def Max(self):
36 return numpy.max(self.samples)
tommi@webrtc.org5263c582014-12-17 12:35:29 +000037
38
39def GrabCpuSamples(sample_count):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010040 print 'Label for snapshot (enter to quit): '
41 label = raw_input().strip()
42 if len(label) == 0:
43 return None
tommi@webrtc.org5263c582014-12-17 12:35:29 +000044
Mirko Bonadei8cc66952020-10-30 10:13:45 +010045 snapshot = CpuSnapshot(label)
46 snapshot.Capture(sample_count)
tommi@webrtc.org5263c582014-12-17 12:35:29 +000047
Mirko Bonadei8cc66952020-10-30 10:13:45 +010048 return snapshot
tommi@webrtc.org5263c582014-12-17 12:35:29 +000049
50
51def main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +010052 print 'How many seconds to capture per snapshot (enter for 60)?'
53 sample_count = raw_input().strip()
54 if len(sample_count) > 0 and int(sample_count) > 0:
55 sample_count = int(sample_count)
56 else:
57 print 'Defaulting to 60 samples.'
58 sample_count = 60
tommi@webrtc.org5263c582014-12-17 12:35:29 +000059
Mirko Bonadei8cc66952020-10-30 10:13:45 +010060 snapshots = []
61 while True:
62 snapshot = GrabCpuSamples(sample_count)
63 if snapshot is None:
64 break
65 snapshots.append(snapshot)
tommi@webrtc.org5263c582014-12-17 12:35:29 +000066
Mirko Bonadei8cc66952020-10-30 10:13:45 +010067 if len(snapshots) == 0:
68 print 'no samples captured'
69 return -1
tommi@webrtc.org5263c582014-12-17 12:35:29 +000070
Mirko Bonadei8cc66952020-10-30 10:13:45 +010071 pyplot.title('CPU usage')
tommi@webrtc.org5263c582014-12-17 12:35:29 +000072
Mirko Bonadei8cc66952020-10-30 10:13:45 +010073 for s in snapshots:
74 pyplot.plot(s.samples, label=s.Text(), linewidth=2)
tommi@webrtc.org5263c582014-12-17 12:35:29 +000075
Mirko Bonadei8cc66952020-10-30 10:13:45 +010076 pyplot.legend()
tommi@webrtc.org5263c582014-12-17 12:35:29 +000077
Mirko Bonadei8cc66952020-10-30 10:13:45 +010078 pyplot.show()
79 return 0
80
tommi@webrtc.org5263c582014-12-17 12:35:29 +000081
82if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +010083 sys.exit(main())