blob: 9c25fbd0887960b26bd3ef5520b40edea52df525 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
tommi@webrtc.org5263c582014-12-17 12:35:29 +00002#
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 sys
12
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010013import psutil
tommi@webrtc.org5263c582014-12-17 12:35:29 +000014import numpy
15from matplotlib import pyplot
16
17
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010018class CpuSnapshot:
19 def __init__(self, label):
20 self.label = label
21 self.samples = []
tommi@webrtc.org5263c582014-12-17 12:35:29 +000022
Christoffer Jansson4e8a7732022-02-08 09:01:12 +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
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010029 def Text(self):
30 return (
31 '%s: avg=%s, median=%s, min=%s, max=%s' %
32 (self.label, numpy.average(self.samples), numpy.median(
33 self.samples), numpy.min(self.samples), numpy.max(self.samples)))
tommi@webrtc.org5263c582014-12-17 12:35:29 +000034
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010035 def Max(self):
36 return numpy.max(self.samples)
tommi@webrtc.org5263c582014-12-17 12:35:29 +000037
38
39def GrabCpuSamples(sample_count):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010040 print('Label for snapshot (enter to quit): ')
41 label = eval(input().strip())
42 if len(label) == 0:
43 return None
tommi@webrtc.org5263c582014-12-17 12:35:29 +000044
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010045 snapshot = CpuSnapshot(label)
46 snapshot.Capture(sample_count)
tommi@webrtc.org5263c582014-12-17 12:35:29 +000047
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010048 return snapshot
tommi@webrtc.org5263c582014-12-17 12:35:29 +000049
50
51def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010052 print('How many seconds to capture per snapshot (enter for 60)?')
53 sample_count = eval(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
Christoffer Jansson4e8a7732022-02-08 09:01:12 +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
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010067 if len(snapshots) == 0:
68 print('no samples captured')
69 return -1
tommi@webrtc.org5263c582014-12-17 12:35:29 +000070
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010071 pyplot.title('CPU usage')
tommi@webrtc.org5263c582014-12-17 12:35:29 +000072
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010073 for s in snapshots:
74 pyplot.plot(s.samples, label=s.Text(), linewidth=2)
tommi@webrtc.org5263c582014-12-17 12:35:29 +000075
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010076 pyplot.legend()
tommi@webrtc.org5263c582014-12-17 12:35:29 +000077
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010078 pyplot.show()
79 return 0
Mirko Bonadei8cc66952020-10-30 10:13:45 +010080
tommi@webrtc.org5263c582014-12-17 12:35:29 +000081
82if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010083 sys.exit(main())