Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 1 | #!/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 | |
| 31 | import multiprocessing |
| 32 | import os |
| 33 | import subprocess |
| 34 | import sys |
| 35 | |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 36 | import python_utils.jobset as jobset |
| 37 | import python_utils.start_port_server as start_port_server |
| 38 | |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 39 | flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph') |
| 40 | |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 41 | os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
| 42 | if not os.path.exists('reports'): |
| 43 | os.makedirs('reports') |
| 44 | |
| 45 | port_server_port = 32766 |
| 46 | start_port_server.start_port_server(port_server_port) |
| 47 | |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 48 | def 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 Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 58 | # index html |
| 59 | index_html = """ |
| 60 | <html> |
| 61 | <head> |
| 62 | <title>Microbenchmark Results</title> |
| 63 | </head> |
| 64 | <body> |
| 65 | """ |
| 66 | |
| 67 | def heading(name): |
| 68 | global index_html |
| 69 | index_html += "<h1>%s</h1>\n" % name |
| 70 | |
| 71 | def link(txt, tgt): |
| 72 | global index_html |
| 73 | index_html += "<p><a href=\"%s\">%s</a></p>\n" % (tgt, txt) |
| 74 | |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 75 | benchmarks = [] |
| 76 | profile_analysis = [] |
Craig Tiller | 6911d08 | 2017-02-07 10:30:44 -0800 | [diff] [blame] | 77 | cleanup = [] |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 78 | |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 79 | for 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 Tiller | 3940179 | 2017-02-02 12:22:07 -0800 | [diff] [blame] | 87 | link(line, '%s.txt' % fnize(line)) |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 88 | 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 Tiller | 715e43b | 2017-02-07 11:13:16 -0800 | [diff] [blame^] | 96 | cleanup.append(jobset.JobSpec(['rm', '%s.trace' % fnize(line)])) |
Craig Tiller | 6911d08 | 2017-02-07 10:30:44 -0800 | [diff] [blame] | 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 Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 110 | |
| 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 Tiller | 95ca017 | 2017-02-02 12:27:11 -0800 | [diff] [blame] | 118 | subprocess.check_call(['sudo', 'perf', 'record', '-g', '-c', '1000', |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 119 | '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 Tiller | 3940179 | 2017-02-02 12:22:07 -0800 | [diff] [blame] | 127 | link(line, '%s.svg' % fnize(line)) |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 128 | 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 | |
| 132 | index_html += "</body>\n</html>\n" |
| 133 | with open('reports/index.html', 'w') as f: |
| 134 | w.write(index_html) |