Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | # Copyright 2016, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Run performance tests locally or remotely.""" |
| 32 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 33 | from __future__ import print_function |
| 34 | |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 35 | import argparse |
Craig Tiller | accf16b | 2016-09-15 09:08:32 -0700 | [diff] [blame] | 36 | import collections |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 37 | import itertools |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 38 | import jobset |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 39 | import json |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 40 | import multiprocessing |
| 41 | import os |
Craig Tiller | accf16b | 2016-09-15 09:08:32 -0700 | [diff] [blame] | 42 | import performance.scenario_config as scenario_config |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 43 | import pipes |
Jan Tattermusch | 38becc2 | 2016-04-14 08:00:35 -0700 | [diff] [blame] | 44 | import re |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 45 | import subprocess |
| 46 | import sys |
| 47 | import tempfile |
| 48 | import time |
Jan Tattermusch | ee9032c | 2016-04-14 08:35:51 -0700 | [diff] [blame] | 49 | import traceback |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 50 | import uuid |
| 51 | |
| 52 | |
| 53 | _ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..')) |
| 54 | os.chdir(_ROOT) |
| 55 | |
| 56 | |
| 57 | _REMOTE_HOST_USERNAME = 'jenkins' |
| 58 | |
| 59 | |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 60 | class QpsWorkerJob: |
| 61 | """Encapsulates a qps worker server job.""" |
| 62 | |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 63 | def __init__(self, spec, language, host_and_port): |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 64 | self._spec = spec |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 65 | self.language = language |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 66 | self.host_and_port = host_and_port |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 67 | self._job = None |
| 68 | |
| 69 | def start(self): |
Craig Tiller | c197ec1 | 2016-09-15 09:19:33 -0700 | [diff] [blame^] | 70 | self._job = jobset.Job(self._spec, newline_on_success=True, travis=True, add_env={}) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 71 | |
| 72 | def is_running(self): |
| 73 | """Polls a job and returns True if given job is still running.""" |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 74 | return self._job and self._job.state() == jobset._RUNNING |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 75 | |
| 76 | def kill(self): |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 77 | if self._job: |
| 78 | self._job.kill() |
| 79 | self._job = None |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 80 | |
| 81 | |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 82 | def create_qpsworker_job(language, shortname=None, |
| 83 | port=10000, remote_host=None): |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 84 | cmdline = language.worker_cmdline() + ['--driver_port=%s' % port] |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 85 | if remote_host: |
| 86 | user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host) |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 87 | cmdline = ['ssh', |
| 88 | str(user_at_host), |
| 89 | 'cd ~/performance_workspace/grpc/ && %s' % ' '.join(cmdline)] |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 90 | host_and_port='%s:%s' % (remote_host, port) |
| 91 | else: |
| 92 | host_and_port='localhost:%s' % port |
| 93 | |
Jan Tattermusch | 38becc2 | 2016-04-14 08:00:35 -0700 | [diff] [blame] | 94 | # TODO(jtattermusch): with some care, we can calculate the right timeout |
| 95 | # of a worker from the sum of warmup + benchmark times for all the scenarios |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 96 | jobspec = jobset.JobSpec( |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 97 | cmdline=cmdline, |
| 98 | shortname=shortname, |
Jan Tattermusch | 541d5d7 | 2016-05-05 16:08:49 -0700 | [diff] [blame] | 99 | timeout_seconds=2*60*60) |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 100 | return QpsWorkerJob(jobspec, language, host_and_port) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 101 | |
| 102 | |
Jan Tattermusch | 6d7fa55 | 2016-04-14 17:42:54 -0700 | [diff] [blame] | 103 | def create_scenario_jobspec(scenario_json, workers, remote_host=None, |
| 104 | bq_result_table=None): |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 105 | """Runs one scenario using QPS driver.""" |
| 106 | # setting QPS_WORKERS env variable here makes sure it works with SSH too. |
Jan Tattermusch | 6d7fa55 | 2016-04-14 17:42:54 -0700 | [diff] [blame] | 107 | cmd = 'QPS_WORKERS="%s" ' % ','.join(workers) |
| 108 | if bq_result_table: |
| 109 | cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table |
| 110 | cmd += 'tools/run_tests/performance/run_qps_driver.sh ' |
| 111 | cmd += '--scenarios_json=%s ' % pipes.quote(json.dumps({'scenarios': [scenario_json]})) |
Vijay Pai | c23d33b | 2016-07-19 11:19:12 -0700 | [diff] [blame] | 112 | cmd += '--scenario_result_file=scenario_result.json' |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 113 | if remote_host: |
| 114 | user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host) |
Jan Tattermusch | 6d7fa55 | 2016-04-14 17:42:54 -0700 | [diff] [blame] | 115 | cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd)) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 116 | |
| 117 | return jobset.JobSpec( |
| 118 | cmdline=[cmd], |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 119 | shortname='qps_json_driver.%s' % scenario_json['name'], |
| 120 | timeout_seconds=3*60, |
| 121 | shell=True, |
| 122 | verbose_success=True) |
| 123 | |
| 124 | |
| 125 | def create_quit_jobspec(workers, remote_host=None): |
| 126 | """Runs quit using QPS driver.""" |
| 127 | # setting QPS_WORKERS env variable here makes sure it works with SSH too. |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 128 | cmd = 'QPS_WORKERS="%s" bins/opt/qps_json_driver --quit' % ','.join(workers) |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 129 | if remote_host: |
| 130 | user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host) |
Jan Tattermusch | 6d7fa55 | 2016-04-14 17:42:54 -0700 | [diff] [blame] | 131 | cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd)) |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 132 | |
| 133 | return jobset.JobSpec( |
| 134 | cmdline=[cmd], |
vjpai | 29089c7 | 2016-04-20 12:38:16 -0700 | [diff] [blame] | 135 | shortname='qps_json_driver.quit', |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 136 | timeout_seconds=3*60, |
| 137 | shell=True, |
| 138 | verbose_success=True) |
| 139 | |
| 140 | |
Jan Tattermusch | 4de2c32 | 2016-05-10 14:33:07 -0700 | [diff] [blame] | 141 | def create_netperf_jobspec(server_host='localhost', client_host=None, |
| 142 | bq_result_table=None): |
| 143 | """Runs netperf benchmark.""" |
| 144 | cmd = 'NETPERF_SERVER_HOST="%s" ' % server_host |
| 145 | if bq_result_table: |
| 146 | cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table |
Jan Tattermusch | ad17bf7 | 2016-05-11 12:41:37 -0700 | [diff] [blame] | 147 | if client_host: |
| 148 | # If netperf is running remotely, the env variables populated by Jenkins |
| 149 | # won't be available on the client, but we need them for uploading results |
| 150 | # to BigQuery. |
| 151 | jenkins_job_name = os.getenv('JOB_NAME') |
| 152 | if jenkins_job_name: |
| 153 | cmd += 'JOB_NAME="%s" ' % jenkins_job_name |
| 154 | jenkins_build_number = os.getenv('BUILD_NUMBER') |
| 155 | if jenkins_build_number: |
| 156 | cmd += 'BUILD_NUMBER="%s" ' % jenkins_build_number |
| 157 | |
Jan Tattermusch | 4de2c32 | 2016-05-10 14:33:07 -0700 | [diff] [blame] | 158 | cmd += 'tools/run_tests/performance/run_netperf.sh' |
| 159 | if client_host: |
| 160 | user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, client_host) |
| 161 | cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd)) |
| 162 | |
| 163 | return jobset.JobSpec( |
| 164 | cmdline=[cmd], |
| 165 | shortname='netperf', |
| 166 | timeout_seconds=60, |
| 167 | shell=True, |
| 168 | verbose_success=True) |
| 169 | |
| 170 | |
Jan Tattermusch | de874a1 | 2016-04-18 09:21:37 -0700 | [diff] [blame] | 171 | def archive_repo(languages): |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 172 | """Archives local version of repo including submodules.""" |
Jan Tattermusch | de874a1 | 2016-04-18 09:21:37 -0700 | [diff] [blame] | 173 | cmdline=['tar', '-cf', '../grpc.tar', '../grpc/'] |
| 174 | if 'java' in languages: |
| 175 | cmdline.append('../grpc-java') |
| 176 | if 'go' in languages: |
| 177 | cmdline.append('../grpc-go') |
| 178 | |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 179 | archive_job = jobset.JobSpec( |
Jan Tattermusch | de874a1 | 2016-04-18 09:21:37 -0700 | [diff] [blame] | 180 | cmdline=cmdline, |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 181 | shortname='archive_repo', |
| 182 | timeout_seconds=3*60) |
| 183 | |
| 184 | jobset.message('START', 'Archiving local repository.', do_newline=True) |
| 185 | num_failures, _ = jobset.run( |
| 186 | [archive_job], newline_on_success=True, maxjobs=1) |
| 187 | if num_failures == 0: |
| 188 | jobset.message('SUCCESS', |
Jan Tattermusch | de874a1 | 2016-04-18 09:21:37 -0700 | [diff] [blame] | 189 | 'Archive with local repository created successfully.', |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 190 | do_newline=True) |
| 191 | else: |
| 192 | jobset.message('FAILED', 'Failed to archive local repository.', |
| 193 | do_newline=True) |
| 194 | sys.exit(1) |
| 195 | |
| 196 | |
Jan Tattermusch | 1408920 | 2016-04-27 17:55:27 -0700 | [diff] [blame] | 197 | def prepare_remote_hosts(hosts, prepare_local=False): |
| 198 | """Prepares remote hosts (and maybe prepare localhost as well).""" |
| 199 | prepare_timeout = 5*60 |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 200 | prepare_jobs = [] |
| 201 | for host in hosts: |
| 202 | user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host) |
| 203 | prepare_jobs.append( |
| 204 | jobset.JobSpec( |
| 205 | cmdline=['tools/run_tests/performance/remote_host_prepare.sh'], |
| 206 | shortname='remote_host_prepare.%s' % host, |
| 207 | environ = {'USER_AT_HOST': user_at_host}, |
Jan Tattermusch | 1408920 | 2016-04-27 17:55:27 -0700 | [diff] [blame] | 208 | timeout_seconds=prepare_timeout)) |
| 209 | if prepare_local: |
| 210 | # Prepare localhost as well |
| 211 | prepare_jobs.append( |
| 212 | jobset.JobSpec( |
| 213 | cmdline=['tools/run_tests/performance/kill_workers.sh'], |
| 214 | shortname='local_prepare', |
| 215 | timeout_seconds=prepare_timeout)) |
| 216 | jobset.message('START', 'Preparing hosts.', do_newline=True) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 217 | num_failures, _ = jobset.run( |
| 218 | prepare_jobs, newline_on_success=True, maxjobs=10) |
| 219 | if num_failures == 0: |
| 220 | jobset.message('SUCCESS', |
Jan Tattermusch | 1408920 | 2016-04-27 17:55:27 -0700 | [diff] [blame] | 221 | 'Prepare step completed successfully.', |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 222 | do_newline=True) |
| 223 | else: |
| 224 | jobset.message('FAILED', 'Failed to prepare remote hosts.', |
| 225 | do_newline=True) |
| 226 | sys.exit(1) |
| 227 | |
| 228 | |
Craig Tiller | d92d5c5 | 2016-04-04 13:49:29 -0700 | [diff] [blame] | 229 | def build_on_remote_hosts(hosts, languages=scenario_config.LANGUAGES.keys(), build_local=False): |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 230 | """Builds performance worker on remote hosts (and maybe also locally).""" |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 231 | build_timeout = 15*60 |
| 232 | build_jobs = [] |
| 233 | for host in hosts: |
| 234 | user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host) |
| 235 | build_jobs.append( |
| 236 | jobset.JobSpec( |
Craig Tiller | 7797e3f | 2016-04-01 07:41:05 -0700 | [diff] [blame] | 237 | cmdline=['tools/run_tests/performance/remote_host_build.sh'] + languages, |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 238 | shortname='remote_host_build.%s' % host, |
| 239 | environ = {'USER_AT_HOST': user_at_host, 'CONFIG': 'opt'}, |
| 240 | timeout_seconds=build_timeout)) |
| 241 | if build_local: |
| 242 | # Build locally as well |
| 243 | build_jobs.append( |
| 244 | jobset.JobSpec( |
Craig Tiller | 7797e3f | 2016-04-01 07:41:05 -0700 | [diff] [blame] | 245 | cmdline=['tools/run_tests/performance/build_performance.sh'] + languages, |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 246 | shortname='local_build', |
| 247 | environ = {'CONFIG': 'opt'}, |
| 248 | timeout_seconds=build_timeout)) |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 249 | jobset.message('START', 'Building.', do_newline=True) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 250 | num_failures, _ = jobset.run( |
| 251 | build_jobs, newline_on_success=True, maxjobs=10) |
| 252 | if num_failures == 0: |
| 253 | jobset.message('SUCCESS', |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 254 | 'Built successfully.', |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 255 | do_newline=True) |
| 256 | else: |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 257 | jobset.message('FAILED', 'Build failed.', |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 258 | do_newline=True) |
| 259 | sys.exit(1) |
| 260 | |
| 261 | |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 262 | def create_qpsworkers(languages, worker_hosts): |
| 263 | """Creates QPS workers (but does not start them).""" |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 264 | if not worker_hosts: |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 265 | # run two workers locally (for each language) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 266 | workers=[(None, 10000), (None, 10010)] |
| 267 | elif len(worker_hosts) == 1: |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 268 | # run two workers on the remote host (for each language) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 269 | workers=[(worker_hosts[0], 10000), (worker_hosts[0], 10010)] |
| 270 | else: |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 271 | # run one worker per each remote host (for each language) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 272 | workers=[(worker_host, 10000) for worker_host in worker_hosts] |
| 273 | |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 274 | return [create_qpsworker_job(language, |
| 275 | shortname= 'qps_worker_%s_%s' % (language, |
| 276 | worker_idx), |
| 277 | port=worker[1] + language.worker_port_offset(), |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 278 | remote_host=worker[0]) |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 279 | for language in languages |
| 280 | for worker_idx, worker in enumerate(workers)] |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 281 | |
| 282 | |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 283 | Scenario = collections.namedtuple('Scenario', 'jobspec workers') |
| 284 | |
| 285 | |
Jan Tattermusch | 6d7fa55 | 2016-04-14 17:42:54 -0700 | [diff] [blame] | 286 | def create_scenarios(languages, workers_by_lang, remote_host=None, regex='.*', |
Jan Tattermusch | 4de2c32 | 2016-05-10 14:33:07 -0700 | [diff] [blame] | 287 | category='all', bq_result_table=None, |
| 288 | netperf=False, netperf_hosts=[]): |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 289 | """Create jobspecs for scenarios to run.""" |
Ken Payson | 0482c10 | 2016-04-19 12:08:34 -0700 | [diff] [blame] | 290 | all_workers = [worker |
| 291 | for workers in workers_by_lang.values() |
| 292 | for worker in workers] |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 293 | scenarios = [] |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 294 | _NO_WORKERS = [] |
Jan Tattermusch | 4de2c32 | 2016-05-10 14:33:07 -0700 | [diff] [blame] | 295 | |
| 296 | if netperf: |
| 297 | if not netperf_hosts: |
| 298 | netperf_server='localhost' |
| 299 | netperf_client=None |
| 300 | elif len(netperf_hosts) == 1: |
| 301 | netperf_server=netperf_hosts[0] |
| 302 | netperf_client=netperf_hosts[0] |
| 303 | else: |
| 304 | netperf_server=netperf_hosts[0] |
| 305 | netperf_client=netperf_hosts[1] |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 306 | scenarios.append(Scenario( |
| 307 | create_netperf_jobspec(server_host=netperf_server, |
| 308 | client_host=netperf_client, |
| 309 | bq_result_table=bq_result_table), |
| 310 | _NO_WORKERS)) |
Jan Tattermusch | 4de2c32 | 2016-05-10 14:33:07 -0700 | [diff] [blame] | 311 | |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 312 | for language in languages: |
Craig Tiller | 0bda0b3 | 2016-03-03 12:51:53 -0800 | [diff] [blame] | 313 | for scenario_json in language.scenarios(): |
Jan Tattermusch | 38becc2 | 2016-04-14 08:00:35 -0700 | [diff] [blame] | 314 | if re.search(args.regex, scenario_json['name']): |
Craig Tiller | c2895d2 | 2016-09-09 15:22:21 -0700 | [diff] [blame] | 315 | categories = scenario_json.get('CATEGORIES', []) |
| 316 | if category in categories or (category == 'all' and categories != ['sweep']): |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 317 | workers = workers_by_lang[str(language)][:] |
Jan Tattermusch | 427699b | 2016-05-05 18:10:14 -0700 | [diff] [blame] | 318 | # 'SERVER_LANGUAGE' is an indicator for this script to pick |
| 319 | # a server in different language. |
| 320 | custom_server_lang = scenario_json.get('SERVER_LANGUAGE', None) |
Jan Tattermusch | 37a907e | 2016-05-13 13:49:43 -0700 | [diff] [blame] | 321 | custom_client_lang = scenario_json.get('CLIENT_LANGUAGE', None) |
Jan Tattermusch | 427699b | 2016-05-05 18:10:14 -0700 | [diff] [blame] | 322 | scenario_json = scenario_config.remove_nonproto_fields(scenario_json) |
Jan Tattermusch | 37a907e | 2016-05-13 13:49:43 -0700 | [diff] [blame] | 323 | if custom_server_lang and custom_client_lang: |
| 324 | raise Exception('Cannot set both custom CLIENT_LANGUAGE and SERVER_LANGUAGE' |
| 325 | 'in the same scenario') |
Jan Tattermusch | 427699b | 2016-05-05 18:10:14 -0700 | [diff] [blame] | 326 | if custom_server_lang: |
| 327 | if not workers_by_lang.get(custom_server_lang, []): |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 328 | print('Warning: Skipping scenario %s as' % scenario_json['name']) |
Jan Tattermusch | 427699b | 2016-05-05 18:10:14 -0700 | [diff] [blame] | 329 | print('SERVER_LANGUAGE is set to %s yet the language has ' |
| 330 | 'not been selected with -l' % custom_server_lang) |
| 331 | continue |
| 332 | for idx in range(0, scenario_json['num_servers']): |
| 333 | # replace first X workers by workers of a different language |
| 334 | workers[idx] = workers_by_lang[custom_server_lang][idx] |
Jan Tattermusch | 37a907e | 2016-05-13 13:49:43 -0700 | [diff] [blame] | 335 | if custom_client_lang: |
| 336 | if not workers_by_lang.get(custom_client_lang, []): |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 337 | print('Warning: Skipping scenario %s as' % scenario_json['name']) |
Jan Tattermusch | 37a907e | 2016-05-13 13:49:43 -0700 | [diff] [blame] | 338 | print('CLIENT_LANGUAGE is set to %s yet the language has ' |
| 339 | 'not been selected with -l' % custom_client_lang) |
| 340 | continue |
| 341 | for idx in range(scenario_json['num_servers'], len(workers)): |
| 342 | # replace all client workers by workers of a different language, |
| 343 | # leave num_server workers as they are server workers. |
| 344 | workers[idx] = workers_by_lang[custom_client_lang][idx] |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 345 | scenario = Scenario( |
| 346 | create_scenario_jobspec(scenario_json, |
| 347 | [w.host_and_port for w in workers], |
| 348 | remote_host=remote_host, |
| 349 | bq_result_table=bq_result_table), |
| 350 | workers) |
Jan Tattermusch | 427699b | 2016-05-05 18:10:14 -0700 | [diff] [blame] | 351 | scenarios.append(scenario) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 352 | |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 353 | return scenarios |
| 354 | |
| 355 | |
| 356 | def finish_qps_workers(jobs): |
| 357 | """Waits for given jobs to finish and eventually kills them.""" |
| 358 | retries = 0 |
| 359 | while any(job.is_running() for job in jobs): |
| 360 | for job in qpsworker_jobs: |
| 361 | if job.is_running(): |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 362 | print('QPS worker "%s" is still running.' % job.host_and_port) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 363 | if retries > 10: |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 364 | print('Killing all QPS workers.') |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 365 | for job in jobs: |
| 366 | job.kill() |
| 367 | retries += 1 |
| 368 | time.sleep(3) |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 369 | print('All QPS workers finished.') |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 370 | |
| 371 | |
| 372 | argp = argparse.ArgumentParser(description='Run performance tests.') |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 373 | argp.add_argument('-l', '--language', |
Craig Tiller | d92d5c5 | 2016-04-04 13:49:29 -0700 | [diff] [blame] | 374 | choices=['all'] + sorted(scenario_config.LANGUAGES.keys()), |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 375 | nargs='+', |
Jan Tattermusch | 427699b | 2016-05-05 18:10:14 -0700 | [diff] [blame] | 376 | required=True, |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 377 | help='Languages to benchmark.') |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 378 | argp.add_argument('--remote_driver_host', |
| 379 | default=None, |
| 380 | help='Run QPS driver on given host. By default, QPS driver is run locally.') |
| 381 | argp.add_argument('--remote_worker_host', |
| 382 | nargs='+', |
| 383 | default=[], |
| 384 | help='Worker hosts where to start QPS workers.') |
Jan Tattermusch | 38becc2 | 2016-04-14 08:00:35 -0700 | [diff] [blame] | 385 | argp.add_argument('-r', '--regex', default='.*', type=str, |
| 386 | help='Regex to select scenarios to run.') |
Jan Tattermusch | 6d7fa55 | 2016-04-14 17:42:54 -0700 | [diff] [blame] | 387 | argp.add_argument('--bq_result_table', default=None, type=str, |
| 388 | help='Bigquery "dataset.table" to upload results to.') |
Jan Tattermusch | 427699b | 2016-05-05 18:10:14 -0700 | [diff] [blame] | 389 | argp.add_argument('--category', |
Craig Tiller | 6388da5 | 2016-09-07 17:06:29 -0700 | [diff] [blame] | 390 | choices=['smoketest','all','scalable','sweep'], |
Jan Tattermusch | 37a907e | 2016-05-13 13:49:43 -0700 | [diff] [blame] | 391 | default='all', |
| 392 | help='Select a category of tests to run.') |
Jan Tattermusch | 4de2c32 | 2016-05-10 14:33:07 -0700 | [diff] [blame] | 393 | argp.add_argument('--netperf', |
| 394 | default=False, |
| 395 | action='store_const', |
| 396 | const=True, |
| 397 | help='Run netperf benchmark as one of the scenarios.') |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 398 | |
| 399 | args = argp.parse_args() |
| 400 | |
Craig Tiller | d92d5c5 | 2016-04-04 13:49:29 -0700 | [diff] [blame] | 401 | languages = set(scenario_config.LANGUAGES[l] |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 402 | for l in itertools.chain.from_iterable( |
Craig Tiller | d92d5c5 | 2016-04-04 13:49:29 -0700 | [diff] [blame] | 403 | scenario_config.LANGUAGES.iterkeys() if x == 'all' else [x] |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 404 | for x in args.language)) |
| 405 | |
Jan Tattermusch | 6d7fa55 | 2016-04-14 17:42:54 -0700 | [diff] [blame] | 406 | |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 407 | # Put together set of remote hosts where to run and build |
| 408 | remote_hosts = set() |
| 409 | if args.remote_worker_host: |
| 410 | for host in args.remote_worker_host: |
| 411 | remote_hosts.add(host) |
| 412 | if args.remote_driver_host: |
| 413 | remote_hosts.add(args.remote_driver_host) |
| 414 | |
| 415 | if remote_hosts: |
Jan Tattermusch | de874a1 | 2016-04-18 09:21:37 -0700 | [diff] [blame] | 416 | archive_repo(languages=[str(l) for l in languages]) |
Jan Tattermusch | 1408920 | 2016-04-27 17:55:27 -0700 | [diff] [blame] | 417 | prepare_remote_hosts(remote_hosts, prepare_local=True) |
| 418 | else: |
| 419 | prepare_remote_hosts([], prepare_local=True) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 420 | |
| 421 | build_local = False |
| 422 | if not args.remote_driver_host: |
| 423 | build_local = True |
Craig Tiller | 7797e3f | 2016-04-01 07:41:05 -0700 | [diff] [blame] | 424 | build_on_remote_hosts(remote_hosts, languages=[str(l) for l in languages], build_local=build_local) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 425 | |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 426 | qpsworker_jobs = create_qpsworkers(languages, args.remote_worker_host) |
Jan Tattermusch | 38becc2 | 2016-04-14 08:00:35 -0700 | [diff] [blame] | 427 | |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 428 | # get list of worker addresses for each language. |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 429 | workers_by_lang = dict([(str(language), []) for language in languages]) |
Jan Tattermusch | bb1a453 | 2016-03-30 18:04:01 -0700 | [diff] [blame] | 430 | for job in qpsworker_jobs: |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 431 | workers_by_lang[str(job.language)].append(job) |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 432 | |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 433 | scenarios = create_scenarios(languages, |
Craig Tiller | d82fccc | 2016-09-15 09:15:23 -0700 | [diff] [blame] | 434 | workers_by_lang=workers_by_lang, |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 435 | remote_host=args.remote_driver_host, |
| 436 | regex=args.regex, |
| 437 | category=args.category, |
| 438 | bq_result_table=args.bq_result_table, |
| 439 | netperf=args.netperf, |
| 440 | netperf_hosts=args.remote_worker_host) |
Jan Tattermusch | 4de2c32 | 2016-05-10 14:33:07 -0700 | [diff] [blame] | 441 | |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 442 | if not scenarios: |
| 443 | raise Exception('No scenarios to run') |
Jan Tattermusch | b275844 | 2016-03-28 09:32:20 -0700 | [diff] [blame] | 444 | |
Craig Tiller | c1b54f2 | 2016-09-15 08:57:14 -0700 | [diff] [blame] | 445 | for scenario in scenarios: |
| 446 | try: |
| 447 | for worker in scenario.workers: |
| 448 | worker.start() |
| 449 | jobset.run([scenario.jobspec, |
| 450 | create_quit_jobspec(scenario.workers, remote_host=remote_host)], |
| 451 | newline_on_success=True, maxjobs=1) |
| 452 | finally: |
| 453 | finish_qps_workers(scenario.workers) |