Debian Codegraph @

test/cpp/qps/scenario_generator_helper.py

raw
1 #!/usr/bin/env python3
2
3 # Copyright 2021 The 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 from __future__ import print_function
18
19 import json
20 import os
21 import sys
22
23 import yaml
24
25 run_tests_root = os.path.abspath(
26 os.path.join(os.path.dirname(sys.argv[0]), '../../../tools/run_tests'))
27 sys.path.append(run_tests_root)
28
29 import performance.scenario_config as scenario_config
30
31 _COPYRIGHT = """# Copyright 2021 The gRPC Authors
32 #
33 # Licensed under the Apache License, Version 2.0 (the "License");
34 # you may not use this file except in compliance with the License.
35 # You may obtain a copy of the License at
36 #
37 # http://www.apache.org/licenses/LICENSE-2.0
38 #
39 # Unless required by applicable law or agreed to in writing, software
40 # distributed under the License is distributed on an "AS IS" BASIS,
41 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 # See the License for the specific language governing permissions and
43 # limitations under the License.
44 """
45
46
47 def _mutate_scenario(scenario_json):
48 """Modifies vanilla benchmark scenario config to make it more suitable for running as a unit test."""
49 # tweak parameters to get fast test times
50 scenario_json = dict(scenario_json)
51 scenario_json['warmup_seconds'] = 0
52 scenario_json['benchmark_seconds'] = 1
53 outstanding_rpcs_divisor = 1
54 if scenario_json['client_config'][
55 'client_type'] == 'SYNC_CLIENT' or scenario_json['server_config'][
56 'server_type'] == 'SYNC_SERVER':
57 # reduce the number of threads needed for scenarios that use synchronous API
58 outstanding_rpcs_divisor = 10
59 scenario_json['client_config']['outstanding_rpcs_per_channel'] = max(
60 1, scenario_json['client_config']['outstanding_rpcs_per_channel'] //
61 outstanding_rpcs_divisor)
62 # Some scenarios use high channel count since when actually
63 # benchmarking, we want to saturate the machine that runs the benchmark.
64 # For unit test, this is an overkill.
65 max_client_channels = 16
66 if scenario_json['client_config']['rpc_type'] == 'STREAMING_FROM_SERVER':
67 # streaming from server scenarios tend to have trouble shutting down
68 # quickly if there are too many channels.
69 max_client_channels = 4
70
71 scenario_json['client_config']['client_channels'] = min(
72 max_client_channels, scenario_json['client_config']['client_channels'])
73
74 return scenario_config.remove_nonproto_fields(scenario_json)
75
76
77 def generate_json_run_localhost_scenarios():
78 return [
79 _mutate_scenario(scenario_json)
80 for scenario_json in scenario_config.CXXLanguage().scenarios()
81 if 'scalable' in scenario_json.get('CATEGORIES', [])
82 ]
83
84
85 def generate_qps_json_driver_scenarios():
86 return [
87 _mutate_scenario(scenario_json)
88 for scenario_json in scenario_config.CXXLanguage().scenarios()
89 if 'inproc' in scenario_json.get('CATEGORIES', [])
90 ]
91
92
93 def generate_scenarios_bzl(json_scenarios, bzl_filename, bzl_variablename):
94 """Generate .bzl file that defines a variable with JSON scenario configs."""
95 all_scenarios = []
96 for scenario in json_scenarios:
97 scenario_name = scenario['name']
98 # argument will be passed as "--scenarios_json" to the test binary
99 # the string needs to be quoted in \' to ensure it gets passed as a single argument in shell
100 scenarios_json_arg_str = '\\\'%s\\\'' % json.dumps(
101 {'scenarios': [scenario]})
102 all_scenarios.append((scenario_name, scenarios_json_arg_str))
103
104 with open(bzl_filename, 'w') as f:
105 f.write(_COPYRIGHT)
106 f.write(
107 '"""AUTOGENERATED: configuration of benchmark scenarios to be run as bazel test"""\n\n'
108 )
109 f.write('%s = {\n' % bzl_variablename)
110 for scenario in all_scenarios:
111 f.write(" \"%s\": '%s',\n" % (scenario[0], scenario[1]))
112 f.write('}\n')