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 |
Craig Tiller | aa64ddf | 2017-02-08 14:20:08 -0800 | [diff] [blame] | 35 | import argparse |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 36 | |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 37 | import python_utils.jobset as jobset |
| 38 | import python_utils.start_port_server as start_port_server |
| 39 | |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 40 | flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph') |
| 41 | |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 42 | os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
| 43 | if not os.path.exists('reports'): |
| 44 | os.makedirs('reports') |
| 45 | |
| 46 | port_server_port = 32766 |
| 47 | start_port_server.start_port_server(port_server_port) |
| 48 | |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 49 | def 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 Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 59 | # index html |
| 60 | index_html = """ |
| 61 | <html> |
| 62 | <head> |
| 63 | <title>Microbenchmark Results</title> |
| 64 | </head> |
| 65 | <body> |
| 66 | """ |
| 67 | |
| 68 | def heading(name): |
| 69 | global index_html |
| 70 | index_html += "<h1>%s</h1>\n" % name |
| 71 | |
| 72 | def link(txt, tgt): |
| 73 | global index_html |
| 74 | index_html += "<p><a href=\"%s\">%s</a></p>\n" % (tgt, txt) |
| 75 | |
Craig Tiller | aa64ddf | 2017-02-08 14:20:08 -0800 | [diff] [blame] | 76 | def text(txt): |
| 77 | global index_html |
Craig Tiller | d1307fe | 2017-02-08 18:02:21 -0800 | [diff] [blame] | 78 | index_html += "<p><pre>%s</pre></p>\n" % txt |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 79 | |
Craig Tiller | aa64ddf | 2017-02-08 14:20:08 -0800 | [diff] [blame] | 80 | def collect_latency(bm_name, args): |
| 81 | """generate latency profiles""" |
| 82 | benchmarks = [] |
| 83 | profile_analysis = [] |
| 84 | cleanup = [] |
| 85 | |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 86 | 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 Tiller | 3940179 | 2017-02-02 12:22:07 -0800 | [diff] [blame] | 92 | link(line, '%s.txt' % fnize(line)) |
Craig Tiller | 7dc4ea6 | 2017-02-02 16:08:05 -0800 | [diff] [blame] | 93 | benchmarks.append( |
| 94 | jobset.JobSpec(['bins/basicprof/%s' % bm_name, '--benchmark_filter=^%s$' % line], |
| 95 | environ={'LATENCY_TRACE': '%s.trace' % fnize(line)})) |
| 96 | profile_analysis.append( |
| 97 | jobset.JobSpec([sys.executable, |
| 98 | 'tools/profiling/latency_profile/profile_analyzer.py', |
| 99 | '--source', '%s.trace' % fnize(line), '--fmt', 'simple', |
| 100 | '--out', 'reports/%s.txt' % fnize(line)], timeout_seconds=None)) |
Craig Tiller | 715e43b | 2017-02-07 11:13:16 -0800 | [diff] [blame] | 101 | cleanup.append(jobset.JobSpec(['rm', '%s.trace' % fnize(line)])) |
Craig Tiller | 360c0d5 | 2017-02-08 13:36:44 -0800 | [diff] [blame] | 102 | # periodically flush out the list of jobs: profile_analysis jobs at least |
| 103 | # consume upwards of five gigabytes of ram in some cases, and so analysing |
| 104 | # hundreds of them at once is impractical -- but we want at least some |
| 105 | # concurrency or the work takes too long |
Craig Tiller | f74d172 | 2017-02-08 11:38:09 -0800 | [diff] [blame] | 106 | if len(benchmarks) >= min(4, multiprocessing.cpu_count()): |
Craig Tiller | 360c0d5 | 2017-02-08 13:36:44 -0800 | [diff] [blame] | 107 | # run up to half the cpu count: each benchmark can use up to two cores |
| 108 | # (one for the microbenchmark, one for the data flush) |
Craig Tiller | 2ef0d54 | 2017-02-08 13:53:18 -0800 | [diff] [blame] | 109 | jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2), |
Craig Tiller | 6911d08 | 2017-02-07 10:30:44 -0800 | [diff] [blame] | 110 | add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port}) |
| 111 | jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count()) |
| 112 | jobset.run(cleanup, maxjobs=multiprocessing.cpu_count()) |
| 113 | benchmarks = [] |
| 114 | profile_analysis = [] |
| 115 | cleanup = [] |
Craig Tiller | 360c0d5 | 2017-02-08 13:36:44 -0800 | [diff] [blame] | 116 | # run the remaining benchmarks that weren't flushed |
Craig Tiller | 6911d08 | 2017-02-07 10:30:44 -0800 | [diff] [blame] | 117 | if len(benchmarks): |
Craig Tiller | 2ef0d54 | 2017-02-08 13:53:18 -0800 | [diff] [blame] | 118 | jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2), |
Craig Tiller | 6911d08 | 2017-02-07 10:30:44 -0800 | [diff] [blame] | 119 | add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port}) |
| 120 | jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count()) |
| 121 | jobset.run(cleanup, maxjobs=multiprocessing.cpu_count()) |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 122 | |
Craig Tiller | aa64ddf | 2017-02-08 14:20:08 -0800 | [diff] [blame] | 123 | def collect_perf(bm_name, args): |
| 124 | """generate flamegraphs""" |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 125 | heading('Flamegraphs: %s' % bm_name) |
| 126 | subprocess.check_call( |
| 127 | ['make', bm_name, |
| 128 | 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()]) |
Craig Tiller | 6ad0072 | 2017-02-15 09:14:24 -0800 | [diff] [blame^] | 129 | benchmarks = [] |
| 130 | profile_analysis = [] |
| 131 | cleanup = [] |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 132 | for line in subprocess.check_output(['bins/mutrace/%s' % bm_name, |
| 133 | '--benchmark_list_tests']).splitlines(): |
Craig Tiller | 5a8c586 | 2017-02-15 07:59:05 -0800 | [diff] [blame] | 134 | link(line, '%s.svg' % fnize(line)) |
Craig Tiller | 6ad0072 | 2017-02-15 09:14:24 -0800 | [diff] [blame^] | 135 | benchmarks.append( |
| 136 | jobset.JobSpec(['perf', 'record', '-o', '%s-perf.data' % fnize(line), |
| 137 | '-g', '-c', '1000', |
| 138 | 'bins/mutrace/%s' % bm_name, |
| 139 | '--benchmark_filter=^%s$' % line, |
| 140 | '--benchmark_min_time=10'])) |
| 141 | profile_analysis.append( |
| 142 | jobset.JobSpec(['tools/run_tests/performance/process_local_perf_flamegraphs.sh'], |
| 143 | environ = { |
| 144 | 'PERF_BASE_NAME': fnize(line), |
| 145 | 'OUTPUT_DIR': 'reports', |
| 146 | 'OUTPUT_FILENAME': fnize(line), |
| 147 | })) |
| 148 | cleanup.append(jobset.JobSpec(['rm', '%s-perf.data' % fnize(line)])) |
| 149 | cleanup.append(jobset.JobSpec(['rm', '%s-out.perf' % fnize(line)])) |
| 150 | # periodically flush out the list of jobs: temporary space required for this |
| 151 | # processing is large |
| 152 | if len(benchmarks) >= 20: |
| 153 | # run up to half the cpu count: each benchmark can use up to two cores |
| 154 | # (one for the microbenchmark, one for the data flush) |
| 155 | jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2), |
| 156 | add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port}) |
| 157 | jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count()) |
| 158 | jobset.run(cleanup, maxjobs=multiprocessing.cpu_count()) |
| 159 | benchmarks = [] |
| 160 | profile_analysis = [] |
| 161 | cleanup = [] |
| 162 | # run the remaining benchmarks that weren't flushed |
| 163 | if len(benchmarks): |
| 164 | jobset.run(benchmarks, maxjobs=max(1, multiprocessing.cpu_count()/2), |
| 165 | add_env={'GRPC_TEST_PORT_SERVER': 'localhost:%d' % port_server_port}) |
| 166 | jobset.run(profile_analysis, maxjobs=multiprocessing.cpu_count()) |
| 167 | jobset.run(cleanup, maxjobs=multiprocessing.cpu_count()) |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 168 | |
Craig Tiller | aa64ddf | 2017-02-08 14:20:08 -0800 | [diff] [blame] | 169 | def collect_summary(bm_name, args): |
| 170 | heading('Summary: %s' % bm_name) |
| 171 | subprocess.check_call( |
| 172 | ['make', bm_name, |
| 173 | 'CONFIG=counters', '-j', '%d' % multiprocessing.cpu_count()]) |
| 174 | text(subprocess.check_output(['bins/counters/%s' % bm_name, |
| 175 | '--benchmark_out=out.json', |
| 176 | '--benchmark_out_format=json'])) |
| 177 | if args.bigquery_upload: |
Craig Tiller | d12731e | 2017-02-13 12:41:27 -0800 | [diff] [blame] | 178 | with open('out.csv', 'w') as f: |
Craig Tiller | fc4f72a | 2017-02-08 15:57:03 -0800 | [diff] [blame] | 179 | f.write(subprocess.check_output(['tools/profiling/microbenchmarks/bm2bq.py', 'out.json'])) |
Craig Tiller | aa64ddf | 2017-02-08 14:20:08 -0800 | [diff] [blame] | 180 | subprocess.check_call(['bq', 'load', 'microbenchmarks.microbenchmarks', 'out.csv']) |
| 181 | |
| 182 | collectors = { |
| 183 | 'latency': collect_latency, |
| 184 | 'perf': collect_perf, |
| 185 | 'summary': collect_summary, |
| 186 | } |
| 187 | |
| 188 | argp = argparse.ArgumentParser(description='Collect data from microbenchmarks') |
| 189 | argp.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') |
| 194 | argp.add_argument('-b', '--benchmarks', |
| 195 | default=['bm_fullstack'], |
| 196 | nargs='+', |
| 197 | type=str, |
| 198 | help='Which microbenchmarks should be run') |
| 199 | argp.add_argument('--bigquery_upload', |
| 200 | default=False, |
| 201 | action='store_const', |
| 202 | const=True, |
| 203 | help='Upload results from summary collection to bigquery') |
| 204 | args = argp.parse_args() |
| 205 | |
| 206 | for bm_name in args.benchmarks: |
| 207 | for collect in args.collect: |
| 208 | collectors[collect](bm_name, args) |
| 209 | |
Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame] | 210 | index_html += "</body>\n</html>\n" |
| 211 | with open('reports/index.html', 'w') as f: |
Craig Tiller | 360c0d5 | 2017-02-08 13:36:44 -0800 | [diff] [blame] | 212 | f.write(index_html) |