scenario_generator_helper.py 4.3 KB

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