tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 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 | |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 11 | import psutil |
| 12 | import sys |
| 13 | |
| 14 | import numpy |
| 15 | from matplotlib import pyplot |
| 16 | |
| 17 | |
| 18 | class CpuSnapshot(object): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 19 | def __init__(self, label): |
| 20 | self.label = label |
| 21 | self.samples = [] |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 22 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 23 | 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.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 28 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 29 | 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.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 34 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 35 | def Max(self): |
| 36 | return numpy.max(self.samples) |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def GrabCpuSamples(sample_count): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 40 | print 'Label for snapshot (enter to quit): ' |
| 41 | label = raw_input().strip() |
| 42 | if len(label) == 0: |
| 43 | return None |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 44 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 45 | snapshot = CpuSnapshot(label) |
| 46 | snapshot.Capture(sample_count) |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 47 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 48 | return snapshot |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def main(): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 52 | 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.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 59 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 60 | snapshots = [] |
| 61 | while True: |
| 62 | snapshot = GrabCpuSamples(sample_count) |
| 63 | if snapshot is None: |
| 64 | break |
| 65 | snapshots.append(snapshot) |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 66 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 67 | if len(snapshots) == 0: |
| 68 | print 'no samples captured' |
| 69 | return -1 |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 70 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 71 | pyplot.title('CPU usage') |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 72 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 73 | for s in snapshots: |
| 74 | pyplot.plot(s.samples, label=s.Text(), linewidth=2) |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 75 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 76 | pyplot.legend() |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 77 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 78 | pyplot.show() |
| 79 | return 0 |
| 80 | |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 81 | |
| 82 | if __name__ == '__main__': |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 83 | sys.exit(main()) |