blob: 42a31a622f74dca90fd5eb4f2cd19731b19ca3cc [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 = []
77
Craig Tillerf7af2a92017-01-31 15:08:31 -080078for bm_name in sys.argv[1:]:
79 # generate latency profiles
80 heading('Latency Profiles: %s' % bm_name)
81 subprocess.check_call(
82 ['make', bm_name,
83 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()])
84 for line in subprocess.check_output(['bins/basicprof/%s' % bm_name,
85 '--benchmark_list_tests']).splitlines():
Craig Tiller39401792017-02-02 12:22:07 -080086 link(line, '%s.txt' % fnize(line))
Craig Tiller7dc4ea62017-02-02 16:08:05 -080087 benchmarks.append(
88 jobset.JobSpec(['bins/basicprof/%s' % bm_name, '--benchmark_filter=^%s$' % line],
89 environ={'LATENCY_TRACE': '%s.trace' % fnize(line)}))
90 profile_analysis.append(
91 jobset.JobSpec([sys.executable,
92 'tools/profiling/latency_profile/profile_analyzer.py',
93 '--source', '%s.trace' % fnize(line), '--fmt', 'simple',
94 '--out', 'reports/%s.txt' % fnize(line)], timeout_seconds=None))
95
96 jobset.run(benchmarks, maxjobs=multiprocessing.cpu_count()/2,
97 add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port})
98 jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count())
Craig Tillerf7af2a92017-01-31 15:08:31 -080099
100 # generate flamegraphs
101 heading('Flamegraphs: %s' % bm_name)
102 subprocess.check_call(
103 ['make', bm_name,
104 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()])
105 for line in subprocess.check_output(['bins/mutrace/%s' % bm_name,
106 '--benchmark_list_tests']).splitlines():
Craig Tiller95ca0172017-02-02 12:27:11 -0800107 subprocess.check_call(['sudo', 'perf', 'record', '-g', '-c', '1000',
Craig Tillerf7af2a92017-01-31 15:08:31 -0800108 'bins/mutrace/%s' % bm_name,
109 '--benchmark_filter=^%s$' % line,
110 '--benchmark_min_time=20'])
111 with open('/tmp/bm.perf', 'w') as f:
112 f.write(subprocess.check_output(['sudo', 'perf', 'script']))
113 with open('/tmp/bm.folded', 'w') as f:
114 f.write(subprocess.check_output([
115 '%s/stackcollapse-perf.pl' % flamegraph_dir, '/tmp/bm.perf']))
Craig Tiller39401792017-02-02 12:22:07 -0800116 link(line, '%s.svg' % fnize(line))
Craig Tillerf7af2a92017-01-31 15:08:31 -0800117 with open('reports/%s.svg' % fnize(line), 'w') as f:
118 f.write(subprocess.check_output([
119 '%s/flamegraph.pl' % flamegraph_dir, '/tmp/bm.folded']))
120
121index_html += "</body>\n</html>\n"
122with open('reports/index.html', 'w') as f:
123 w.write(index_html)