Debian Codegraph @

scripts/performance/benchmark

raw
1 #!/usr/bin/env python
2 """
3 Use for benchmarking performance of other scripts. Provides data about
4 time, memory use, cpu usage, network in, network out about the script ran in
5 the form of a csv.
6
7
8 Usage
9 =====
10
11 NOTE: Make sure you run ``pip install -r requirements-dev.txt`` before running.
12
13 To use the script, run::
14
15 ./benchmark "./my-script-to-run"
16
17
18 If no ``--output-file`` was provided, the data will be saved to
19 ``performance.csv``
20 """
21
22 import argparse
23 import os
24 import subprocess
25 import sys
26 import time
27
28 import psutil
29
30 # Determine the interface to track network IO depending on the platform.
31 if sys.platform.startswith('linux'):
32 INTERFACE = 'eth0'
33 elif sys.platform == 'darwin':
34 INTERFACE = 'en0'
35 else:
36 # TODO: Add support for windows. This would require figuring out what
37 # interface to use on windows.
38 raise RuntimeError(f'Script cannot be run on {sys.platform}')
39
40
41 def benchmark(args):
42 parent_pid = os.getpid()
43 child_p = run_script(args)
44 try:
45 # Benchmark the process where the script is being ran.
46 return run_benchmark(child_p.pid, args.output_file, args.data_interval)
47 except KeyboardInterrupt:
48 # If there is an interrupt, then try to clean everything up.
49 proc = psutil.Process(parent_pid)
50 procs = proc.children(recursive=True)
51
52 for child in procs:
53 child.terminate()
54
55 gone, alive = psutil.wait_procs(procs, timeout=1)
56 for child in alive:
57 child.kill()
58 return 1
59
60
61 def run_script(args):
62 return subprocess.Popen(args.script, shell=True)
63
64
65 def run_benchmark(pid, output_file, data_interval):
66 p = psutil.Process(pid)
67 previous_net = psutil.net_io_counters(pernic=True)[INTERFACE]
68 previous_time = time.time()
69
70 with open(output_file, 'w') as f:
71 while p.is_running():
72 if p.status() == psutil.STATUS_ZOMBIE:
73 p.kill()
74 break
75 time.sleep(data_interval)
76 process_to_measure = _get_underlying_python_process(p)
77 try:
78 # Collect the memory and cpu usage.
79 memory_used = process_to_measure.memory_info().rss
80 cpu_percent = process_to_measure.cpu_percent()
81 current_net = psutil.net_io_counters(pernic=True)[INTERFACE]
82 except (psutil.AccessDenied, psutil.ZombieProcess):
83 # Trying to get process information from a closed or zombie process will
84 # result in corresponding exceptions.
85 break
86
87 # Collect data on the in/out network io.
88 sent_delta = current_net.bytes_sent - previous_net.bytes_sent
89 recv_delta = current_net.bytes_recv - previous_net.bytes_recv
90
91 # Determine the lapsed time to determine the network io rate.
92 current_time = time.time()
93 previous_net = current_net
94 dt = current_time - previous_time
95 previous_time = current_time
96 sent_rate = sent_delta / dt
97 recv_rate = recv_delta / dt
98
99 # Save all of the data into a CSV file.
100 f.write(
101 f"{current_time},{memory_used},{cpu_percent},"
102 f"{sent_rate},{recv_rate}\n"
103 )
104 f.flush()
105 return 0
106
107
108 def _get_underlying_python_process(process):
109 # For some scripts such as the streaming CLI commands, the process is
110 # nested under a shell script that does not account for the python process.
111 # We want to always be measuring the python process.
112 children = process.children(recursive=True)
113 for child_process in children:
114 if 'python' in child_process.name().lower():
115 return child_process
116 return process
117
118
119 def main():
120 parser = argparse.ArgumentParser(usage=__doc__)
121 parser.add_argument('script', help='The script to run for benchmarking')
122 parser.add_argument(
123 '--data-interval',
124 default=1,
125 type=float,
126 help='The interval in seconds to poll for data points',
127 )
128 parser.add_argument(
129 '--output-file',
130 default='performance.csv',
131 help='The file to output the data collected to',
132 )
133 args = parser.parse_args()
134 return benchmark(args)
135
136
137 if __name__ == '__main__':
138 sys.exit(main())