blob: 9bcba598b836f539422a370cd575a4a6f0e78296 [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(
96 jobset.JobSpec(['bins/basicprof/%s' % bm_name, '--benchmark_filter=^%s$' % line],
97 environ={'LATENCY_TRACE': '%s.trace' % fnize(line)}))
98 profile_analysis.append(
99 jobset.JobSpec([sys.executable,
100 'tools/profiling/latency_profile/profile_analyzer.py',
101 '--source', '%s.trace' % fnize(line), '--fmt', 'simple',
102 '--out', 'reports/%s.txt' % fnize(line)], timeout_seconds=None))
Craig Tiller715e43b2017-02-07 11:13:16 -0800103 cleanup.append(jobset.JobSpec(['rm', '%s.trace' % fnize(line)]))
Craig Tiller360c0d52017-02-08 13:36:44 -0800104 # periodically flush out the list of jobs: profile_analysis jobs at least
105 # consume upwards of five gigabytes of ram in some cases, and so analysing
106 # hundreds of them at once is impractical -- but we want at least some
107 # concurrency or the work takes too long
Craig Tillerf74d1722017-02-08 11:38:09 -0800108 if len(benchmarks) >= min(4, multiprocessing.cpu_count()):
Craig Tiller360c0d52017-02-08 13:36:44 -0800109 # run up to half the cpu count: each benchmark can use up to two cores
110 # (one for the microbenchmark, one for the data flush)
Craig Tiller2ef0d542017-02-08 13:53:18 -0800111 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2),
Craig Tiller6911d082017-02-07 10:30:44 -0800112 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
113 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
114 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
115 benchmarks = []
116 profile_analysis = []
117 cleanup = []
Craig Tiller360c0d52017-02-08 13:36:44 -0800118 # run the remaining benchmarks that weren't flushed
Craig Tiller6911d082017-02-07 10:30:44 -0800119 if len(benchmarks):
Craig Tiller2ef0d542017-02-08 13:53:18 -0800120 jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2),
Craig Tiller6911d082017-02-07 10:30:44 -0800121 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
122 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
123 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800124
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800125def collect_perf(bm_name, args):
126 """generate flamegraphs"""
Craig Tillerf7af2a92017-01-31 15:08:31 -0800127 heading('Flamegraphs: %s' % bm_name)
128 subprocess.check_call(
129 ['make', bm_name,
130 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()])
Craig Tiller6ad00722017-02-15 09:14:24 -0800131 benchmarks = []
132 profile_analysis = []
133 cleanup = []
Craig Tillerf7af2a92017-01-31 15:08:31 -0800134 for line in subprocess.check_output(['bins/mutrace/%s' % bm_name,
135 '--benchmark_list_tests']).splitlines():
Craig Tiller5a8c5862017-02-15 07:59:05 -0800136 link(line, '%s.svg' % fnize(line))
Craig Tiller6ad00722017-02-15 09:14:24 -0800137 benchmarks.append(
138 jobset.JobSpec(['perf', 'record', '-o', '%s-perf.data' % fnize(line),
Craig Tillerc2c0c6f2017-02-15 11:27:37 -0800139 '-g', '-F', '997',
Craig Tiller6ad00722017-02-15 09:14:24 -0800140 'bins/mutrace/%s' % bm_name,
141 '--benchmark_filter=^%s$' % line,
142 '--benchmark_min_time=10']))
143 profile_analysis.append(
144 jobset.JobSpec(['tools/run_tests/performance/process_local_perf_flamegraphs.sh'],
145 environ = {
146 'PERF_BASE_NAME': fnize(line),
147 'OUTPUT_DIR': 'reports',
148 'OUTPUT_FILENAME': fnize(line),
149 }))
150 cleanup.append(jobset.JobSpec(['rm', '%s-perf.data' % fnize(line)]))
151 cleanup.append(jobset.JobSpec(['rm', '%s-out.perf' % fnize(line)]))
152 # periodically flush out the list of jobs: temporary space required for this
153 # processing is large
154 if len(benchmarks) >= 20:
155 # run up to half the cpu count: each benchmark can use up to two cores
156 # (one for the microbenchmark, one for the data flush)
Craig Tillerab951942017-02-15 11:26:54 -0800157 jobset.run(benchmarks, maxjobs=1,
Craig Tiller6ad00722017-02-15 09:14:24 -0800158 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
159 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
160 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
161 benchmarks = []
162 profile_analysis = []
163 cleanup = []
164 # run the remaining benchmarks that weren't flushed
165 if len(benchmarks):
Craig Tillerab951942017-02-15 11:26:54 -0800166 jobset.run(benchmarks, maxjobs=1,
Craig Tiller6ad00722017-02-15 09:14:24 -0800167 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
168 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
169 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800170
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800171def collect_summary(bm_name, args):
172 heading('Summary: %s' % bm_name)
173 subprocess.check_call(
174 ['make', bm_name,
175 'CONFIG=counters', '-j', '%d' % multiprocessing.cpu_count()])
176 text(subprocess.check_output(['bins/counters/%s' % bm_name,
177 '--benchmark_out=out.json',
178 '--benchmark_out_format=json']))
179 if args.bigquery_upload:
Craig Tillerd12731e2017-02-13 12:41:27 -0800180 with open('out.csv', 'w') as f:
Craig Tillerfc4f72a2017-02-08 15:57:03 -0800181 f.write(subprocess.check_output(['tools/profiling/microbenchmarks/bm2bq.py', 'out.json']))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800182 subprocess.check_call(['bq', 'load', 'microbenchmarks.microbenchmarks', 'out.csv'])
183
184collectors = {
185 'latency': collect_latency,
186 'perf': collect_perf,
187 'summary': collect_summary,
188}
189
190argp = argparse.ArgumentParser(description='Collect data from microbenchmarks')
191argp.add_argument('-c', '--collect',
192 choices=sorted(collectors.keys()),
193 nargs='+',
194 default=sorted(collectors.keys()),
195 help='Which collectors should be run against each benchmark')
196argp.add_argument('-b', '--benchmarks',
197 default=['bm_fullstack'],
198 nargs='+',
199 type=str,
200 help='Which microbenchmarks should be run')
201argp.add_argument('--bigquery_upload',
202 default=False,
203 action='store_const',
204 const=True,
205 help='Upload results from summary collection to bigquery')
206args = argp.parse_args()
207
208for bm_name in args.benchmarks:
209 for collect in args.collect:
210 collectors[collect](bm_name, args)
211
Craig Tillerf7af2a92017-01-31 15:08:31 -0800212index_html += "</body>\n</html>\n"
213with open('reports/index.html', 'w') as f:
Craig Tiller360c0d52017-02-08 13:36:44 -0800214 f.write(index_html)