blob: a67c8468d9f6e1c6493fc1de06cb0da623442b2e [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
Craig Tillercba864b2017-02-17 10:27:56 -080047start_port_server.start_port_server()
Craig Tiller7dc4ea62017-02-02 16:08:05 -080048
Craig Tillerf7af2a92017-01-31 15:08:31 -080049def fnize(s):
50 out = ''
51 for c in s:
52 if c in '<>, /':
53 if len(out) and out[-1] == '_': continue
54 out += '_'
55 else:
56 out += c
57 return out
58
Craig Tillerf7af2a92017-01-31 15:08:31 -080059# index html
60index_html = """
61<html>
62<head>
63<title>Microbenchmark Results</title>
64</head>
65<body>
66"""
67
68def heading(name):
69 global index_html
70 index_html += "<h1>%s</h1>\n" % name
71
72def link(txt, tgt):
73 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080074 index_html += "<p><a href=\"%s\">%s</a></p>\n" % (
75 cgi.escape(tgt, quote=True), cgi.escape(txt))
Craig Tillerf7af2a92017-01-31 15:08:31 -080076
Craig Tilleraa64ddf2017-02-08 14:20:08 -080077def text(txt):
78 global index_html
Craig Tiller891e8162017-02-15 23:30:27 -080079 index_html += "<p><pre>%s</pre></p>\n" % cgi.escape(txt)
Craig Tiller7dc4ea62017-02-02 16:08:05 -080080
Craig Tilleraa64ddf2017-02-08 14:20:08 -080081def collect_latency(bm_name, args):
82 """generate latency profiles"""
83 benchmarks = []
84 profile_analysis = []
85 cleanup = []
86
Craig Tillerf7af2a92017-01-31 15:08:31 -080087 heading('Latency Profiles: %s' % bm_name)
88 subprocess.check_call(
89 ['make', bm_name,
90 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()])
91 for line in subprocess.check_output(['bins/basicprof/%s' % bm_name,
92 '--benchmark_list_tests']).splitlines():
Craig Tiller39401792017-02-02 12:22:07 -080093 link(line, '%s.txt' % fnize(line))
Craig Tiller7dc4ea62017-02-02 16:08:05 -080094 benchmarks.append(
95 jobset.JobSpec(['bins/basicprof/%s' % bm_name, '--benchmark_filter=^%s$' % line],
96 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 Tillerf74d1722017-02-08 11:38:09 -0800107 if len(benchmarks) >= min(4, 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 Tilleraa64ddf2017-02-08 14:20:08 -0800166def collect_summary(bm_name, args):
167 heading('Summary: %s' % bm_name)
168 subprocess.check_call(
169 ['make', bm_name,
170 'CONFIG=counters', '-j', '%d' % multiprocessing.cpu_count()])
Craig Tillerd9bc2102017-02-15 08:24:55 -0800171 cmd = ['bins/counters/%s' % bm_name,
172 '--benchmark_out=out.json',
173 '--benchmark_out_format=json']
174 if args.summary_time is not None:
175 cmd += ['--benchmark_min_time=%d' % args.summary_time]
176 text(subprocess.check_output(cmd))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800177 if args.bigquery_upload:
Craig Tillerd12731e2017-02-13 12:41:27 -0800178 with open('out.csv', 'w') as f:
Craig Tillerfc4f72a2017-02-08 15:57:03 -0800179 f.write(subprocess.check_output(['tools/profiling/microbenchmarks/bm2bq.py', 'out.json']))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800180 subprocess.check_call(['bq', 'load', 'microbenchmarks.microbenchmarks', 'out.csv'])
181
182collectors = {
183 'latency': collect_latency,
184 'perf': collect_perf,
185 'summary': collect_summary,
186}
187
188argp = argparse.ArgumentParser(description='Collect data from microbenchmarks')
189argp.add_argument('-c', '--collect',
190 choices=sorted(collectors.keys()),
191 nargs='+',
192 default=sorted(collectors.keys()),
193 help='Which collectors should be run against each benchmark')
194argp.add_argument('-b', '--benchmarks',
Craig Tiller86e36912017-02-15 08:35:55 -0800195 default=['bm_fullstack', 'bm_closure'],
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800196 nargs='+',
197 type=str,
198 help='Which microbenchmarks should be run')
199argp.add_argument('--bigquery_upload',
200 default=False,
201 action='store_const',
202 const=True,
203 help='Upload results from summary collection to bigquery')
Craig Tillerd9bc2102017-02-15 08:24:55 -0800204argp.add_argument('--summary_time',
205 default=None,
206 type=int,
207 help='Minimum time to run benchmarks for the summary collection')
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800208args = argp.parse_args()
209
210for bm_name in args.benchmarks:
211 for collect in args.collect:
212 collectors[collect](bm_name, args)
213
Craig Tillerf7af2a92017-01-31 15:08:31 -0800214index_html += "</body>\n</html>\n"
215with open('reports/index.html', 'w') as f:
Craig Tiller360c0d52017-02-08 13:36:44 -0800216 f.write(index_html)