Craig Tiller | cc89f78 | 2017-04-03 08:08:25 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Matt Kwong | f90d4c8 | 2017-03-09 16:35:21 -0800 | [diff] [blame] | 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. |
Craig Tiller | 76fc48b | 2017-03-07 17:21:16 -0800 | [diff] [blame] | 30 | |
| 31 | import sys |
| 32 | import json |
| 33 | import bm_json |
Craig Tiller | 47c56a6 | 2017-03-08 13:45:53 -0800 | [diff] [blame] | 34 | import tabulate |
ncteisen | ff023ef | 2017-03-10 11:43:48 -0800 | [diff] [blame] | 35 | import argparse |
Craig Tiller | 4274925 | 2017-03-27 22:20:37 -0700 | [diff] [blame] | 36 | from scipy import stats |
Craig Tiller | ec8dee2 | 2017-03-27 21:26:47 -0700 | [diff] [blame] | 37 | import subprocess |
Craig Tiller | a0af68d | 2017-03-27 21:27:37 -0700 | [diff] [blame] | 38 | import multiprocessing |
Craig Tiller | 8aeb898 | 2017-03-27 21:53:04 -0700 | [diff] [blame] | 39 | import collections |
Craig Tiller | 06ac1c7 | 2017-03-28 08:25:35 -0700 | [diff] [blame] | 40 | import pipes |
Craig Tiller | ebb7e82 | 2017-03-29 08:17:11 -0700 | [diff] [blame] | 41 | import os |
| 42 | sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'run_tests', 'python_utils')) |
| 43 | import comment_on_pr |
Craig Tiller | 691eefb | 2017-04-03 08:06:32 -0700 | [diff] [blame] | 44 | import jobset |
Craig Tiller | 2fbbcd1 | 2017-04-03 08:29:08 -0700 | [diff] [blame] | 45 | import itertools |
Craig Tiller | 125b04f | 2017-04-04 21:31:44 -0700 | [diff] [blame] | 46 | import speedup |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 47 | import random |
Craig Tiller | 0553b9c | 2017-04-14 08:31:17 -0700 | [diff] [blame] | 48 | import shutil |
Craig Tiller | 349907b | 2017-04-14 13:00:47 -0700 | [diff] [blame] | 49 | import errno |
Craig Tiller | 76fc48b | 2017-03-07 17:21:16 -0800 | [diff] [blame] | 50 | |
Craig Tiller | 7281d19 | 2017-04-05 15:04:55 -0700 | [diff] [blame] | 51 | _INTERESTING = ( |
| 52 | 'cpu_time', |
| 53 | 'real_time', |
| 54 | 'locks_per_iteration', |
| 55 | 'allocs_per_iteration', |
| 56 | 'writes_per_iteration', |
| 57 | 'atm_cas_per_iteration', |
| 58 | 'atm_add_per_iteration', |
| 59 | ) |
| 60 | |
ncteisen | ff023ef | 2017-03-10 11:43:48 -0800 | [diff] [blame] | 61 | def changed_ratio(n, o): |
| 62 | if float(o) <= .0001: o = 0 |
| 63 | if float(n) <= .0001: n = 0 |
| 64 | if o == 0 and n == 0: return 0 |
| 65 | if o == 0: return 100 |
| 66 | return (float(n)-float(o))/float(o) |
| 67 | |
Craig Tiller | 23e6a8a | 2017-03-30 07:24:01 -0700 | [diff] [blame] | 68 | def median(ary): |
| 69 | ary = sorted(ary) |
| 70 | n = len(ary) |
| 71 | if n%2 == 0: |
| 72 | return (ary[n/2] + ary[n/2+1]) / 2.0 |
| 73 | else: |
| 74 | return ary[n/2] |
| 75 | |
ncteisen | ff023ef | 2017-03-10 11:43:48 -0800 | [diff] [blame] | 76 | def min_change(pct): |
| 77 | return lambda n, o: abs(changed_ratio(n,o)) > pct/100.0 |
| 78 | |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 79 | _AVAILABLE_BENCHMARK_TESTS = ['bm_fullstack_unary_ping_pong', |
| 80 | 'bm_fullstack_streaming_ping_pong', |
| 81 | 'bm_fullstack_streaming_pump', |
| 82 | 'bm_closure', |
| 83 | 'bm_cq', |
| 84 | 'bm_call_create', |
| 85 | 'bm_error', |
| 86 | 'bm_chttp2_hpack', |
| 87 | 'bm_chttp2_transport', |
| 88 | 'bm_pollset', |
| 89 | 'bm_metadata', |
| 90 | 'bm_fullstack_trickle'] |
ncteisen | ff023ef | 2017-03-10 11:43:48 -0800 | [diff] [blame] | 91 | |
| 92 | argp = argparse.ArgumentParser(description='Perform diff on microbenchmarks') |
| 93 | argp.add_argument('-t', '--track', |
Craig Tiller | 7281d19 | 2017-04-05 15:04:55 -0700 | [diff] [blame] | 94 | choices=sorted(_INTERESTING), |
ncteisen | ff023ef | 2017-03-10 11:43:48 -0800 | [diff] [blame] | 95 | nargs='+', |
Craig Tiller | 7281d19 | 2017-04-05 15:04:55 -0700 | [diff] [blame] | 96 | default=sorted(_INTERESTING), |
ncteisen | ff023ef | 2017-03-10 11:43:48 -0800 | [diff] [blame] | 97 | help='Which metrics to track') |
Craig Tiller | c523f5d | 2017-03-27 21:37:19 -0700 | [diff] [blame] | 98 | argp.add_argument('-b', '--benchmarks', nargs='+', choices=_AVAILABLE_BENCHMARK_TESTS, default=['bm_cq']) |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 99 | argp.add_argument('-d', '--diff_base', type=str) |
Craig Tiller | d71ba83 | 2017-04-14 08:36:11 -0700 | [diff] [blame] | 100 | argp.add_argument('-r', '--repetitions', type=int, default=1) |
Craig Tiller | 97e40da | 2017-04-14 09:04:07 -0700 | [diff] [blame] | 101 | argp.add_argument('-l', '--loops', type=int, default=12) |
Craig Tiller | 691eefb | 2017-04-03 08:06:32 -0700 | [diff] [blame] | 102 | argp.add_argument('-j', '--jobs', type=int, default=multiprocessing.cpu_count()) |
ncteisen | ff023ef | 2017-03-10 11:43:48 -0800 | [diff] [blame] | 103 | args = argp.parse_args() |
| 104 | |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 105 | assert args.diff_base |
Craig Tiller | 76fc48b | 2017-03-07 17:21:16 -0800 | [diff] [blame] | 106 | |
Craig Tiller | ed2c292 | 2017-03-27 23:23:45 -0700 | [diff] [blame] | 107 | def avg(lst): |
Craig Tiller | 23e6a8a | 2017-03-30 07:24:01 -0700 | [diff] [blame] | 108 | sum = 0.0 |
| 109 | n = 0.0 |
Craig Tiller | ed2c292 | 2017-03-27 23:23:45 -0700 | [diff] [blame] | 110 | for el in lst: |
| 111 | sum += el |
| 112 | n += 1 |
| 113 | return sum / n |
| 114 | |
Craig Tiller | d1d055a | 2017-03-29 01:52:28 -0700 | [diff] [blame] | 115 | def make_cmd(cfg): |
Craig Tiller | fda2262 | 2017-03-29 01:54:16 -0700 | [diff] [blame] | 116 | return ['make'] + args.benchmarks + [ |
Craig Tiller | 691eefb | 2017-04-03 08:06:32 -0700 | [diff] [blame] | 117 | 'CONFIG=%s' % cfg, '-j', '%d' % args.jobs] |
Craig Tiller | d1d055a | 2017-03-29 01:52:28 -0700 | [diff] [blame] | 118 | |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 119 | def build(dest): |
Craig Tiller | 0553b9c | 2017-04-14 08:31:17 -0700 | [diff] [blame] | 120 | shutil.rmtree('bm_diff_%s' % dest, ignore_errors=True) |
Craig Tiller | d1d055a | 2017-03-29 01:52:28 -0700 | [diff] [blame] | 121 | subprocess.check_call(['git', 'submodule', 'update']) |
Craig Tiller | ba32fee | 2017-03-29 01:49:02 -0700 | [diff] [blame] | 122 | try: |
Craig Tiller | d1d055a | 2017-03-29 01:52:28 -0700 | [diff] [blame] | 123 | subprocess.check_call(make_cmd('opt')) |
| 124 | subprocess.check_call(make_cmd('counters')) |
Craig Tiller | ba32fee | 2017-03-29 01:49:02 -0700 | [diff] [blame] | 125 | except subprocess.CalledProcessError, e: |
| 126 | subprocess.check_call(['make', 'clean']) |
Craig Tiller | d1d055a | 2017-03-29 01:52:28 -0700 | [diff] [blame] | 127 | subprocess.check_call(make_cmd('opt')) |
| 128 | subprocess.check_call(make_cmd('counters')) |
Craig Tiller | 0553b9c | 2017-04-14 08:31:17 -0700 | [diff] [blame] | 129 | os.rename('bins', 'bm_diff_%s' % dest) |
Craig Tiller | ba32fee | 2017-03-29 01:49:02 -0700 | [diff] [blame] | 130 | |
Craig Tiller | 67dd855 | 2017-04-12 16:42:30 -0700 | [diff] [blame] | 131 | def collect1(bm, cfg, ver, idx): |
Craig Tiller | 0553b9c | 2017-04-14 08:31:17 -0700 | [diff] [blame] | 132 | cmd = ['bm_diff_%s/%s/%s' % (ver, cfg, bm), |
Craig Tiller | 67dd855 | 2017-04-12 16:42:30 -0700 | [diff] [blame] | 133 | '--benchmark_out=%s.%s.%s.%d.json' % (bm, cfg, ver, idx), |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 134 | '--benchmark_out_format=json', |
| 135 | '--benchmark_repetitions=%d' % (args.repetitions) |
| 136 | ] |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 137 | return jobset.JobSpec(cmd, shortname='%s %s %s %d/%d' % (bm, cfg, ver, idx+1, args.loops), |
Craig Tiller | 7e31385 | 2017-04-03 15:14:08 -0700 | [diff] [blame] | 138 | verbose_success=True, timeout_seconds=None) |
Craig Tiller | 76fc48b | 2017-03-07 17:21:16 -0800 | [diff] [blame] | 139 | |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 140 | build('new') |
| 141 | |
| 142 | where_am_i = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip() |
| 143 | subprocess.check_call(['git', 'checkout', args.diff_base]) |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 144 | try: |
| 145 | build('old') |
| 146 | finally: |
| 147 | subprocess.check_call(['git', 'checkout', where_am_i]) |
| 148 | subprocess.check_call(['git', 'submodule', 'update']) |
| 149 | |
Craig Tiller | bc442e1 | 2017-04-14 08:33:55 -0700 | [diff] [blame] | 150 | jobs = [] |
Craig Tiller | 0ebff8b | 2017-04-14 08:32:38 -0700 | [diff] [blame] | 151 | for loop in range(0, args.loops): |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 152 | jobs.extend(x for x in itertools.chain( |
Craig Tiller | 67dd855 | 2017-04-12 16:42:30 -0700 | [diff] [blame] | 153 | (collect1(bm, 'opt', 'new', loop) for bm in args.benchmarks), |
| 154 | (collect1(bm, 'counters', 'new', loop) for bm in args.benchmarks), |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 155 | (collect1(bm, 'opt', 'old', loop) for bm in args.benchmarks), |
| 156 | (collect1(bm, 'counters', 'old', loop) for bm in args.benchmarks), |
| 157 | )) |
Craig Tiller | 97e40da | 2017-04-14 09:04:07 -0700 | [diff] [blame] | 158 | random.shuffle(jobs, random.SystemRandom().random) |
Craig Tiller | 67dd855 | 2017-04-12 16:42:30 -0700 | [diff] [blame] | 159 | |
Craig Tiller | 233ad39 | 2017-04-14 08:25:53 -0700 | [diff] [blame] | 160 | jobset.run(jobs, maxjobs=args.jobs) |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 161 | |
| 162 | class Benchmark: |
| 163 | |
| 164 | def __init__(self): |
| 165 | self.samples = { |
| 166 | True: collections.defaultdict(list), |
| 167 | False: collections.defaultdict(list) |
| 168 | } |
| 169 | self.final = {} |
| 170 | |
| 171 | def add_sample(self, data, new): |
Craig Tiller | 9a212df | 2017-03-30 13:19:45 -0700 | [diff] [blame] | 172 | for f in args.track: |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 173 | if f in data: |
Craig Tiller | 4efb5e1 | 2017-03-27 22:37:00 -0700 | [diff] [blame] | 174 | self.samples[new][f].append(float(data[f])) |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 175 | |
| 176 | def process(self): |
Craig Tiller | 9298a92 | 2017-03-31 06:51:48 -0700 | [diff] [blame] | 177 | for f in sorted(args.track): |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 178 | new = self.samples[True][f] |
| 179 | old = self.samples[False][f] |
| 180 | if not new or not old: continue |
Craig Tiller | b1c02ca | 2017-04-05 14:11:28 -0700 | [diff] [blame] | 181 | mdn_diff = abs(median(new) - median(old)) |
| 182 | print '%s: new=%r old=%r mdn_diff=%r' % (f, new, old, mdn_diff) |
Craig Tiller | 125b04f | 2017-04-04 21:31:44 -0700 | [diff] [blame] | 183 | s = speedup.speedup(new, old) |
Craig Tiller | 9646477 | 2017-04-13 22:06:25 -0700 | [diff] [blame] | 184 | if abs(s) > 3 and mdn_diff > 0.5: |
Craig Tiller | e5f1828 | 2017-04-05 09:08:37 -0700 | [diff] [blame] | 185 | self.final[f] = '%+d%%' % s |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 186 | return self.final.keys() |
| 187 | |
Craig Tiller | a87b138 | 2017-03-27 23:06:10 -0700 | [diff] [blame] | 188 | def skip(self): |
| 189 | return not self.final |
| 190 | |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 191 | def row(self, flds): |
| 192 | return [self.final[f] if f in self.final else '' for f in flds] |
| 193 | |
| 194 | |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 195 | def eintr_be_gone(fn): |
| 196 | """Run fn until it doesn't stop because of EINTR""" |
Craig Tiller | 349907b | 2017-04-14 13:00:47 -0700 | [diff] [blame] | 197 | while True: |
| 198 | try: |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 199 | return fn() |
Craig Tiller | 349907b | 2017-04-14 13:00:47 -0700 | [diff] [blame] | 200 | except IOError, e: |
| 201 | if e.errno != errno.EINTR: |
| 202 | raise |
| 203 | |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 204 | |
Craig Tiller | 349907b | 2017-04-14 13:00:47 -0700 | [diff] [blame] | 205 | def read_json(filename): |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 206 | with open(filename) as f: return json.loads(f.read()) |
Craig Tiller | 349907b | 2017-04-14 13:00:47 -0700 | [diff] [blame] | 207 | |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 208 | |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 209 | def finalize(): |
| 210 | benchmarks = collections.defaultdict(Benchmark) |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 211 | |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 212 | for bm in args.benchmarks: |
| 213 | for loop in range(0, args.loops): |
| 214 | js_new_ctr = read_json('%s.counters.new.%d.json' % (bm, loop)) |
| 215 | js_new_opt = read_json('%s.opt.new.%d.json' % (bm, loop)) |
| 216 | js_old_ctr = read_json('%s.counters.old.%d.json' % (bm, loop)) |
| 217 | js_old_opt = read_json('%s.opt.old.%d.json' % (bm, loop)) |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 218 | |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 219 | for row in bm_json.expand_json(js_new_ctr, js_new_opt): |
| 220 | print row |
| 221 | name = row['cpp_name'] |
| 222 | if name.endswith('_mean') or name.endswith('_stddev'): continue |
| 223 | benchmarks[name].add_sample(row, True) |
| 224 | for row in bm_json.expand_json(js_old_ctr, js_old_opt): |
| 225 | print row |
| 226 | name = row['cpp_name'] |
| 227 | if name.endswith('_mean') or name.endswith('_stddev'): continue |
| 228 | benchmarks[name].add_sample(row, False) |
Craig Tiller | d23826e | 2017-03-27 21:25:15 -0700 | [diff] [blame] | 229 | |
Craig Tiller | 25e3c6d | 2017-04-18 13:57:38 -0700 | [diff] [blame^] | 230 | really_interesting = set() |
| 231 | for name, bm in benchmarks.items(): |
| 232 | print name |
| 233 | really_interesting.update(bm.process()) |
| 234 | fields = [f for f in args.track if f in really_interesting] |
| 235 | |
| 236 | headers = ['Benchmark'] + fields |
| 237 | rows = [] |
| 238 | for name in sorted(benchmarks.keys()): |
| 239 | if benchmarks[name].skip(): continue |
| 240 | rows.append([name] + benchmarks[name].row(fields)) |
| 241 | if rows: |
| 242 | text = 'Performance differences noted:\n' + tabulate.tabulate(rows, headers=headers, floatfmt='+.2f') |
| 243 | else: |
| 244 | text = 'No significant performance differences' |
| 245 | comment_on_pr.comment_on_pr('```\n%s\n```' % text) |
| 246 | print text |
| 247 | |
| 248 | |
| 249 | eintr_be_gone(finalize) |
| 250 | |