memory_diff.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 #
3 # Copyright 2022 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 
17 import argparse
18 import csv
19 import glob
20 import math
21 import multiprocessing
22 import os
23 import pathlib
24 import re
25 import shutil
26 import subprocess
27 import sys
28 
29 sys.path.append(
30  os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'run_tests',
31  'python_utils'))
32 import check_on_pr
33 
34 argp = argparse.ArgumentParser(description='Perform diff on memory benchmarks')
35 
36 argp.add_argument('-d',
37  '--diff_base',
38  type=str,
39  help='Commit or branch to compare the current one to')
40 
41 argp.add_argument('-j', '--jobs', type=int, default=multiprocessing.cpu_count())
42 
43 args = argp.parse_args()
44 
45 _INTERESTING = {
46  'client call':
47  (rb'client call memory usage: ([0-9\.]+) bytes per call', float),
48  'server call':
49  (rb'server call memory usage: ([0-9\.]+) bytes per call', float),
50 }
51 
52 _SCENARIOS = {
53  'default': [],
54  'minstack': ['--minstack'],
55 }
56 
57 
58 def _run():
59  """Build with Bazel, then run, and extract interesting lines from the output."""
60  subprocess.check_call([
61  'tools/bazel', 'build', '-c', 'opt',
62  'test/core/memory_usage/memory_usage_test'
63  ])
64  ret = {}
65  for scenario, extra_args in _SCENARIOS.items():
66  try:
67  output = subprocess.check_output([
68  'bazel-bin/test/core/memory_usage/memory_usage_test',
69  '--warmup=10000',
70  '--benchmark=50000',
71  ] + extra_args)
72  except subprocess.CalledProcessError as e:
73  print('Error running benchmark:', e)
74  continue
75  for line in output.splitlines():
76  for key, (pattern, conversion) in _INTERESTING.items():
77  m = re.match(pattern, line)
78  if m:
79  ret[scenario + ': ' + key] = conversion(m.group(1))
80  return ret
81 
82 
83 cur = _run()
84 old = None
85 
86 if args.diff_base:
87  where_am_i = subprocess.check_output(
88  ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
89  # checkout the diff base (="old")
90  subprocess.check_call(['git', 'checkout', args.diff_base])
91  try:
92  old = _run()
93  finally:
94  # restore the original revision (="cur")
95  subprocess.check_call(['git', 'checkout', where_am_i])
96 
97 text = ''
98 if old is None:
99  print(cur)
100  for key, value in sorted(cur.items()):
101  text += '{}: {}\n'.format(key, value)
102 else:
103  print(cur, old)
104  diff_size = 0
105  for scenario in _SCENARIOS.keys():
106  for key, value in sorted(_INTERESTING.items()):
107  key = scenario + ': ' + key
108  if key in cur:
109  if key not in old:
110  text += '{}: {}\n'.format(key, cur[key])
111  else:
112  diff_size += cur[key] - old[key]
113  text += '{}: {} -> {}\n'.format(key, old[key], cur[key])
114 
115  print("DIFF_SIZE: %f" % diff_size)
116  check_on_pr.label_increase_decrease_on_pr('per-call-memory', diff_size, 64)
117 
118 print(text)
119 check_on_pr.check_on_pr('Memory Difference', '```\n%s\n```' % text)
http2_test_server.format
format
Definition: http2_test_server.py:118
grpc._common.decode
def decode(b)
Definition: grpc/_common.py:75
memory_diff._run
def _run()
Definition: memory_diff.py:58


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:35