Skip to content

Commit 5139a01

Browse files
authored
Migrate the bm_diff benchmarks to python3 (grpc#25619)
* Migrate the bm_diff benchmarks to python3 Includes a requirements.txt pinned at ~2017 versions, when this script was first written. * Replace p2 with p3 scipy/numpy dependencies. * py2->3 for benchmark setup scripts * upgrade pip to resolve python3 cryptography/setuptools-rust problem * re-add jobset import (accidentally removed) * re-add six's urllib import. This file is still used in py2 tests * force py3 in run_if_c_cpp_modified.sh * Fix another instance of subprocess.check_output binary mode * Use the requirements.txt for CI perf environment setup * Try to upgrade PyJWT. (v2.0.0 was problematic, grpc#25053) v2.x makes encode return strs from jwt.encode in both py2 and py3. Previously, py3 would return bytes, and py2 a str. * upgate cryptography lib version requirements for jwt. * Wrap pip requirements specifier in quotes '>=x,<y' * Decode subprocess output once instead of for every line * Revert "Decode subprocess output once instead of for every line" This reverts commit 28d1402. py2 doesn't support the `text` argument to subprocess.check_output. * Address reviewer requests * Pin a valid scipy version * Remove scipy and tabulate dependencies from macos tests
1 parent 5c95dd5 commit 5139a01

21 files changed

+82
-63
lines changed

tools/gce/linux_kokoro_performance_worker_init.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ echo 4096 | sudo tee /proc/sys/kernel/perf_event_mlock_kb
200200
git clone -v https://github.com/brendangregg/FlameGraph ~/FlameGraph
201201

202202
# Install scipy and numpy for benchmarking scripts
203-
sudo apt-get install -y python-scipy python-numpy
203+
sudo apt-get install -y python3-scipy python3-numpy
204204

205205
# Install docker
206206
curl -sSL https://get.docker.com/ | sh

tools/internal_ci/helper_scripts/prepare_build_linux_perf_rc

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
ulimit -n 32768
2020
ulimit -c unlimited
2121

22-
sudo pip install tabulate
22+
python3 -m pip install pip==19.3.1
2323

2424
# Python dependencies for tools/run_tests/python_utils/check_on_pr.py
25-
time python2.7 -m pip install pyjwt cryptography requests --user
25+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
26+
time python3 -m pip install --user -r $DIR/requirements.linux_perf.txt
2627

2728
git submodule update --init

tools/internal_ci/helper_scripts/prepare_build_macos_rc

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ brew config
3737
# Add GCP credentials for BQ access
3838
pip install --user google-api-python-client oauth2client six==1.15.0
3939
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/GrpcTesting-d0eeee2db331.json
40+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
4041

4142
# If this is a PR using RUN_TESTS_FLAGS var, then add flags to filter tests
4243
if [ -n "$KOKORO_GITHUB_PULL_REQUEST_NUMBER" ]; then
@@ -67,7 +68,7 @@ then
6768
time git clone --depth 1 https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/master
6869

6970
# Needed for ios-binary-size
70-
time pip install --user pyyaml pyjwt==1.7.1 pyOpenSSL cryptography requests
71+
time pip install --user -r $DIR/requirements.macos.txt
7172

7273
# Store intermediate build files of ObjC tests into /tmpfs
7374
# TODO(jtattermusch): this has likely been done to avoid running
@@ -84,8 +85,8 @@ fi
8485
if [ "${PREPARE_BUILD_INSTALL_DEPS_PYTHON}" == "true" ]
8586
then
8687
# python
87-
time pip install --user virtualenv
88-
time pip install --user --upgrade Mako tox setuptools==44.1.1 twisted pyyaml pyjwt==1.7.1 pyOpenSSL cryptography requests
88+
time pip install --user -r $DIR/requirements.macos.txt
89+
time pip install --user --upgrade virtualenv Mako tox setuptools==44.1.1 twisted
8990

9091
# Install Python 3.7 if it doesn't exist
9192
if [ ! -f "/usr/local/bin/python3.7" ]; then
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cryptography==3.4.6
2+
PyJWT==2.0.1
3+
requests==2.25.1
4+
scipy==1.5.4
5+
tabulate==0.8.9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cryptography==3.4.6
2+
PyJWT==2.0.1
3+
pyOpenSSL==20.0.1
4+
PyYAML==5.4.1
5+
requests==2.25.1

tools/internal_ci/linux/grpc_performance_profile_daily.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cd $(dirname $0)/../../..
2020

2121
source tools/internal_ci/helper_scripts/prepare_build_linux_perf_rc
2222

23-
CPUS=`python -c 'import multiprocessing; print multiprocessing.cpu_count()'`
23+
CPUS=`python3 -c 'import multiprocessing; print(multiprocessing.cpu_count())'`
2424

2525
./tools/run_tests/start_port_server.py || true
2626

tools/internal_ci/linux/run_if_c_cpp_modified.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ set -ex
2020
# Enter the gRPC repo root
2121
cd $(dirname $0)/../../..
2222

23-
AFFECTS_C_CPP=`python -c 'import os; \
23+
AFFECTS_C_CPP=`python3 -c 'import os; \
2424
import sys; \
2525
sys.path.insert(0, "tools/run_tests/python_utils"); \
2626
import filter_pull_request_tests as filter; \

tools/profiling/bloat/bloat_diff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#

tools/profiling/microbenchmarks/bm2bq.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#
@@ -41,7 +41,7 @@
4141
}
4242

