The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#) https://grpc.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.0 KiB

#!/usr/bin/env python3
8 years ago
#
# Copyright 2017 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Computes the diff between two qps runs and outputs significant results """
import argparse
import json
import multiprocessing
import os
8 years ago
import shutil
import subprocess
import sys
import qps_scenarios
8 years ago
import tabulate
sys.path.append(
os.path.join(os.path.dirname(sys.argv[0]), '..', 'microbenchmarks',
'bm_diff'))
8 years ago
import bm_speedup
sys.path.append(
os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'run_tests',
'python_utils'))
import check_on_pr
8 years ago
def _args():
7 years ago
argp = argparse.ArgumentParser(description='Perform diff on QPS Driver')
argp.add_argument('-d',
'--diff_base',
type=str,
help='Commit or branch to compare the current one to')
7 years ago
argp.add_argument(
'-l',
'--loops',
type=int,
default=4,
help='Number of loops for each benchmark. More loops cuts down on noise'
)
argp.add_argument('-j',
'--jobs',
type=int,
default=multiprocessing.cpu_count(),
help='Number of CPUs to use')
7 years ago
args = argp.parse_args()
assert args.diff_base, "diff_base must be set"
return args
8 years ago
8 years ago
def _make_cmd(jobs):
7 years ago
return ['make', '-j', '%d' % jobs, 'qps_json_driver', 'qps_worker']
8 years ago
def build(name, jobs):
7 years ago
shutil.rmtree('qps_diff_%s' % name, ignore_errors=True)
subprocess.check_call(['git', 'submodule', 'update'])
try:
subprocess.check_call(_make_cmd(jobs))
except subprocess.CalledProcessError as e:
7 years ago
subprocess.check_call(['make', 'clean'])
subprocess.check_call(_make_cmd(jobs))
os.rename('bins', 'qps_diff_%s' % name)
8 years ago
8 years ago
def _run_cmd(name, scenario, fname):
7 years ago
return [
'qps_diff_%s/opt/qps_json_driver' % name, '--scenarios_json', scenario,
'--json_file_out', fname
]
8 years ago
8 years ago
def run(name, scenarios, loops):
7 years ago
for sn in scenarios:
for i in range(0, loops):
fname = "%s.%s.%d.json" % (sn, name, i)
subprocess.check_call(_run_cmd(name, scenarios[sn], fname))
8 years ago
8 years ago
def _load_qps(fname):
7 years ago
try:
with open(fname) as f:
return json.loads(f.read())['qps']
except IOError as e:
print(("IOError occurred reading file: %s" % fname))
7 years ago
return None
except ValueError as e:
print(("ValueError occurred reading file: %s" % fname))
7 years ago
return None
8 years ago
8 years ago
def _median(ary):
7 years ago
assert (len(ary))
ary = sorted(ary)
n = len(ary)
if n % 2 == 0:
return (ary[(n - 1) / 2] + ary[(n - 1) / 2 + 1]) / 2.0
else:
return ary[n / 2]
8 years ago
8 years ago
def diff(scenarios, loops, old, new):
7 years ago
old_data = {}
new_data = {}
# collect data
for sn in scenarios:
old_data[sn] = []
new_data[sn] = []
for i in range(loops):
old_data[sn].append(_load_qps("%s.%s.%d.json" % (sn, old, i)))
new_data[sn].append(_load_qps("%s.%s.%d.json" % (sn, new, i)))
# crunch data
headers = ['Benchmark', 'qps']
rows = []
for sn in scenarios:
mdn_diff = abs(_median(new_data[sn]) - _median(old_data[sn]))
print(('%s: %s=%r %s=%r mdn_diff=%r' %
(sn, new, new_data[sn], old, old_data[sn], mdn_diff)))
7 years ago
s = bm_speedup.speedup(new_data[sn], old_data[sn], 10e-5)
if abs(s) > 3 and mdn_diff > 0.5:
rows.append([sn, '%+d%%' % s])
if rows:
return tabulate.tabulate(rows, headers=headers, floatfmt='+.2f')
else:
return None
8 years ago
def main(args):
7 years ago
build('new', args.jobs)
8 years ago
7 years ago
if args.diff_base:
where_am_i = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
7 years ago
subprocess.check_call(['git', 'checkout', args.diff_base])
try:
build('old', args.jobs)
finally:
subprocess.check_call(['git', 'checkout', where_am_i])
subprocess.check_call(['git', 'submodule', 'update'])
8 years ago
7 years ago
run('new', qps_scenarios._SCENARIOS, args.loops)
run('old', qps_scenarios._SCENARIOS, args.loops)
8 years ago
7 years ago
diff_output = diff(qps_scenarios._SCENARIOS, args.loops, 'old', 'new')
8 years ago
7 years ago
if diff_output:
text = '[qps] Performance differences noted:\n%s' % diff_output
else:
text = '[qps] No significant performance differences'
print(('%s' % text))
check_on_pr.check_on_pr('QPS', '```\n%s\n```' % text)
8 years ago
8 years ago
if __name__ == '__main__':
7 years ago
args = _args()
main(args)