Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env vpython3 |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 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 sys |
| 12 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 13 | import psutil |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 14 | import numpy |
| 15 | from matplotlib import pyplot |
| 16 | |
| 17 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 18 | class CpuSnapshot: |
| 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 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +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 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 29 | 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.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 34 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +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): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 40 | print('Label for snapshot (enter to quit): ') |
| 41 | label = eval(input().strip()) |
| 42 | if len(label) == 0: |
| 43 | return None |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 44 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +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 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 48 | return snapshot |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def main(): |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 52 | 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.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 59 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +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 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +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 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 71 | pyplot.title('CPU usage') |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 72 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +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 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 76 | pyplot.legend() |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 77 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 78 | pyplot.show() |
| 79 | return 0 |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 80 | |
tommi@webrtc.org | 5263c58 | 2014-12-17 12:35:29 +0000 | [diff] [blame] | 81 | |
| 82 | if __name__ == '__main__': |
Christoffer Jansson | 4e8a773 | 2022-02-08 09:01:12 +0100 | [diff] [blame^] | 83 | sys.exit(main()) |