4343
if sys.argv[1] == '--schema':
44-
print ',\n'.join('%s:%s' % (k, t.upper()) for k, t in columns)
44+
print(',\n'.join('%s:%s' % (k, t.upper()) for k, t in columns))
4545
sys.exit(0)
4646

4747
with open(sys.argv[1]) as f:

tools/profiling/microbenchmarks/bm_diff/bm_build.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#
@@ -15,13 +15,13 @@
1515
# limitations under the License.
1616
""" Python utility to build opt and counters benchmarks """
1717

18-
import bm_constants
19-
2018
import argparse
21-
import subprocess
2219
import multiprocessing
2320
import os
2421
import shutil
22+
import subprocess
23+
24+
import bm_constants
2525

2626

2727
def _args():

tools/profiling/microbenchmarks/bm_diff/bm_constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#

tools/profiling/microbenchmarks/bm_diff/bm_diff.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#
@@ -15,20 +15,19 @@
1515
# limitations under the License.
1616
""" Computes the diff between two bm runs and outputs significant results """
1717

18-
import bm_constants
19-
import bm_speedup
20-
21-
import sys
18+
import argparse
19+
import collections
20+
import json
2221
import os
22+
import subprocess
23+
import sys
2324

2425
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..'))
25-
import bm_json
2626

27-
import json
27+
import bm_constants
28+
import bm_json
29+
import bm_speedup
2830
import tabulate
29-
import argparse
30-
import collections
31-
import subprocess
3231

3332
verbose = False
3433

@@ -38,9 +37,9 @@ def _median(ary):
3837
ary = sorted(ary)
3938
n = len(ary)
4039
if n % 2 == 0:
41-
return (ary[(n - 1) / 2] + ary[(n - 1) / 2 + 1]) / 2.0
40+
return (ary[(n - 1) // 2] + ary[(n - 1) // 2 + 1]) / 2.0
4241
else:
43-
return ary[n / 2]
42+
return ary[n // 2]
4443

4544

4645
def _args():
@@ -91,7 +90,7 @@ def _args():
9190

9291
def _maybe_print(str):
9392
if verbose:
94-
print str
93+
print(str)
9594

9695

9796
class Benchmark:
@@ -136,14 +135,14 @@ def _read_json(filename, badjson_files, nonexistant_files):
136135
with open(filename) as f:
137136
r = f.read()
138137
return json.loads(r)
139-
except IOError, e:
138+
except IOError as e:
140139
if stripped in nonexistant_files:
141140
nonexistant_files[stripped] += 1
142141
else:
143142
nonexistant_files[stripped] = 1
144143
return None
145-
except ValueError, e:
146-
print r
144+
except ValueError as e:
145+
print(r)
147146
if stripped in badjson_files:
148147
badjson_files[stripped] += 1
149148
else:
@@ -166,6 +165,7 @@ def diff(bms, loops, regex, track, old, new, counters):
166165
'bm_diff_%s/opt/%s' % (old, bm), '--benchmark_list_tests',
167166
'--benchmark_filter=%s' % regex
168167
]).splitlines():
168+
line = line.decode('UTF-8')
169169
stripped_line = line.strip().replace("/", "_").replace(
170170
"<", "_").replace(">", "_").replace(", ", "_")
171171
js_new_opt = _read_json(

tools/profiling/microbenchmarks/bm_diff/bm_main.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#
@@ -15,26 +15,26 @@
1515
# limitations under the License.
1616
""" Runs the entire bm_*.py pipeline, and possible comments on the PR """
1717

18-
import bm_constants
19-
import bm_build
20-
import bm_run
21-
import bm_diff
22-
23-
import sys
24-
import os
25-
import random
2618
import argparse
2719
import multiprocessing
20+
import os
21+
import random
2822
import subprocess
23+
import sys
2924

3025
sys.path.append(
3126
os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'run_tests',
3227
'python_utils'))
33-
import check_on_pr
3428

3529
sys.path.append(
3630
os.path.join(os.path.dirname(sys.argv[0]), '..', '..', '..', 'run_tests',
3731
'python_utils'))
32+
33+
import bm_build
34+
import bm_constants
35+
import bm_diff
36+
import bm_run
37+
import check_on_pr
3838
import jobset
3939

4040

tools/profiling/microbenchmarks/bm_diff/bm_run.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#
@@ -15,20 +15,20 @@
1515
# limitations under the License.
1616
""" Python utility to run opt and counters benchmarks and save json output """
1717

18-
import bm_constants
19-
2018
import argparse
21-
import subprocess
19+
import itertools
2220
import multiprocessing
21+
import os
2322
import random
24-
import itertools
23+
import subprocess
2524
import sys
26-
import os
25+
26+
import bm_constants
27+
import jobset
2728

2829
sys.path.append(
2930
os.path.join(os.path.dirname(sys.argv[0]), '..', '..', '..', 'run_tests',
3031
'python_utils'))
31-
import jobset
3232

3333

3434
def _args():
@@ -70,7 +70,8 @@ def _args():
7070
args = argp.parse_args()
7171
assert args.name
7272
if args.loops < 3:
73-
print "WARNING: This run will likely be noisy. Increase loops to at least 3."
73+
print("WARNING: This run will likely be noisy. Increase loops to at "
74+
"least 3.")
7475
return args
7576

7677

@@ -80,6 +81,7 @@ def _collect_bm_data(bm, cfg, name, regex, idx, loops):
8081
'bm_diff_%s/%s/%s' % (name, cfg, bm), '--benchmark_list_tests',
8182
'--benchmark_filter=%s' % regex
8283
]).splitlines():
84+
line = line.decode('UTF-8')
8385
stripped_line = line.strip().replace("/",
8486
"_").replace("<", "_").replace(
8587
">", "_").replace(", ", "_")

