blob: 17b156c78f9828625441757eda92801d5c72de74 [file] [log] [blame]
Siddharth Shukla8e64d902017-03-12 19:50:18 +01001#!/usr/bin/env python
Craig Tillerf7af2a92017-01-31 15:08:31 -08002# 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
Matt Kwongd0ee10d2017-03-10 22:37:52 -080041_AVAILABLE_BENCHMARK_TESTS = ['bm_fullstack_unary_ping_pong',
42 'bm_fullstack_streaming_ping_pong',
43 'bm_fullstack_streaming_pump',
44 'bm_closure',
45 'bm_cq',
46 'bm_call_create',
47 'bm_error',
48 'bm_chttp2_hpack',
Matt Kwong063adf32017-03-22 12:48:34 -070049 'bm_chttp2_transport',
50 'bm_pollset',
Matt Kwongd0ee10d2017-03-10 22:37:52 -080051 'bm_metadata',
52 'bm_fullstack_trickle']
53
Craig Tillerf7af2a92017-01-31 15:08:31 -080054flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph')
55
Craig Tiller7dc4ea62017-02-02 16:08:05 -080056os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
57if not os.path.exists('reports'):
58 os.makedirs('reports')
59
Craig Tillercba864b2017-02-17 10:27:56 -080060start_port_server.start_port_server()
Craig Tiller7dc4ea62017-02-02 16:08:05 -080061
Craig Tillerf7af2a92017-01-31 15:08:31 -080062def fnize(s):
63 out = ''
64 for c in s:
65 if c in '<>, /':
66 if len(out) and out[-1] == '_': continue
67 out += '_'
68 else:
69 out += c
70 return out
71
Craig Tillerf7af2a92017-01-31 15:08:31 -080072# index html
73index_html = """
74<html>
75<head>
76<title>Microbenchmark Results</title>
77</head>
78<body>
79"""
80
81def heading(name):
82 global index_html
83 index_html += "<h1>%s</h1>\n" % name
84
85def link(txt, tgt):
86 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080087 index_html += "<p><a href=\"%s\">%s</a></p>\n" % (
88 cgi.escape(tgt, quote=True), cgi.escape(txt))
Craig Tillerf7af2a92017-01-31 15:08:31 -080089
Craig Tilleraa64ddf2017-02-08 14:20:08 -080090def text(txt):
91 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080092 index_html += "<p><pre>%s</pre></p>\n" % cgi.escape(txt)
Craig Tiller7dc4ea62017-02-02 16:08:05 -080093
Craig Tilleraa64ddf2017-02-08 14:20:08 -080094def collect_latency(bm_name, args):
95 """generate latency profiles"""
96 benchmarks = []
97 profile_analysis = []
98 cleanup = []
99
Craig Tillerf7af2a92017-01-31 15:08:31 -0800100 heading('Latency Profiles: %s' % bm_name)
101 subprocess.check_call(
102 ['make', bm_name,
103 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()])
104 for line in subprocess.check_output(['bins/basicprof/%s' % bm_name,
105 '--benchmark_list_tests']).splitlines():
Craig Tiller39401792017-02-02 12:22:07 -0800106 link(line, '%s.txt' % fnize(line))
Craig Tiller7dc4ea62017-02-02 16:08:05 -0800107 benchmarks.append(
Craig Tillerece502f2017-02-17 16:20:50 -0800108 jobset.JobSpec(['bins/basicprof/%s' % bm_name,
109 '--benchmark_filter=^%s$' % line,
110 '--benchmark_min_time=0.05'],
Craig Tiller7dc4ea62017-02-02 16:08:05 -0800111 environ={'LATENCY_TRACE': '%s.trace' % fnize(line)}))
112 profile_analysis.append(
113 jobset.JobSpec([sys.executable,
114 'tools/profiling/latency_profile/profile_analyzer.py',
115 '--source', '%s.trace' % fnize(line), '--fmt', 'simple',
116 '--out', 'reports/%s.txt' % fnize(line)], timeout_seconds=None))
Craig Tiller715e43b2017-02-07 11:13:16 -0800117 cleanup.append(jobset.JobSpec(['rm', '%s.trace' % fnize(line)]))
Craig Tiller360c0d52017-02-08 13:36:44 -0800118 # periodically flush out the list of jobs: profile_analysis jobs at least
119 # consume upwards of five gigabytes of ram in some cases, and so analysing
120 # hundreds of them at once is impractical -- but we want at least some
121 # concurrency or the work takes too long
Craig Tillerece502f2017-02-17 16:20:50 -0800122 if len(benchmarks) >= min(16, multiprocessing.cpu_count()):
Craig Tiller360c0d52017-02-08 13:36:44 -0800123 # run up to half the cpu count: each benchmark can use up to two cores
124 # (one for the microbenchmark, one for the data flush)
Craig Tillercba864b2017-02-17 10:27:56 -0800125 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2))
Craig Tiller6911d082017-02-07 10:30:44 -0800126 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
127 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
128 benchmarks = []
129 profile_analysis = []
130 cleanup = []
Craig Tiller360c0d52017-02-08 13:36:44 -0800131 # run the remaining benchmarks that weren't flushed
Craig Tiller6911d082017-02-07 10:30:44 -0800132 if len(benchmarks):
Craig Tillercba864b2017-02-17 10:27:56 -0800133 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2))
Craig Tiller6911d082017-02-07 10:30:44 -0800134 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
135 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800136
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800137def collect_perf(bm_name, args):
138 """generate flamegraphs"""
Craig Tillerf7af2a92017-01-31 15:08:31 -0800139 heading('Flamegraphs: %s' % bm_name)
140 subprocess.check_call(
141 ['make', bm_name,
142 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()])
Craig Tiller6ad00722017-02-15 09:14:24 -0800143 benchmarks = []
144 profile_analysis = []
145 cleanup = []
Craig Tillerf7af2a92017-01-31 15:08:31 -0800146 for line in subprocess.check_output(['bins/mutrace/%s' % bm_name,
147 '--benchmark_list_tests']).splitlines():
Craig Tiller5a8c5862017-02-15 07:59:05 -0800148 link(line, '%s.svg' % fnize(line))
Craig Tiller6ad00722017-02-15 09:14:24 -0800149 benchmarks.append(
150 jobset.JobSpec(['perf', 'record', '-o', '%s-perf.data' % fnize(line),
Craig Tillerc2c0c6f2017-02-15 11:27:37 -0800151 '-g', '-F', '997',
Craig Tiller6ad00722017-02-15 09:14:24 -0800152 'bins/mutrace/%s' % bm_name,
153 '--benchmark_filter=^%s$' % line,
154 '--benchmark_min_time=10']))
155 profile_analysis.append(
156 jobset.JobSpec(['tools/run_tests/performance/process_local_perf_flamegraphs.sh'],
157 environ = {
158 'PERF_BASE_NAME': fnize(line),
159 'OUTPUT_DIR': 'reports',
160 'OUTPUT_FILENAME': fnize(line),
161 }))
162 cleanup.append(jobset.JobSpec(['rm', '%s-perf.data' % fnize(line)]))
163 cleanup.append(jobset.JobSpec(['rm', '%s-out.perf' % fnize(line)]))
164 # periodically flush out the list of jobs: temporary space required for this
165 # processing is large
166 if len(benchmarks) >= 20:
167 # run up to half the cpu count: each benchmark can use up to two cores
168 # (one for the microbenchmark, one for the data flush)
Craig Tillercba864b2017-02-17 10:27:56 -0800169 jobset.run(benchmarks, maxjobs=1)
Craig Tiller6ad00722017-02-15 09:14:24 -0800170 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
171 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
172 benchmarks = []
173 profile_analysis = []
174 cleanup = []
175 # run the remaining benchmarks that weren't flushed
176 if len(benchmarks):
Craig Tillercba864b2017-02-17 10:27:56 -0800177 jobset.run(benchmarks, maxjobs=1)
Craig Tiller6ad00722017-02-15 09:14:24 -0800178 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
179 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800180
Craig Tillerff84b362017-03-01 14:11:15 -0800181def run_summary(bm_name, cfg, base_json_name):
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800182 subprocess.check_call(
183 ['make', bm_name,
Craig Tiller541b87e2017-03-01 08:42:52 -0800184 'CONFIG=%s' % cfg, '-j', '%d' % multiprocessing.cpu_count()])
185 cmd = ['bins/%s/%s' % (cfg, bm_name),
Craig Tillerff84b362017-03-01 14:11:15 -0800186 '--benchmark_out=%s.%s.json' % (base_json_name, cfg),
Craig Tillerd9bc2102017-02-15 08:24:55 -0800187 '--benchmark_out_format=json']
188 if args.summary_time is not None:
189 cmd += ['--benchmark_min_time=%d' % args.summary_time]
Craig Tiller541b87e2017-03-01 08:42:52 -0800190 return subprocess.check_output(cmd)
191
192def collect_summary(bm_name, args):
193 heading('Summary: %s [no counters]' % bm_name)
Craig Tiller26995eb2017-03-08 13:10:28 -0800194 text(run_summary(bm_name, 'opt', bm_name))
Craig Tiller541b87e2017-03-01 08:42:52 -0800195 heading('Summary: %s [with counters]' % bm_name)
Craig Tiller26995eb2017-03-08 13:10:28 -0800196 text(run_summary(bm_name, 'counters', bm_name))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800197 if args.bigquery_upload:
Craig Tiller26995eb2017-03-08 13:10:28 -0800198 with open('%s.csv' % bm_name, 'w') as f:
199 f.write(subprocess.check_output(['tools/profiling/microbenchmarks/bm2bq.py',
200 '%s.counters.json' % bm_name,
201 '%s.opt.json' % bm_name]))
202 subprocess.check_call(['bq', 'load', 'microbenchmarks.microbenchmarks', '%s.csv' % bm_name])
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800203
204collectors = {
205 'latency': collect_latency,
206 'perf': collect_perf,
207 'summary': collect_summary,
208}
209
210argp = argparse.ArgumentParser(description='Collect data from microbenchmarks')
211argp.add_argument('-c', '--collect',
212 choices=sorted(collectors.keys()),
Craig Tiller5ef448d2017-03-01 14:12:47 -0800213 nargs='*',
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800214 default=sorted(collectors.keys()),
215 help='Which collectors should be run against each benchmark')
216argp.add_argument('-b', '--benchmarks',
Matt Kwongd0ee10d2017-03-10 22:37:52 -0800217 choices=_AVAILABLE_BENCHMARK_TESTS,
218 default=_AVAILABLE_BENCHMARK_TESTS,
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800219 nargs='+',
220 type=str,
221 help='Which microbenchmarks should be run')
222argp.add_argument('--bigquery_upload',
223 default=False,
224 action='store_const',
225 const=True,
226 help='Upload results from summary collection to bigquery')
Craig Tillerd9bc2102017-02-15 08:24:55 -0800227argp.add_argument('--summary_time',
228 default=None,
229 type=int,
230 help='Minimum time to run benchmarks for the summary collection')
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800231args = argp.parse_args()
232
Craig Tillerb4328852017-03-08 13:08:40 -0800233try:
Craig Tiller47f37f32017-03-10 10:44:56 -0800234 for collect in args.collect:
235 for bm_name in args.benchmarks:
Craig Tillerb4328852017-03-08 13:08:40 -0800236 collectors[collect](bm_name, args)
Craig Tillerb4328852017-03-08 13:08:40 -0800237finally:
Matt Kwongaff1c052017-03-09 15:08:01 -0800238 if not os.path.exists('reports'):
239 os.makedirs('reports')
Craig Tillerb4328852017-03-08 13:08:40 -0800240 index_html += "</body>\n</html>\n"
241 with open('reports/index.html', 'w') as f:
242 f.write(index_html)