blob: d51388bbf059ca33b915032fd5edfbb52d2f3cea [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
35
Craig Tiller7dc4ea62017-02-02 16:08:05 -080036import python_utils.jobset as jobset
37import python_utils.start_port_server as start_port_server
38
Craig Tillerf7af2a92017-01-31 15:08:31 -080039flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph')
40
Craig Tiller7dc4ea62017-02-02 16:08:05 -080041os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
42if not os.path.exists('reports'):
43 os.makedirs('reports')
44
45port_server_port = 32766
46start_port_server.start_port_server(port_server_port)
47
Craig Tillerf7af2a92017-01-31 15:08:31 -080048def fnize(s):
49 out = ''
50 for c in s:
51 if c in '<>, /':
52 if len(out) and out[-1] == '_': continue
53 out += '_'
54 else:
55 out += c
56 return out
57
Craig Tillerf7af2a92017-01-31 15:08:31 -080058# index html
59index_html = """
60<html>
61<head>
62<title>Microbenchmark Results</title>
63</head>
64<body>
65"""
66
67def heading(name):
68 global index_html
69 index_html += "<h1>%s</h1>\n" % name
70
71def link(txt, tgt):
72 global index_html
73 index_html += "<p><a href=\"%s\">%s</a></p>\n" % (tgt, txt)
74
Craig Tiller7dc4ea62017-02-02 16:08:05 -080075benchmarks = []
76profile_analysis = []
Craig Tiller6911d082017-02-07 10:30:44 -080077cleanup = []
Craig Tiller7dc4ea62017-02-02 16:08:05 -080078
Craig Tillerf7af2a92017-01-31 15:08:31 -080079for bm_name in sys.argv[1:]:
80 # generate latency profiles
81 heading('Latency Profiles: %s' % bm_name)
82 subprocess.check_call(
83 ['make', bm_name,
84 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()])
85 for line in subprocess.check_output(['bins/basicprof/%s' % bm_name,
86 '--benchmark_list_tests']).splitlines():
Craig Tiller39401792017-02-02 12:22:07 -080087 link(line, '%s.txt' % fnize(line))
Craig Tiller7dc4ea62017-02-02 16:08:05 -080088 benchmarks.append(
89 jobset.JobSpec(['bins/basicprof/%s' % bm_name, '--benchmark_filter=^%s$' % line],
90 environ={'LATENCY_TRACE': '%s.trace' % fnize(line)}))
91 profile_analysis.append(
92 jobset.JobSpec([sys.executable,
93 'tools/profiling/latency_profile/profile_analyzer.py',
94 '--source', '%s.trace' % fnize(line), '--fmt', 'simple',
95 '--out', 'reports/%s.txt' % fnize(line)], timeout_seconds=None))
Craig Tiller6911d082017-02-07 10:30:44 -080096 cleanup.append('rm', '%s.trace' % fnize(line))
97 if len(benchmarks) >= 2 * multiprocessing.cpu_count():
98 jobset.run(benchmarks, maxjobs=multiprocessing.cpu_count()/2,
99 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
100 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
101 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
102 benchmarks = []
103 profile_analysis = []
104 cleanup = []
105 if len(benchmarks):
106 jobset.run(benchmarks, maxjobs=multiprocessing.cpu_count()/2,
107 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
108 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
109 jobset.run(cleanup, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -0800110
111 # generate flamegraphs
112 heading('Flamegraphs: %s' % bm_name)
113 subprocess.check_call(
114 ['make', bm_name,
115 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()])
116 for line in subprocess.check_output(['bins/mutrace/%s' % bm_name,
117 '--benchmark_list_tests']).splitlines():
Craig Tiller95ca0172017-02-02 12:27:11 -0800118 subprocess.check_call(['sudo', 'perf', 'record', '-g', '-c', '1000',
Craig Tillerf7af2a92017-01-31 15:08:31 -0800119 'bins/mutrace/%s' % bm_name,
120 '--benchmark_filter=^%s$' % line,
121 '--benchmark_min_time=20'])
122 with open('/tmp/bm.perf', 'w') as f:
123 f.write(subprocess.check_output(['sudo', 'perf', 'script']))
124 with open('/tmp/bm.folded', 'w') as f:
125 f.write(subprocess.check_output([
126 '%s/stackcollapse-perf.pl' % flamegraph_dir, '/tmp/bm.perf']))
Craig Tiller39401792017-02-02 12:22:07 -0800127 link(line, '%s.svg' % fnize(line))
Craig Tillerf7af2a92017-01-31 15:08:31 -0800128 with open('reports/%s.svg' % fnize(line), 'w') as f:
129 f.write(subprocess.check_output([
130 '%s/flamegraph.pl' % flamegraph_dir, '/tmp/bm.folded']))
131
132index_html += "</body>\n</html>\n"
133with open('reports/index.html', 'w') as f:
134 w.write(index_html)