tools/profiling/microbenchmarks/bm_diff/bm_speedup.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2.7
1+
#!/usr/bin/env python3
22
#
33
# Copyright 2017 gRPC authors.
44
#
@@ -14,9 +14,10 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from scipy import stats
1817
import math
1918

19+
from scipy import stats
20+
2021
_DEFAULT_THRESHOLD = 1e-10
2122

2223

@@ -63,5 +64,5 @@ def speedup(new, old, threshold=_DEFAULT_THRESHOLD):
6364
if __name__ == "__main__":
6465
new = [0.0, 0.0, 0.0, 0.0]
6566
old = [2.96608e-06, 3.35076e-06, 3.45384e-06, 3.34407e-06]
66-
print speedup(new, old, 1e-5)
67-
print speedup(old, new, 1e-5)
67+
print(speedup(new, old, 1e-5))
68+
print(speedup(old, new, 1e-5))

tools/profiling/microbenchmarks/bm_json.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
# Copyright 2017 gRPC authors.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");

tools/run_tests/python_utils/check_on_pr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _access_token():
5555
url='https://api.github.com/app/installations/%s/access_tokens'
5656
% _INSTALLATION_ID,
5757
headers={
58-
'Authorization': 'Bearer %s' % _jwt_token().decode('ASCII'),
58+
'Authorization': 'Bearer %s' % _jwt_token(),
5959
'Accept': 'application/vnd.github.machine-man-preview+json',
6060
})
6161

tools/run_tests/python_utils/filter_pull_request_tests.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import re
2020
import six
21-
from subprocess import check_output
21+
import subprocess
2222

2323

2424
class TestSuite:
@@ -126,10 +126,11 @@ def _get_changed_files(base_branch):
126126
"""
127127
# Get file changes between branch and merge-base of specified branch
128128
# Not combined to be Windows friendly
129-
base_commit = check_output(["git", "merge-base", base_branch,
130-
"HEAD"]).rstrip()
131-
return check_output(["git", "diff", base_commit, "--name-only",
132-
"HEAD"]).splitlines()
129+
base_commit = subprocess.check_output(
130+
["git", "merge-base", base_branch, "HEAD"]).decode("UTF-8").rstrip()
131+
return subprocess.check_output(
132+
["git", "diff", base_commit, "--name-only",
133+
"HEAD"]).decode("UTF-8").splitlines()
133134

134135

135136
def _can_skip_tests(file_names, triggers):

tools/run_tests/python_utils/start_port_server.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
# Copyright 2015 gRPC authors.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");

tools/run_tests/run_microbenchmark.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# Copyright 2017 gRPC authors.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -96,6 +96,7 @@ def collect_latency(bm_name, args):
9696
'bazel-bin/test/cpp/microbenchmarks/%s' % bm_name,
9797
'--benchmark_list_tests'
9898
]).splitlines():
99+
line = line.decode('UTF-8')
99100
link(line, '%s.txt' % fnize(line))
100101
benchmarks.append(
101102
jobset.JobSpec([
@@ -150,6 +151,7 @@ def collect_perf(bm_name, args):
150151
'bazel-bin/test/cpp/microbenchmarks/%s' % bm_name,
151152
'--benchmark_list_tests'
152153
]).splitlines():
154+
line = line.decode('UTF-8')
153155
link(line, '%s.svg' % fnize(line))
154156
benchmarks.append(
155157
jobset.JobSpec([

0 commit comments

Comments
 (0)