qps_diff.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2017 gRPC authors.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """ Computes the diff between two qps runs and outputs significant results """
  17. import argparse
  18. import json
  19. import multiprocessing
  20. import os
  21. import shutil
  22. import subprocess
  23. import sys
  24. import qps_scenarios
  25. import tabulate
  26. sys.path.append(
  27. os.path.join(os.path.dirname(sys.argv[0]), '..', 'microbenchmarks',
  28. 'bm_diff'))
  29. import bm_speedup
  30. sys.path.append(
  31. os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'run_tests',
  32. 'python_utils'))
  33. import check_on_pr
  34. def _args():
  35. argp = argparse.ArgumentParser(description='Perform diff on QPS Driver')
  36. argp.add_argument('-d',
  37. '--diff_base',
  38. type=str,
  39. help='Commit or branch to compare the current one to')
  40. argp.add_argument(
  41. '-l',
  42. '--loops',
  43. type=int,
  44. default=4,
  45. help='Number of loops for each benchmark. More loops cuts down on noise'
  46. )
  47. argp.add_argument('-j',
  48. '--jobs',
  49. type=int,
  50. default=multiprocessing.cpu_count(),
  51. help='Number of CPUs to use')
  52. args = argp.parse_args()
  53. assert args.diff_base, "diff_base must be set"
  54. return args
  55. def _make_cmd(jobs):
  56. return ['make', '-j', '%d' % jobs, 'qps_json_driver', 'qps_worker']
  57. def build(name, jobs):
  58. shutil.rmtree('qps_diff_%s' % name, ignore_errors=True)
  59. subprocess.check_call(['git', 'submodule', 'update'])
  60. try:
  61. subprocess.check_call(_make_cmd(jobs))
  62. except subprocess.CalledProcessError as e:
  63. subprocess.check_call(['make', 'clean'])
  64. subprocess.check_call(_make_cmd(jobs))
  65. os.rename('bins', 'qps_diff_%s' % name)
  66. def _run_cmd(name, scenario, fname):
  67. return [
  68. 'qps_diff_%s/opt/qps_json_driver' % name, '--scenarios_json', scenario,
  69. '--json_file_out', fname
  70. ]
  71. def run(name, scenarios, loops):
  72. for sn in scenarios:
  73. for i in range(0, loops):
  74. fname = "%s.%s.%d.json" % (sn, name, i)
  75. subprocess.check_call(_run_cmd(name, scenarios[sn], fname))
  76. def _load_qps(fname):
  77. try:
  78. with open(fname) as f:
  79. return json.loads(f.read())['qps']
  80. except IOError as e:
  81. print(("IOError occurred reading file: %s" % fname))
  82. return None
  83. except ValueError as e:
  84. print(("ValueError occurred reading file: %s" % fname))
  85. return None
  86. def _median(ary):
  87. assert (len(ary))
  88. ary = sorted(ary)
  89. n = len(ary)
  90. if n % 2 == 0:
  91. return (ary[(n - 1) / 2] + ary[(n - 1) / 2 + 1]) / 2.0
  92. else:
  93. return ary[n / 2]
  94. def diff(scenarios, loops, old, new):
  95. old_data = {}
  96. new_data = {}
  97. # collect data
  98. for sn in scenarios:
  99. old_data[sn] = []
  100. new_data[sn] = []
  101. for i in range(loops):
  102. old_data[sn].append(_load_qps("%s.%s.%d.json" % (sn, old, i)))
  103. new_data[sn].append(_load_qps("%s.%s.%d.json" % (sn, new, i)))
  104. # crunch data
  105. headers = ['Benchmark', 'qps']
  106. rows = []
  107. for sn in scenarios:
  108. mdn_diff = abs(_median(new_data[sn]) - _median(old_data[sn]))
  109. print(('%s: %s=%r %s=%r mdn_diff=%r' %
  110. (sn, new, new_data[sn], old, old_data[sn], mdn_diff)))
  111. s = bm_speedup.speedup(new_data[sn], old_data[sn], 10e-5)
  112. if abs(s) > 3 and mdn_diff > 0.5:
  113. rows.append([sn, '%+d%%' % s])
  114. if rows:
  115. return tabulate.tabulate(rows, headers=headers, floatfmt='+.2f')
  116. else:
  117. return None
  118. def main(args):
  119. build('new', args.jobs)
  120. if args.diff_base:
  121. where_am_i = subprocess.check_output(
  122. ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
  123. subprocess.check_call(['git', 'checkout', args.diff_base])
  124. try:
  125. build('old', args.jobs)
  126. finally:
  127. subprocess.check_call(['git', 'checkout', where_am_i])
  128. subprocess.check_call(['git', 'submodule', 'update'])
  129. run('new', qps_scenarios._SCENARIOS, args.loops)
  130. run('old', qps_scenarios._SCENARIOS, args.loops)
  131. diff_output = diff(qps_scenarios._SCENARIOS, args.loops, 'old', 'new')
  132. if diff_output:
  133. text = '[qps] Performance differences noted:\n%s' % diff_output
  134. else:
  135. text = '[qps] No significant performance differences'
  136. print(('%s' % text))
  137. check_on_pr.check_on_pr('QPS', '```\n%s\n```' % text)
  138. if __name__ == '__main__':
  139. args = _args()
  140. main(args)