blob: 84f0586cdf743a8dd6cd6c98f327f84f48392ea6 [file] [log] [blame]
Craig Tillerf7af2a92017-01-31 15:08:31 -08001#!/usr/bin/env python2.7
2# Copyright 2017, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Craig Tiller891e8162017-02-15 23:30:27 -080031import cgi
Craig Tillerf7af2a92017-01-31 15:08:31 -080032import multiprocessing
33import os
34import subprocess
35import sys
Craig Tilleraa64ddf2017-02-08 14:20:08 -080036import argparse
Craig Tillerf7af2a92017-01-31 15:08:31 -080037
Craig Tiller7dc4ea62017-02-02 16:08:05 -080038import python_utils.jobset as jobset
39import python_utils.start_port_server as start_port_server
40
Craig Tillerf7af2a92017-01-31 15:08:31 -080041flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph')
42
Craig Tiller7dc4ea62017-02-02 16:08:05 -080043os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
44if not os.path.exists('reports'):
45 os.makedirs('reports')
46
47port_server_port = 32766
48start_port_server.start_port_server(port_server_port)
49
Craig Tillerf7af2a92017-01-31 15:08:31 -080050def fnize(s):
51 out = ''
52 for c in s:
53 if c in '<>, /':
54 if len(out) and out[-1] == '_': continue
55 out += '_'
56 else:
57 out += c
58 return out
59
Craig Tillerf7af2a92017-01-31 15:08:31 -080060# index html
61index_html = """
62<html>
63<head>
64<title>Microbenchmark Results</title>
65</head>
66<body>
67"""
68
69def heading(name):
70 global index_html
71 index_html += "<h1>%s</h1>\n" % name
72
73def link(txt, tgt):
74 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080075 index_html += "<p><a href=\"%s\">%s</a></p>\n" % (
76 cgi.escape(tgt, quote=True), cgi.escape(txt))
Craig Tillerf7af2a92017-01-31 15:08:31 -080077
Craig Tilleraa64ddf2017-02-08 14:20:08 -080078def text(txt):
79 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080080 index_html += "<p><pre>%s</pre></p>\n" % cgi.escape(txt)
Craig Tiller7dc4ea62017-02-02 16:08:05 -080081
Craig Tilleraa64ddf2017-02-08 14:20:08 -080082def collect_latency(bm_name, args):
83 """generate latency profiles"""
84 benchmarks = []
85 profile_analysis = []
86 cleanup = []
87
Craig Tillerf7af2a92017-01-31 15:08:31 -080088 heading('Latency Profiles: %s' % bm_name)
89 subprocess.check_call(
90 ['make', bm_name,
91 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()])
92 for line in subprocess.check_output(['bins/basicprof/%s' % bm_name,
93 '--benchmark_list_tests']).splitlines():
Craig Tiller39401792017-02-02 12:22:07 -080094 link(line, '%s.txt' % fnize(line))
Craig Tiller7dc4ea62017-02-02 16:08:05 -080095 benchmarks.append(
Craig Tillerece502f2017-02-17 16:20:50 -080096 jobset.JobSpec(['bins/basicprof/%s' % bm_name,
97 '--benchmark_filter=^%s$' % line,
98 '--benchmark_min_time=0.05'],
Craig Tiller7dc4ea62017-02-02 16:08:05 -080099 environ={'LATENCY_TRACE': '%s.trace' % fnize(line)}))
100 profile_analysis.append(
101 jobset.JobSpec([sys.executable,
102 'tools/profiling/latency_profile/profile_analyzer.py',
103 '--source', '%s.trace' % fnize(line), '--fmt', 'simple',
104 '--out', 'reports/%s.txt' % fnize(line)], timeout_seconds=None))
Craig Tiller715e43b2017-02-07 11:13:16 -0800105 cleanup.append(jobset.JobSpec(['rm', '%s.trace' % fnize(line)]))
Craig Tiller360c0d52017-02-08 13:36:44 -0800106 # periodically flush out the list of jobs: profile_analysis jobs at least
107 # consume upwards of five gigabytes of ram in some cases, and so analysing
108 # hundreds of them at once is impractical -- but we want at least some
109 # concurrency or the work takes too long
Craig Tillerece502f2017-02-17 16:20:50 -0800110 if len(benchmarks) >= min(16, multiprocessing.cpu_count()):
Craig Tiller360c0d52017-02-08 13:36:44 -0800111 # run up to half the cpu count: each benchmark can use up to two cores
112 # (one for the microbenchmark, one for the data flush)
Craig Tiller2ef0d542017-02-08 13:53:18 -0800113 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2),
Craig Tiller6911d082017-02-07 10:30:44 -0800114 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
115 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
116 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
117 benchmarks = []
118 profile_analysis = []
119 cleanup = []
Craig Tiller360c0d52017-02-08 13:36:44 -0800120 # run the remaining benchmarks that weren't flushed
Craig Tiller6911d082017-02-07 10:30:44 -0800121 if len(benchmarks):
Craig Tiller2ef0d542017-02-08 13:53:18 -0800122 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2),
Craig Tiller6911d082017-02-07 10:30:44 -0800123 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
124 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
125 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800126
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800127def collect_perf(bm_name, args):
128 """generate flamegraphs"""
Craig Tillerf7af2a92017-01-31 15:08:31 -0800129 heading('Flamegraphs: %s' % bm_name)
130 subprocess.check_call(
131 ['make', bm_name,
132 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()])
Craig Tiller6ad00722017-02-15 09:14:24 -0800133 benchmarks = []
134 profile_analysis = []
135 cleanup = []
Craig Tillerf7af2a92017-01-31 15:08:31 -0800136 for line in subprocess.check_output(['bins/mutrace/%s' % bm_name,
137 '--benchmark_list_tests']).splitlines():
Craig Tiller5a8c5862017-02-15 07:59:05 -0800138 link(line, '%s.svg' % fnize(line))
Craig Tiller6ad00722017-02-15 09:14:24 -0800139 benchmarks.append(
140 jobset.JobSpec(['perf', 'record', '-o', '%s-perf.data' % fnize(line),
Craig Tillerc2c0c6f2017-02-15 11:27:37 -0800141 '-g', '-F', '997',
Craig Tiller6ad00722017-02-15 09:14:24 -0800142 'bins/mutrace/%s' % bm_name,
143 '--benchmark_filter=^%s$' % line,
144 '--benchmark_min_time=10']))
145 profile_analysis.append(
146 jobset.JobSpec(['tools/run_tests/performance/process_local_perf_flamegraphs.sh'],
147 environ = {
148 'PERF_BASE_NAME': fnize(line),
149 'OUTPUT_DIR': 'reports',
150 'OUTPUT_FILENAME': fnize(line),
151 }))
152 cleanup.append(jobset.JobSpec(['rm', '%s-perf.data' % fnize(line)]))
153 cleanup.append(jobset.JobSpec(['rm', '%s-out.perf' % fnize(line)]))
154 # periodically flush out the list of jobs: temporary space required for this
155 # processing is large
156 if len(benchmarks) >= 20:
157 # run up to half the cpu count: each benchmark can use up to two cores
158 # (one for the microbenchmark, one for the data flush)
Craig Tillerab951942017-02-15 11:26:54 -0800159 jobset.run(benchmarks, maxjobs=1,
Craig Tiller6ad00722017-02-15 09:14:24 -0800160 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
161 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
162 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
163 benchmarks = []
164 profile_analysis = []
165 cleanup = []
166 # run the remaining benchmarks that weren't flushed
167 if len(benchmarks):
Craig Tillerab951942017-02-15 11:26:54 -0800168 jobset.run(benchmarks, maxjobs=1,
Craig Tiller6ad00722017-02-15 09:14:24 -0800169 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
170 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
171 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800172
Craig Tillerff84b362017-03-01 14:11:15 -0800173def run_summary(bm_name, cfg, base_json_name):
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800174 subprocess.check_call(
175 ['make', bm_name,
Craig Tiller541b87e2017-03-01 08:42:52 -0800176 'CONFIG=%s' % cfg, '-j', '%d' % multiprocessing.cpu_count()])
177 cmd = ['bins/%s/%s' % (cfg, bm_name),
Craig Tillerff84b362017-03-01 14:11:15 -0800178 '--benchmark_out=%s.%s.json' % (base_json_name, cfg),
Craig Tillerd9bc2102017-02-15 08:24:55 -0800179 '--benchmark_out_format=json']
180 if args.summary_time is not None:
181 cmd += ['--benchmark_min_time=%d' % args.summary_time]
Craig Tiller541b87e2017-03-01 08:42:52 -0800182 return subprocess.check_output(cmd)
183
184def collect_summary(bm_name, args):
185 heading('Summary: %s [no counters]' % bm_name)
Craig Tillerff84b362017-03-01 14:11:15 -0800186 text(run_summary(bm_name, 'opt', 'out'))
Craig Tiller541b87e2017-03-01 08:42:52 -0800187 heading('Summary: %s [with counters]' % bm_name)
Craig Tillerff84b362017-03-01 14:11:15 -0800188 text(run_summary(bm_name, 'counters', 'out'))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800189 if args.bigquery_upload:
Craig Tillerd12731e2017-02-13 12:41:27 -0800190 with open('out.csv', 'w') as f:
Craig Tiller541b87e2017-03-01 08:42:52 -0800191 f.write(subprocess.check_output(['tools/profiling/microbenchmarks/bm2bq.py', 'out.counters.json', 'out.opt.json']))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800192 subprocess.check_call(['bq', 'load', 'microbenchmarks.microbenchmarks', 'out.csv'])
193
194collectors = {
195 'latency': collect_latency,
196 'perf': collect_perf,
197 'summary': collect_summary,
198}
199
200argp = argparse.ArgumentParser(description='Collect data from microbenchmarks')
201argp.add_argument('-c', '--collect',
202 choices=sorted(collectors.keys()),
203 nargs='+',
204 default=sorted(collectors.keys()),
205 help='Which collectors should be run against each benchmark')
206argp.add_argument('-b', '--benchmarks',
Craig Tiller523d54b2017-02-23 08:52:38 -0800207 default=['bm_fullstack',
208 'bm_closure',
209 'bm_cq',
210 'bm_call_create',
211 'bm_error',
Craig Tiller510f38a2017-02-24 17:00:19 -0800212 'bm_chttp2_hpack',
213 'bm_metadata'],
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800214 nargs='+',
215 type=str,
216 help='Which microbenchmarks should be run')
Craig Tillerd753f452017-03-01 14:00:35 -0800217argp.add_argument('--diff_perf',
218 default=None,
219 type=str,
220 help='Diff microbenchmarks against this git revision')
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800221argp.add_argument('--bigquery_upload',
222 default=False,
223 action='store_const',
224 const=True,
225 help='Upload results from summary collection to bigquery')
Craig Tillerd9bc2102017-02-15 08:24:55 -0800226argp.add_argument('--summary_time',
227 default=None,
228 type=int,
229 help='Minimum time to run benchmarks for the summary collection')
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800230args = argp.parse_args()
231
232for bm_name in args.benchmarks:
233 for collect in args.collect:
234 collectors[collect](bm_name, args)
Craig Tillerd753f452017-03-01 14:00:35 -0800235if args.diff_perf:
Craig Tillerff84b362017-03-01 14:11:15 -0800236 for bm_name in args.benchmarks:
237 run_summary(bm_name, 'opt', '%s.new' % bm_name)
238 where_am_i = submodule.check_call(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
239 submodule.check_call(['git', 'checkout', args.diff_perf])
240 comparables = []
241 try:
242 for bm_name in args.benchmarks:
243 try:
244 run_summary(bm_name, 'opt', '%s.old' % bm_name)
245 comparables.append(bm_name)
246 except subprocess.CalledProcessError, e:
247 pass
248 finally:
249 submodule.check_call(['git', 'checkout', where_am_i])
250 for bm_name in comparables:
251 submodule.check_call(['third_party/benchmark/tools/compare_bench.py',
252 '%s.new.opt.json' % bm_name,
253 '%s.old.opt.json' % bm_name])
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800254
Craig Tillerf7af2a92017-01-31 15:08:31 -0800255index_html += "</body>\n</html>\n"
256with open('reports/index.html', 'w') as f:
Craig Tiller360c0d52017-02-08 13:36:44 -0800257 f.write(index_html)