blob: 3eecd8eff422c7afd3a0fec2a9cc9502a69dc8d0 [file] [log] [blame]
Siddharth Shukla8e64d902017-03-12 19:50:18 +01001#!/usr/bin/env python
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2017 gRPC authors.
Craig Tillerf7af2a92017-01-31 15:08:31 -08003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
Craig Tillerf7af2a92017-01-31 15:08:31 -08007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Craig Tillerf7af2a92017-01-31 15:08:31 -08009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Craig Tillerf7af2a92017-01-31 15:08:31 -080015
Craig Tiller891e8162017-02-15 23:30:27 -080016import cgi
Craig Tillerf7af2a92017-01-31 15:08:31 -080017import multiprocessing
18import os
19import subprocess
20import sys
Craig Tilleraa64ddf2017-02-08 14:20:08 -080021import argparse
Craig Tillerf7af2a92017-01-31 15:08:31 -080022
Craig Tiller7dc4ea62017-02-02 16:08:05 -080023import python_utils.jobset as jobset
24import python_utils.start_port_server as start_port_server
25
Matt Kwongd0ee10d2017-03-10 22:37:52 -080026_AVAILABLE_BENCHMARK_TESTS = ['bm_fullstack_unary_ping_pong',
27 'bm_fullstack_streaming_ping_pong',
28 'bm_fullstack_streaming_pump',
29 'bm_closure',
30 'bm_cq',
31 'bm_call_create',
32 'bm_error',
33 'bm_chttp2_hpack',
Matt Kwong063adf32017-03-22 12:48:34 -070034 'bm_chttp2_transport',
35 'bm_pollset',
Matt Kwongd0ee10d2017-03-10 22:37:52 -080036 'bm_metadata',
37 'bm_fullstack_trickle']
38
Craig Tillerf7af2a92017-01-31 15:08:31 -080039flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph')
40
Craig Tiller7dc4ea62017-02-02 16:08:05 -080041os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
42if not os.path.exists('reports'):
43 os.makedirs('reports')
44
Craig Tillercba864b2017-02-17 10:27:56 -080045start_port_server.start_port_server()
Craig Tiller7dc4ea62017-02-02 16:08:05 -080046
Craig Tillerf7af2a92017-01-31 15:08:31 -080047def fnize(s):
48 out = ''
49 for c in s:
50 if c in '<>, /':
51 if len(out) and out[-1] == '_': continue
52 out += '_'
53 else:
54 out += c
55 return out
56
Craig Tillerf7af2a92017-01-31 15:08:31 -080057# index html
58index_html = """
59<html>
60<head>
61<title>Microbenchmark Results</title>
62</head>
63<body>
64"""
65
66def heading(name):
67 global index_html
68 index_html += "<h1>%s</h1>\n" % name
69
70def link(txt, tgt):
71 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080072 index_html += "<p><a href=\"%s\">%s</a></p>\n" % (
73 cgi.escape(tgt, quote=True), cgi.escape(txt))
Craig Tillerf7af2a92017-01-31 15:08:31 -080074
Craig Tilleraa64ddf2017-02-08 14:20:08 -080075def text(txt):
76 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080077 index_html += "<p><pre>%s</pre></p>\n" % cgi.escape(txt)
Craig Tiller7dc4ea62017-02-02 16:08:05 -080078
Craig Tilleraa64ddf2017-02-08 14:20:08 -080079def collect_latency(bm_name, args):
80 """generate latency profiles"""
81 benchmarks = []
82 profile_analysis = []
83 cleanup = []
84
Craig Tillerf7af2a92017-01-31 15:08:31 -080085 heading('Latency Profiles: %s' % bm_name)
86 subprocess.check_call(
87 ['make', bm_name,
88 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()])
89 for line in subprocess.check_output(['bins/basicprof/%s' % bm_name,
90 '--benchmark_list_tests']).splitlines():
Craig Tiller39401792017-02-02 12:22:07 -080091 link(line, '%s.txt' % fnize(line))
Craig Tiller7dc4ea62017-02-02 16:08:05 -080092 benchmarks.append(
Craig Tillerece502f2017-02-17 16:20:50 -080093 jobset.JobSpec(['bins/basicprof/%s' % bm_name,
94 '--benchmark_filter=^%s$' % line,
95 '--benchmark_min_time=0.05'],
Craig Tiller7dc4ea62017-02-02 16:08:05 -080096 environ={'LATENCY_TRACE': '%s.trace' % fnize(line)}))
97 profile_analysis.append(
98 jobset.JobSpec([sys.executable,
99 'tools/profiling/latency_profile/profile_analyzer.py',
100 '--source', '%s.trace' % fnize(line), '--fmt', 'simple',
101 '--out', 'reports/%s.txt' % fnize(line)], timeout_seconds=None))
Craig Tiller715e43b2017-02-07 11:13:16 -0800102 cleanup.append(jobset.JobSpec(['rm', '%s.trace' % fnize(line)]))
Craig Tiller360c0d52017-02-08 13:36:44 -0800103 # periodically flush out the list of jobs: profile_analysis jobs at least
104 # consume upwards of five gigabytes of ram in some cases, and so analysing
105 # hundreds of them at once is impractical -- but we want at least some
106 # concurrency or the work takes too long
Craig Tillerece502f2017-02-17 16:20:50 -0800107 if len(benchmarks) >= min(16, multiprocessing.cpu_count()):
Craig Tiller360c0d52017-02-08 13:36:44 -0800108 # run up to half the cpu count: each benchmark can use up to two cores
109 # (one for the microbenchmark, one for the data flush)
Craig Tillercba864b2017-02-17 10:27:56 -0800110 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2))
Craig Tiller6911d082017-02-07 10:30:44 -0800111 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
112 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
113 benchmarks = []
114 profile_analysis = []
115 cleanup = []
Craig Tiller360c0d52017-02-08 13:36:44 -0800116 # run the remaining benchmarks that weren't flushed
Craig Tiller6911d082017-02-07 10:30:44 -0800117 if len(benchmarks):
Craig Tillercba864b2017-02-17 10:27:56 -0800118 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2))
Craig Tiller6911d082017-02-07 10:30:44 -0800119 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
120 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800121
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800122def collect_perf(bm_name, args):
123 """generate flamegraphs"""
Craig Tillerf7af2a92017-01-31 15:08:31 -0800124 heading('Flamegraphs: %s' % bm_name)
125 subprocess.check_call(
126 ['make', bm_name,
127 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()])
Craig Tiller6ad00722017-02-15 09:14:24 -0800128 benchmarks = []
129 profile_analysis = []
130 cleanup = []
Craig Tillerf7af2a92017-01-31 15:08:31 -0800131 for line in subprocess.check_output(['bins/mutrace/%s' % bm_name,
132 '--benchmark_list_tests']).splitlines():
Craig Tiller5a8c5862017-02-15 07:59:05 -0800133 link(line, '%s.svg' % fnize(line))
Craig Tiller6ad00722017-02-15 09:14:24 -0800134 benchmarks.append(
135 jobset.JobSpec(['perf', 'record', '-o', '%s-perf.data' % fnize(line),
Craig Tillerc2c0c6f2017-02-15 11:27:37 -0800136 '-g', '-F', '997',
Craig Tiller6ad00722017-02-15 09:14:24 -0800137 'bins/mutrace/%s' % bm_name,
138 '--benchmark_filter=^%s$' % line,
139 '--benchmark_min_time=10']))
140 profile_analysis.append(
141 jobset.JobSpec(['tools/run_tests/performance/process_local_perf_flamegraphs.sh'],
142 environ = {
143 'PERF_BASE_NAME': fnize(line),
144 'OUTPUT_DIR': 'reports',
145 'OUTPUT_FILENAME': fnize(line),
146 }))
147 cleanup.append(jobset.JobSpec(['rm', '%s-perf.data' % fnize(line)]))
148 cleanup.append(jobset.JobSpec(['rm', '%s-out.perf' % fnize(line)]))
149 # periodically flush out the list of jobs: temporary space required for this
150 # processing is large
151 if len(benchmarks) >= 20:
152 # run up to half the cpu count: each benchmark can use up to two cores
153 # (one for the microbenchmark, one for the data flush)
Craig Tillercba864b2017-02-17 10:27:56 -0800154 jobset.run(benchmarks, maxjobs=1)
Craig Tiller6ad00722017-02-15 09:14:24 -0800155 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
156 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
157 benchmarks = []
158 profile_analysis = []
159 cleanup = []
160 # run the remaining benchmarks that weren't flushed
161 if len(benchmarks):
Craig Tillercba864b2017-02-17 10:27:56 -0800162 jobset.run(benchmarks, maxjobs=1)
Craig Tiller6ad00722017-02-15 09:14:24 -0800163 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
164 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800165
Craig Tillerff84b362017-03-01 14:11:15 -0800166def run_summary(bm_name, cfg, base_json_name):
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800167 subprocess.check_call(
168 ['make', bm_name,
Craig Tiller541b87e2017-03-01 08:42:52 -0800169 'CONFIG=%s' % cfg, '-j', '%d' % multiprocessing.cpu_count()])
170 cmd = ['bins/%s/%s' % (cfg, bm_name),
Craig Tillerff84b362017-03-01 14:11:15 -0800171 '--benchmark_out=%s.%s.json' % (base_json_name, cfg),
Craig Tillerd9bc2102017-02-15 08:24:55 -0800172 '--benchmark_out_format=json']
173 if args.summary_time is not None:
174 cmd += ['--benchmark_min_time=%d' % args.summary_time]
Craig Tiller541b87e2017-03-01 08:42:52 -0800175 return subprocess.check_output(cmd)
176
177def collect_summary(bm_name, args):
178 heading('Summary: %s [no counters]' % bm_name)
Craig Tiller26995eb2017-03-08 13:10:28 -0800179 text(run_summary(bm_name, 'opt', bm_name))
Craig Tiller541b87e2017-03-01 08:42:52 -0800180 heading('Summary: %s [with counters]' % bm_name)
Craig Tiller26995eb2017-03-08 13:10:28 -0800181 text(run_summary(bm_name, 'counters', bm_name))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800182 if args.bigquery_upload:
Craig Tiller26995eb2017-03-08 13:10:28 -0800183 with open('%s.csv' % bm_name, 'w') as f:
184 f.write(subprocess.check_output(['tools/profiling/microbenchmarks/bm2bq.py',
185 '%s.counters.json' % bm_name,
186 '%s.opt.json' % bm_name]))
187 subprocess.check_call(['bq', 'load', 'microbenchmarks.microbenchmarks', '%s.csv' % bm_name])
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800188
189collectors = {
190 'latency': collect_latency,
191 'perf': collect_perf,
192 'summary': collect_summary,
193}
194
195argp = argparse.ArgumentParser(description='Collect data from microbenchmarks')
196argp.add_argument('-c', '--collect',
197 choices=sorted(collectors.keys()),
Craig Tiller5ef448d2017-03-01 14:12:47 -0800198 nargs='*',
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800199 default=sorted(collectors.keys()),
200 help='Which collectors should be run against each benchmark')
201argp.add_argument('-b', '--benchmarks',
Matt Kwongd0ee10d2017-03-10 22:37:52 -0800202 choices=_AVAILABLE_BENCHMARK_TESTS,
203 default=_AVAILABLE_BENCHMARK_TESTS,
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800204 nargs='+',
205 type=str,
206 help='Which microbenchmarks should be run')
207argp.add_argument('--bigquery_upload',
208 default=False,
209 action='store_const',
210 const=True,
211 help='Upload results from summary collection to bigquery')
Craig Tillerd9bc2102017-02-15 08:24:55 -0800212argp.add_argument('--summary_time',
213 default=None,
214 type=int,
215 help='Minimum time to run benchmarks for the summary collection')
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800216args = argp.parse_args()
217
Craig Tillerb4328852017-03-08 13:08:40 -0800218try:
Craig Tiller47f37f32017-03-10 10:44:56 -0800219 for collect in args.collect:
220 for bm_name in args.benchmarks:
Craig Tillerb4328852017-03-08 13:08:40 -0800221 collectors[collect](bm_name, args)
Craig Tillerb4328852017-03-08 13:08:40 -0800222finally:
Matt Kwongaff1c052017-03-09 15:08:01 -0800223 if not os.path.exists('reports'):
224 os.makedirs('reports')
Craig Tillerb4328852017-03-08 13:08:40 -0800225 index_html += "</body>\n</html>\n"
226 with open('reports/index.html', 'w') as f:
227 f.write(index_html)