blob: 1cafffb52dd2edae7d8f72dc90764b639ced7a13 [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
31import multiprocessing
32import os
33import subprocess
34import sys
Craig Tilleraa64ddf2017-02-08 14:20:08 -080035import argparse
Craig Tillerf7af2a92017-01-31 15:08:31 -080036
Craig Tiller7dc4ea62017-02-02 16:08:05 -080037import python_utils.jobset as jobset
38import python_utils.start_port_server as start_port_server
39
Craig Tillerf7af2a92017-01-31 15:08:31 -080040flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph')
41
Craig Tiller7dc4ea62017-02-02 16:08:05 -080042os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
43if not os.path.exists('reports'):
44 os.makedirs('reports')
45
46port_server_port = 32766
47start_port_server.start_port_server(port_server_port)
48
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
74 index_html += "<p><a href=\"%s\">%s</a></p>\n" % (tgt, txt)
75
Craig Tilleraa64ddf2017-02-08 14:20:08 -080076def text(txt):
77 global index_html
Craig Tillerd1307fe2017-02-08 18:02:21 -080078 index_html += "<p><pre>%s</pre></p>\n" % txt
Craig Tiller7dc4ea62017-02-02 16:08:05 -080079
Craig Tilleraa64ddf2017-02-08 14:20:08 -080080def collect_latency(bm_name, args):
81 """generate latency profiles"""
82 benchmarks = []
83 profile_analysis = []
84 cleanup = []
85
Craig Tillerf7af2a92017-01-31 15:08:31 -080086 heading('Latency Profiles: %s' % bm_name)
87 subprocess.check_call(
88 ['make', bm_name,
89 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()])
90 for line in subprocess.check_output(['bins/basicprof/%s' % bm_name,
91 '--benchmark_list_tests']).splitlines():
Craig Tiller39401792017-02-02 12:22:07 -080092 link(line, '%s.txt' % fnize(line))
Craig Tiller7dc4ea62017-02-02 16:08:05 -080093 benchmarks.append(
Craig Tillerece502f2017-02-17 16:20:50 -080094 jobset.JobSpec(['bins/basicprof/%s' % bm_name,
95 '--benchmark_filter=^%s$' % line,
96 '--benchmark_min_time=0.05'],
Craig Tiller7dc4ea62017-02-02 16:08:05 -080097 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 Tillerece502f2017-02-17 16:20:50 -0800108 if len(benchmarks) >= min(16, 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()])
131 for line in subprocess.check_output(['bins/mutrace/%s' % bm_name,
132 '--benchmark_list_tests']).splitlines():
Craig Tiller26077632017-02-13 11:26:43 -0800133 subprocess.check_call(['sudo', 'perf', 'record', '-o', 'perf.data',
134 '-g', '-c', '1000',
Craig Tillerf7af2a92017-01-31 15:08:31 -0800135 'bins/mutrace/%s' % bm_name,
136 '--benchmark_filter=^%s$' % line,
137 '--benchmark_min_time=20'])
Craig Tillera48e6902017-02-13 19:31:49 -0800138 subprocess.check_call(['sudo', 'perf', 'script', '-i', 'perf.data', '>', 'bm.perf'], shell=True)
139 subprocess.check_call([
140 '%s/stackcollapse-perf.pl' % flamegraph_dir, 'bm.perf', '>', 'bm.folded'], shell=True)
Craig Tiller39401792017-02-02 12:22:07 -0800141 link(line, '%s.svg' % fnize(line))
Craig Tillerf7af2a92017-01-31 15:08:31 -0800142 with open('reports/%s.svg' % fnize(line), 'w') as f:
143 f.write(subprocess.check_output([
Craig Tiller557443e2017-02-09 22:30:37 -0800144 '%s/flamegraph.pl' % flamegraph_dir, 'bm.folded']))
Craig Tillerf7af2a92017-01-31 15:08:31 -0800145
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800146def collect_summary(bm_name, args):
147 heading('Summary: %s' % bm_name)
148 subprocess.check_call(
149 ['make', bm_name,
150 'CONFIG=counters', '-j', '%d' % multiprocessing.cpu_count()])
151 text(subprocess.check_output(['bins/counters/%s' % bm_name,
152 '--benchmark_out=out.json',
153 '--benchmark_out_format=json']))
154 if args.bigquery_upload:
Craig Tillerd12731e2017-02-13 12:41:27 -0800155 with open('out.csv', 'w') as f:
Craig Tillerfc4f72a2017-02-08 15:57:03 -0800156 f.write(subprocess.check_output(['tools/profiling/microbenchmarks/bm2bq.py', 'out.json']))
Craig Tilleraa64ddf2017-02-08 14:20:08 -0800157 subprocess.check_call(['bq', 'load', 'microbenchmarks.microbenchmarks', 'out.csv'])
158
159collectors = {
160 'latency': collect_latency,
161 'perf': collect_perf,
162 'summary': collect_summary,
163}
164
165argp = argparse.ArgumentParser(description='Collect data from microbenchmarks')
166argp.add_argument('-c', '--collect',
167 choices=sorted(collectors.keys()),
168 nargs='+',
169 default=sorted(collectors.keys()),
170 help='Which collectors should be run against each benchmark')
171argp.add_argument('-b', '--benchmarks',
172 default=['bm_fullstack'],
173 nargs='+',
174 type=str,
175 help='Which microbenchmarks should be run')
176argp.add_argument('--bigquery_upload',
177 default=False,
178 action='store_const',
179 const=True,
180 help='Upload results from summary collection to bigquery')
181args = argp.parse_args()
182
183for bm_name in args.benchmarks:
184 for collect in args.collect:
185 collectors[collect](bm_name, args)
186
Craig Tillerf7af2a92017-01-31 15:08:31 -0800187index_html += "</body>\n</html>\n"
188with open('reports/index.html', 'w') as f:
Craig Tiller360c0d52017-02-08 13:36:44 -0800189 f.write(index_html)