blob: fb3b0a1afdf4360ed9a6d4f56cbd856376394276 [file] [log] [blame]
Jan Tattermuschb2758442016-03-28 09:32:20 -07001#!/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
33import argparse
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070034import itertools
Jan Tattermuschb2758442016-03-28 09:32:20 -070035import jobset
Craig Tiller0bda0b32016-03-03 12:51:53 -080036import json
Jan Tattermuschb2758442016-03-28 09:32:20 -070037import multiprocessing
38import os
Craig Tiller0bda0b32016-03-03 12:51:53 -080039import pipes
Jan Tattermusch38becc22016-04-14 08:00:35 -070040import re
Jan Tattermuschb2758442016-03-28 09:32:20 -070041import subprocess
42import sys
43import tempfile
44import time
Jan Tattermuschee9032c2016-04-14 08:35:51 -070045import traceback
Jan Tattermuschb2758442016-03-28 09:32:20 -070046import uuid
Craig Tillerd92d5c52016-04-04 13:49:29 -070047import performance.scenario_config as scenario_config
Jan Tattermuschb2758442016-03-28 09:32:20 -070048
49
50_ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
51os.chdir(_ROOT)
52
53
54_REMOTE_HOST_USERNAME = 'jenkins'
55
56
Jan Tattermuschb2758442016-03-28 09:32:20 -070057class QpsWorkerJob:
58 """Encapsulates a qps worker server job."""
59
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070060 def __init__(self, spec, language, host_and_port):
Jan Tattermuschb2758442016-03-28 09:32:20 -070061 self._spec = spec
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070062 self.language = language
Jan Tattermuschb2758442016-03-28 09:32:20 -070063 self.host_and_port = host_and_port
64 self._job = jobset.Job(spec, bin_hash=None, newline_on_success=True, travis=True, add_env={})
65
66 def is_running(self):
67 """Polls a job and returns True if given job is still running."""
68 return self._job.state(jobset.NoCache()) == jobset._RUNNING
69
70 def kill(self):
71 return self._job.kill()
72
73
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070074def create_qpsworker_job(language, shortname=None,
75 port=10000, remote_host=None):
Jan Tattermuschb2758442016-03-28 09:32:20 -070076 # TODO: support more languages
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070077 cmdline = language.worker_cmdline() + ['--driver_port=%s' % port]
Jan Tattermuschb2758442016-03-28 09:32:20 -070078 if remote_host:
79 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070080 cmdline = ['ssh',
81 str(user_at_host),
82 'cd ~/performance_workspace/grpc/ && %s' % ' '.join(cmdline)]
Jan Tattermuschb2758442016-03-28 09:32:20 -070083 host_and_port='%s:%s' % (remote_host, port)
84 else:
85 host_and_port='localhost:%s' % port
86
Jan Tattermusch38becc22016-04-14 08:00:35 -070087 # TODO(jtattermusch): with some care, we can calculate the right timeout
88 # of a worker from the sum of warmup + benchmark times for all the scenarios
Jan Tattermuschb2758442016-03-28 09:32:20 -070089 jobspec = jobset.JobSpec(
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070090 cmdline=cmdline,
91 shortname=shortname,
Jan Tattermusch92091112016-04-20 09:27:00 -070092 timeout_seconds=30*60)
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070093 return QpsWorkerJob(jobspec, language, host_and_port)
Jan Tattermuschb2758442016-03-28 09:32:20 -070094
95
Jan Tattermusch6d7fa552016-04-14 17:42:54 -070096def create_scenario_jobspec(scenario_json, workers, remote_host=None,
97 bq_result_table=None):
Jan Tattermuschb2758442016-03-28 09:32:20 -070098 """Runs one scenario using QPS driver."""
99 # setting QPS_WORKERS env variable here makes sure it works with SSH too.
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700100 cmd = 'QPS_WORKERS="%s" ' % ','.join(workers)
101 if bq_result_table:
102 cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table
103 cmd += 'tools/run_tests/performance/run_qps_driver.sh '
104 cmd += '--scenarios_json=%s ' % pipes.quote(json.dumps({'scenarios': [scenario_json]}))
105 cmd += '--scenario_result_file=scenario_result.json'
Jan Tattermuschb2758442016-03-28 09:32:20 -0700106 if remote_host:
107 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700108 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Jan Tattermuschb2758442016-03-28 09:32:20 -0700109
110 return jobset.JobSpec(
111 cmdline=[cmd],
Craig Tiller0bda0b32016-03-03 12:51:53 -0800112 shortname='qps_json_driver.%s' % scenario_json['name'],
113 timeout_seconds=3*60,
114 shell=True,
115 verbose_success=True)
116
117
118def create_quit_jobspec(workers, remote_host=None):
119 """Runs quit using QPS driver."""
120 # setting QPS_WORKERS env variable here makes sure it works with SSH too.
vjpai29089c72016-04-20 12:38:16 -0700121 cmd = 'QPS_WORKERS="%s" bins/opt/qps_json_driver --quit' % ','.join(workers)
Craig Tiller0bda0b32016-03-03 12:51:53 -0800122 if remote_host:
123 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700124 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Craig Tiller0bda0b32016-03-03 12:51:53 -0800125
126 return jobset.JobSpec(
127 cmdline=[cmd],
vjpai29089c72016-04-20 12:38:16 -0700128 shortname='qps_json_driver.quit',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700129 timeout_seconds=3*60,
130 shell=True,
131 verbose_success=True)
132
133
Jan Tattermuschde874a12016-04-18 09:21:37 -0700134def archive_repo(languages):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700135 """Archives local version of repo including submodules."""
Jan Tattermuschde874a12016-04-18 09:21:37 -0700136 cmdline=['tar', '-cf', '../grpc.tar', '../grpc/']
137 if 'java' in languages:
138 cmdline.append('../grpc-java')
139 if 'go' in languages:
140 cmdline.append('../grpc-go')
141
Jan Tattermuschb2758442016-03-28 09:32:20 -0700142 archive_job = jobset.JobSpec(
Jan Tattermuschde874a12016-04-18 09:21:37 -0700143 cmdline=cmdline,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700144 shortname='archive_repo',
145 timeout_seconds=3*60)
146
147 jobset.message('START', 'Archiving local repository.', do_newline=True)
148 num_failures, _ = jobset.run(
149 [archive_job], newline_on_success=True, maxjobs=1)
150 if num_failures == 0:
151 jobset.message('SUCCESS',
Jan Tattermuschde874a12016-04-18 09:21:37 -0700152 'Archive with local repository created successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700153 do_newline=True)
154 else:
155 jobset.message('FAILED', 'Failed to archive local repository.',
156 do_newline=True)
157 sys.exit(1)
158
159
Jan Tattermusch14089202016-04-27 17:55:27 -0700160def prepare_remote_hosts(hosts, prepare_local=False):
161 """Prepares remote hosts (and maybe prepare localhost as well)."""
162 prepare_timeout = 5*60
Jan Tattermuschb2758442016-03-28 09:32:20 -0700163 prepare_jobs = []
164 for host in hosts:
165 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host)
166 prepare_jobs.append(
167 jobset.JobSpec(
168 cmdline=['tools/run_tests/performance/remote_host_prepare.sh'],
169 shortname='remote_host_prepare.%s' % host,
170 environ = {'USER_AT_HOST': user_at_host},
Jan Tattermusch14089202016-04-27 17:55:27 -0700171 timeout_seconds=prepare_timeout))
172 if prepare_local:
173 # Prepare localhost as well
174 prepare_jobs.append(
175 jobset.JobSpec(
176 cmdline=['tools/run_tests/performance/kill_workers.sh'],
177 shortname='local_prepare',
178 timeout_seconds=prepare_timeout))
179 jobset.message('START', 'Preparing hosts.', do_newline=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700180 num_failures, _ = jobset.run(
181 prepare_jobs, newline_on_success=True, maxjobs=10)
182 if num_failures == 0:
183 jobset.message('SUCCESS',
Jan Tattermusch14089202016-04-27 17:55:27 -0700184 'Prepare step completed successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700185 do_newline=True)
186 else:
187 jobset.message('FAILED', 'Failed to prepare remote hosts.',
188 do_newline=True)
189 sys.exit(1)
190
191
Craig Tillerd92d5c52016-04-04 13:49:29 -0700192def build_on_remote_hosts(hosts, languages=scenario_config.LANGUAGES.keys(), build_local=False):
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700193 """Builds performance worker on remote hosts (and maybe also locally)."""
Jan Tattermuschb2758442016-03-28 09:32:20 -0700194 build_timeout = 15*60
195 build_jobs = []
196 for host in hosts:
197 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host)
198 build_jobs.append(
199 jobset.JobSpec(
Craig Tiller7797e3f2016-04-01 07:41:05 -0700200 cmdline=['tools/run_tests/performance/remote_host_build.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700201 shortname='remote_host_build.%s' % host,
202 environ = {'USER_AT_HOST': user_at_host, 'CONFIG': 'opt'},
203 timeout_seconds=build_timeout))
204 if build_local:
205 # Build locally as well
206 build_jobs.append(
207 jobset.JobSpec(
Craig Tiller7797e3f2016-04-01 07:41:05 -0700208 cmdline=['tools/run_tests/performance/build_performance.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700209 shortname='local_build',
210 environ = {'CONFIG': 'opt'},
211 timeout_seconds=build_timeout))
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700212 jobset.message('START', 'Building.', do_newline=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700213 num_failures, _ = jobset.run(
214 build_jobs, newline_on_success=True, maxjobs=10)
215 if num_failures == 0:
216 jobset.message('SUCCESS',
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700217 'Built successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700218 do_newline=True)
219 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700220 jobset.message('FAILED', 'Build failed.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700221 do_newline=True)
222 sys.exit(1)
223
224
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700225def start_qpsworkers(languages, worker_hosts):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700226 """Starts QPS workers as background jobs."""
227 if not worker_hosts:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700228 # run two workers locally (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700229 workers=[(None, 10000), (None, 10010)]
230 elif len(worker_hosts) == 1:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700231 # run two workers on the remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700232 workers=[(worker_hosts[0], 10000), (worker_hosts[0], 10010)]
233 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700234 # run one worker per each remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700235 workers=[(worker_host, 10000) for worker_host in worker_hosts]
236
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700237 return [create_qpsworker_job(language,
238 shortname= 'qps_worker_%s_%s' % (language,
239 worker_idx),
240 port=worker[1] + language.worker_port_offset(),
Jan Tattermuschb2758442016-03-28 09:32:20 -0700241 remote_host=worker[0])
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700242 for language in languages
243 for worker_idx, worker in enumerate(workers)]
Jan Tattermuschb2758442016-03-28 09:32:20 -0700244
245
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700246def create_scenarios(languages, workers_by_lang, remote_host=None, regex='.*',
247 bq_result_table=None):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700248 """Create jobspecs for scenarios to run."""
249 scenarios = []
250 for language in languages:
Craig Tiller0bda0b32016-03-03 12:51:53 -0800251 for scenario_json in language.scenarios():
Jan Tattermusch38becc22016-04-14 08:00:35 -0700252 if re.search(args.regex, scenario_json['name']):
Jan Tattermuschee9032c2016-04-14 08:35:51 -0700253 workers = workers_by_lang[str(language)]
254 # 'SERVER_LANGUAGE' is an indicator for this script to pick
255 # a server in different language. It doesn't belong to the Scenario
256 # schema, so we also need to remove it.
257 custom_server_lang = scenario_json.pop('SERVER_LANGUAGE', None)
258 if custom_server_lang:
259 if not workers_by_lang.get(custom_server_lang, []):
260 print 'Warning: Skipping scenario %s as' % scenario_json['name']
261 print('SERVER_LANGUAGE is set to %s yet the language has '
262 'not been selected with -l' % custom_server_lang)
263 continue
264 for idx in range(0, scenario_json['num_servers']):
265 # replace first X workers by workers of a different language
266 workers[idx] = workers_by_lang[custom_server_lang][idx]
Jan Tattermusch38becc22016-04-14 08:00:35 -0700267 scenario = create_scenario_jobspec(scenario_json,
Jan Tattermuschee9032c2016-04-14 08:35:51 -0700268 workers,
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700269 remote_host=remote_host,
270 bq_result_table=bq_result_table)
Jan Tattermusch38becc22016-04-14 08:00:35 -0700271 scenarios.append(scenario)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700272
273 # the very last scenario requests shutting down the workers.
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700274 all_workers = [worker
275 for workers in workers_by_lang.values()
276 for worker in workers]
Craig Tiller0bda0b32016-03-03 12:51:53 -0800277 scenarios.append(create_quit_jobspec(all_workers, remote_host=remote_host))
Jan Tattermuschb2758442016-03-28 09:32:20 -0700278 return scenarios
279
280
281def finish_qps_workers(jobs):
282 """Waits for given jobs to finish and eventually kills them."""
283 retries = 0
284 while any(job.is_running() for job in jobs):
285 for job in qpsworker_jobs:
286 if job.is_running():
287 print 'QPS worker "%s" is still running.' % job.host_and_port
288 if retries > 10:
289 print 'Killing all QPS workers.'
290 for job in jobs:
291 job.kill()
292 retries += 1
293 time.sleep(3)
294 print 'All QPS workers finished.'
295
296
297argp = argparse.ArgumentParser(description='Run performance tests.')
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700298argp.add_argument('-l', '--language',
Craig Tillerd92d5c52016-04-04 13:49:29 -0700299 choices=['all'] + sorted(scenario_config.LANGUAGES.keys()),
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700300 nargs='+',
301 default=['all'],
302 help='Languages to benchmark.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700303argp.add_argument('--remote_driver_host',
304 default=None,
305 help='Run QPS driver on given host. By default, QPS driver is run locally.')
306argp.add_argument('--remote_worker_host',
307 nargs='+',
308 default=[],
309 help='Worker hosts where to start QPS workers.')
Jan Tattermusch38becc22016-04-14 08:00:35 -0700310argp.add_argument('-r', '--regex', default='.*', type=str,
311 help='Regex to select scenarios to run.')
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700312argp.add_argument('--bq_result_table', default=None, type=str,
313 help='Bigquery "dataset.table" to upload results to.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700314
315args = argp.parse_args()
316
Craig Tillerd92d5c52016-04-04 13:49:29 -0700317languages = set(scenario_config.LANGUAGES[l]
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700318 for l in itertools.chain.from_iterable(
Craig Tillerd92d5c52016-04-04 13:49:29 -0700319 scenario_config.LANGUAGES.iterkeys() if x == 'all' else [x]
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700320 for x in args.language))
321
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700322
Jan Tattermuschb2758442016-03-28 09:32:20 -0700323# Put together set of remote hosts where to run and build
324remote_hosts = set()
325if args.remote_worker_host:
326 for host in args.remote_worker_host:
327 remote_hosts.add(host)
328if args.remote_driver_host:
329 remote_hosts.add(args.remote_driver_host)
330
331if remote_hosts:
Jan Tattermuschde874a12016-04-18 09:21:37 -0700332 archive_repo(languages=[str(l) for l in languages])
Jan Tattermusch14089202016-04-27 17:55:27 -0700333 prepare_remote_hosts(remote_hosts, prepare_local=True)
334else:
335 prepare_remote_hosts([], prepare_local=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700336
337build_local = False
338if not args.remote_driver_host:
339 build_local = True
Craig Tiller7797e3f2016-04-01 07:41:05 -0700340build_on_remote_hosts(remote_hosts, languages=[str(l) for l in languages], build_local=build_local)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700341
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700342qpsworker_jobs = start_qpsworkers(languages, args.remote_worker_host)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700343
Jan Tattermusch38becc22016-04-14 08:00:35 -0700344# TODO(jtattermusch): see https://github.com/grpc/grpc/issues/6174
345time.sleep(5)
346
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700347# get list of worker addresses for each language.
348worker_addresses = dict([(str(language), []) for language in languages])
349for job in qpsworker_jobs:
350 worker_addresses[str(job.language)].append(job.host_and_port)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700351
352try:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700353 scenarios = create_scenarios(languages,
354 workers_by_lang=worker_addresses,
Jan Tattermusch38becc22016-04-14 08:00:35 -0700355 remote_host=args.remote_driver_host,
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700356 regex=args.regex,
357 bq_result_table=args.bq_result_table)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700358 if not scenarios:
359 raise Exception('No scenarios to run')
360
361 jobset.message('START', 'Running scenarios.', do_newline=True)
362 num_failures, _ = jobset.run(
363 scenarios, newline_on_success=True, maxjobs=1)
364 if num_failures == 0:
365 jobset.message('SUCCESS',
366 'All scenarios finished successfully.',
367 do_newline=True)
368 else:
369 jobset.message('FAILED', 'Some of the scenarios failed.',
370 do_newline=True)
371 sys.exit(1)
Jan Tattermuschee9032c2016-04-14 08:35:51 -0700372except:
373 traceback.print_exc()
Jan Tattermuschb688db02016-04-21 08:53:45 -0700374 raise
Jan Tattermuschb2758442016-03-28 09:32:20 -0700375finally:
376 finish_qps_workers(qpsworker_jobs)