blob: 35d20be5b75d2abf58b37ed55316cf930fcbfd90 [file] [log] [blame]
Siddharth Shukla8e64d902017-03-12 19:50:18 +01001#!/usr/bin/env python
Jan Tattermuschb2758442016-03-28 09:32:20 -07002# 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
siddharthshukla0589e532016-07-07 16:08:01 +020033from __future__ import print_function
34
Jan Tattermuschb2758442016-03-28 09:32:20 -070035import argparse
Craig Tilleraccf16b2016-09-15 09:08:32 -070036import collections
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070037import itertools
Craig Tiller0bda0b32016-03-03 12:51:53 -080038import json
Jan Tattermuschb2758442016-03-28 09:32:20 -070039import multiprocessing
40import os
Craig Tiller0bda0b32016-03-03 12:51:53 -080041import pipes
Jan Tattermusch38becc22016-04-14 08:00:35 -070042import re
Jan Tattermuschb2758442016-03-28 09:32:20 -070043import subprocess
44import sys
45import tempfile
46import time
Jan Tattermuschee9032c2016-04-14 08:35:51 -070047import traceback
Jan Tattermuschb2758442016-03-28 09:32:20 -070048import uuid
Siddharth Shuklad194f592017-03-11 19:12:43 +010049import six
Jan Tattermusch5c79a312016-12-20 11:02:50 +010050
51import performance.scenario_config as scenario_config
52import python_utils.jobset as jobset
53import python_utils.report_utils as report_utils
Jan Tattermuschb2758442016-03-28 09:32:20 -070054
55
56_ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
57os.chdir(_ROOT)
58
59
60_REMOTE_HOST_USERNAME = 'jenkins'
61
62
Jan Tattermuschb2758442016-03-28 09:32:20 -070063class QpsWorkerJob:
64 """Encapsulates a qps worker server job."""
65
Alexander Polcyn9f08d112016-10-24 12:25:02 -070066 def __init__(self, spec, language, host_and_port, perf_file_base_name=None):
Jan Tattermuschb2758442016-03-28 09:32:20 -070067 self._spec = spec
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070068 self.language = language
Jan Tattermuschb2758442016-03-28 09:32:20 -070069 self.host_and_port = host_and_port
Craig Tillerc1b54f22016-09-15 08:57:14 -070070 self._job = None
Alexander Polcyn9f08d112016-10-24 12:25:02 -070071 self.perf_file_base_name = perf_file_base_name
Craig Tillerc1b54f22016-09-15 08:57:14 -070072
73 def start(self):
Craig Tillerc197ec12016-09-15 09:19:33 -070074 self._job = jobset.Job(self._spec, newline_on_success=True, travis=True, add_env={})
Jan Tattermuschb2758442016-03-28 09:32:20 -070075
76 def is_running(self):
77 """Polls a job and returns True if given job is still running."""
Craig Tillerc1b54f22016-09-15 08:57:14 -070078 return self._job and self._job.state() == jobset._RUNNING
Jan Tattermuschb2758442016-03-28 09:32:20 -070079
80 def kill(self):
Craig Tillerc1b54f22016-09-15 08:57:14 -070081 if self._job:
82 self._job.kill()
83 self._job = None
Jan Tattermuschb2758442016-03-28 09:32:20 -070084
85
Alexander Polcyn9f08d112016-10-24 12:25:02 -070086def create_qpsworker_job(language, shortname=None, port=10000, remote_host=None, perf_cmd=None):
87 cmdline = (language.worker_cmdline() + ['--driver_port=%s' % port])
88
Jan Tattermuschb2758442016-03-28 09:32:20 -070089 if remote_host:
Jan Tattermuschb2758442016-03-28 09:32:20 -070090 host_and_port='%s:%s' % (remote_host, port)
91 else:
92 host_and_port='localhost:%s' % port
93
Alexander Polcyn9f08d112016-10-24 12:25:02 -070094 perf_file_base_name = None
95 if perf_cmd:
96 perf_file_base_name = '%s-%s' % (host_and_port, shortname)
97 # specify -o output file so perf.data gets collected when worker stopped
98 cmdline = perf_cmd + ['-o', '%s-perf.data' % perf_file_base_name] + cmdline
99
Alexander Polcyn76be3062017-02-01 12:06:23 -0800100 worker_timeout = 3 * 60
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700101 if remote_host:
102 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
103 ssh_cmd = ['ssh']
Alexander Polcyn76be3062017-02-01 12:06:23 -0800104 cmdline = ['timeout', '%s' % (worker_timeout + 30)] + cmdline
Craig Tiller69218202017-03-07 15:54:33 -0800105 ssh_cmd.extend([str(user_at_host), 'cd ~/performance_workspace/grpc/ && tools/run_tests/start_port_server.py && %s' % ' '.join(cmdline)])
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700106 cmdline = ssh_cmd
107
Jan Tattermuschb2758442016-03-28 09:32:20 -0700108 jobspec = jobset.JobSpec(
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700109 cmdline=cmdline,
110 shortname=shortname,
Alexander Polcyn76be3062017-02-01 12:06:23 -0800111 timeout_seconds=worker_timeout, # workers get restarted after each scenario
Jan Tattermusch447548b2016-10-17 12:04:56 +0200112 verbose_success=True)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700113 return QpsWorkerJob(jobspec, language, host_and_port, perf_file_base_name)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700114
115
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700116def create_scenario_jobspec(scenario_json, workers, remote_host=None,
Yuxuan Liac87a462016-11-11 12:05:11 -0800117 bq_result_table=None, server_cpu_load=0):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700118 """Runs one scenario using QPS driver."""
119 # setting QPS_WORKERS env variable here makes sure it works with SSH too.
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700120 cmd = 'QPS_WORKERS="%s" ' % ','.join(workers)
121 if bq_result_table:
122 cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table
123 cmd += 'tools/run_tests/performance/run_qps_driver.sh '
124 cmd += '--scenarios_json=%s ' % pipes.quote(json.dumps({'scenarios': [scenario_json]}))
Yuxuan Liac87a462016-11-11 12:05:11 -0800125 cmd += '--scenario_result_file=scenario_result.json '
126 if server_cpu_load != 0:
127 cmd += '--search_param=offered_load --initial_search_value=1000 --targeted_cpu_load=%d --stride=500 --error_tolerance=0.01' % server_cpu_load
Jan Tattermuschb2758442016-03-28 09:32:20 -0700128 if remote_host:
129 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700130 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Jan Tattermuschb2758442016-03-28 09:32:20 -0700131
132 return jobset.JobSpec(
133 cmdline=[cmd],
Craig Tiller0bda0b32016-03-03 12:51:53 -0800134 shortname='qps_json_driver.%s' % scenario_json['name'],
Yuxuan Liac87a462016-11-11 12:05:11 -0800135 timeout_seconds=12*60,
Craig Tiller0bda0b32016-03-03 12:51:53 -0800136 shell=True,
137 verbose_success=True)
138
139
140def create_quit_jobspec(workers, remote_host=None):
141 """Runs quit using QPS driver."""
142 # setting QPS_WORKERS env variable here makes sure it works with SSH too.
Craig Tiller025972d2016-09-15 09:26:50 -0700143 cmd = 'QPS_WORKERS="%s" bins/opt/qps_json_driver --quit' % ','.join(w.host_and_port for w in workers)
Craig Tiller0bda0b32016-03-03 12:51:53 -0800144 if remote_host:
145 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700146 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Craig Tiller0bda0b32016-03-03 12:51:53 -0800147
148 return jobset.JobSpec(
149 cmdline=[cmd],
vjpai29089c72016-04-20 12:38:16 -0700150 shortname='qps_json_driver.quit',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700151 timeout_seconds=3*60,
152 shell=True,
153 verbose_success=True)
154
155
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700156def create_netperf_jobspec(server_host='localhost', client_host=None,
157 bq_result_table=None):
158 """Runs netperf benchmark."""
159 cmd = 'NETPERF_SERVER_HOST="%s" ' % server_host
160 if bq_result_table:
161 cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table
Jan Tattermuschad17bf72016-05-11 12:41:37 -0700162 if client_host:
163 # If netperf is running remotely, the env variables populated by Jenkins
164 # won't be available on the client, but we need them for uploading results
165 # to BigQuery.
166 jenkins_job_name = os.getenv('JOB_NAME')
167 if jenkins_job_name:
168 cmd += 'JOB_NAME="%s" ' % jenkins_job_name
169 jenkins_build_number = os.getenv('BUILD_NUMBER')
170 if jenkins_build_number:
171 cmd += 'BUILD_NUMBER="%s" ' % jenkins_build_number
172
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700173 cmd += 'tools/run_tests/performance/run_netperf.sh'
174 if client_host:
175 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, client_host)
176 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
177
178 return jobset.JobSpec(
179 cmdline=[cmd],
180 shortname='netperf',
181 timeout_seconds=60,
182 shell=True,
183 verbose_success=True)
184
185
Jan Tattermuschde874a12016-04-18 09:21:37 -0700186def archive_repo(languages):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700187 """Archives local version of repo including submodules."""
Jan Tattermuschde874a12016-04-18 09:21:37 -0700188 cmdline=['tar', '-cf', '../grpc.tar', '../grpc/']
189 if 'java' in languages:
190 cmdline.append('../grpc-java')
191 if 'go' in languages:
192 cmdline.append('../grpc-go')
193
Jan Tattermuschb2758442016-03-28 09:32:20 -0700194 archive_job = jobset.JobSpec(
Jan Tattermuschde874a12016-04-18 09:21:37 -0700195 cmdline=cmdline,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700196 shortname='archive_repo',
197 timeout_seconds=3*60)
198
199 jobset.message('START', 'Archiving local repository.', do_newline=True)
200 num_failures, _ = jobset.run(
201 [archive_job], newline_on_success=True, maxjobs=1)
202 if num_failures == 0:
203 jobset.message('SUCCESS',
Jan Tattermuschde874a12016-04-18 09:21:37 -0700204 'Archive with local repository created successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700205 do_newline=True)
206 else:
207 jobset.message('FAILED', 'Failed to archive local repository.',
208 do_newline=True)
209 sys.exit(1)
210
211
Jan Tattermusch14089202016-04-27 17:55:27 -0700212def prepare_remote_hosts(hosts, prepare_local=False):
213 """Prepares remote hosts (and maybe prepare localhost as well)."""
214 prepare_timeout = 5*60
Jan Tattermuschb2758442016-03-28 09:32:20 -0700215 prepare_jobs = []
216 for host in hosts:
217 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host)
218 prepare_jobs.append(
219 jobset.JobSpec(
220 cmdline=['tools/run_tests/performance/remote_host_prepare.sh'],
221 shortname='remote_host_prepare.%s' % host,
222 environ = {'USER_AT_HOST': user_at_host},
Jan Tattermusch14089202016-04-27 17:55:27 -0700223 timeout_seconds=prepare_timeout))
224 if prepare_local:
225 # Prepare localhost as well
226 prepare_jobs.append(
227 jobset.JobSpec(
228 cmdline=['tools/run_tests/performance/kill_workers.sh'],
229 shortname='local_prepare',
230 timeout_seconds=prepare_timeout))
231 jobset.message('START', 'Preparing hosts.', do_newline=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700232 num_failures, _ = jobset.run(
233 prepare_jobs, newline_on_success=True, maxjobs=10)
234 if num_failures == 0:
235 jobset.message('SUCCESS',
Jan Tattermusch14089202016-04-27 17:55:27 -0700236 'Prepare step completed successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700237 do_newline=True)
238 else:
239 jobset.message('FAILED', 'Failed to prepare remote hosts.',
240 do_newline=True)
241 sys.exit(1)
242
243
Craig Tillerd92d5c52016-04-04 13:49:29 -0700244def build_on_remote_hosts(hosts, languages=scenario_config.LANGUAGES.keys(), build_local=False):
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700245 """Builds performance worker on remote hosts (and maybe also locally)."""
Jan Tattermuschb2758442016-03-28 09:32:20 -0700246 build_timeout = 15*60
247 build_jobs = []
248 for host in hosts:
249 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host)
250 build_jobs.append(
251 jobset.JobSpec(
Craig Tiller7797e3f2016-04-01 07:41:05 -0700252 cmdline=['tools/run_tests/performance/remote_host_build.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700253 shortname='remote_host_build.%s' % host,
Craig Tiller0a3d5f92017-03-01 17:08:39 -0800254 environ = {'USER_AT_HOST': user_at_host, 'CONFIG': 'opt'},
Jan Tattermuschb2758442016-03-28 09:32:20 -0700255 timeout_seconds=build_timeout))
256 if build_local:
257 # Build locally as well
258 build_jobs.append(
259 jobset.JobSpec(
Craig Tiller7797e3f2016-04-01 07:41:05 -0700260 cmdline=['tools/run_tests/performance/build_performance.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700261 shortname='local_build',
Craig Tiller0a3d5f92017-03-01 17:08:39 -0800262 environ = {'CONFIG': 'opt'},
Jan Tattermuschb2758442016-03-28 09:32:20 -0700263 timeout_seconds=build_timeout))
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700264 jobset.message('START', 'Building.', do_newline=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700265 num_failures, _ = jobset.run(
266 build_jobs, newline_on_success=True, maxjobs=10)
267 if num_failures == 0:
268 jobset.message('SUCCESS',
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700269 'Built successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700270 do_newline=True)
271 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700272 jobset.message('FAILED', 'Build failed.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700273 do_newline=True)
274 sys.exit(1)
275
276
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700277def create_qpsworkers(languages, worker_hosts, perf_cmd=None):
Craig Tillerc1b54f22016-09-15 08:57:14 -0700278 """Creates QPS workers (but does not start them)."""
Jan Tattermuschb2758442016-03-28 09:32:20 -0700279 if not worker_hosts:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700280 # run two workers locally (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700281 workers=[(None, 10000), (None, 10010)]
282 elif len(worker_hosts) == 1:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700283 # run two workers on the remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700284 workers=[(worker_hosts[0], 10000), (worker_hosts[0], 10010)]
285 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700286 # run one worker per each remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700287 workers=[(worker_host, 10000) for worker_host in worker_hosts]
288
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700289 return [create_qpsworker_job(language,
290 shortname= 'qps_worker_%s_%s' % (language,
291 worker_idx),
292 port=worker[1] + language.worker_port_offset(),
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700293 remote_host=worker[0],
294 perf_cmd=perf_cmd)
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700295 for language in languages
296 for worker_idx, worker in enumerate(workers)]
Jan Tattermuschb2758442016-03-28 09:32:20 -0700297
298
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700299def perf_report_processor_job(worker_host, perf_base_name, output_filename):
300 print('Creating perf report collection job for %s' % worker_host)
301 cmd = ''
302 if worker_host != 'localhost':
303 user_at_host = "%s@%s" % (_REMOTE_HOST_USERNAME, worker_host)
304 cmd = "USER_AT_HOST=%s OUTPUT_FILENAME=%s OUTPUT_DIR=%s PERF_BASE_NAME=%s\
305 tools/run_tests/performance/process_remote_perf_flamegraphs.sh" \
Alexander Polcyn66c67822016-12-09 10:22:50 -0800306 % (user_at_host, output_filename, args.flame_graph_reports, perf_base_name)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700307 else:
308 cmd = "OUTPUT_FILENAME=%s OUTPUT_DIR=%s PERF_BASE_NAME=%s\
309 tools/run_tests/performance/process_local_perf_flamegraphs.sh" \
Alexander Polcyn66c67822016-12-09 10:22:50 -0800310 % (output_filename, args.flame_graph_reports, perf_base_name)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700311
312 return jobset.JobSpec(cmdline=cmd,
313 timeout_seconds=3*60,
314 shell=True,
315 verbose_success=True,
316 shortname='process perf report')
317
318
Craig Tiller677966a2016-09-26 07:37:28 -0700319Scenario = collections.namedtuple('Scenario', 'jobspec workers name')
Craig Tillerc1b54f22016-09-15 08:57:14 -0700320
321
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700322def create_scenarios(languages, workers_by_lang, remote_host=None, regex='.*',
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700323 category='all', bq_result_table=None,
Yuxuan Liac87a462016-11-11 12:05:11 -0800324 netperf=False, netperf_hosts=[], server_cpu_load=0):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700325 """Create jobspecs for scenarios to run."""
Ken Payson0482c102016-04-19 12:08:34 -0700326 all_workers = [worker
327 for workers in workers_by_lang.values()
328 for worker in workers]
Jan Tattermuschb2758442016-03-28 09:32:20 -0700329 scenarios = []
Craig Tillerc1b54f22016-09-15 08:57:14 -0700330 _NO_WORKERS = []
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700331
332 if netperf:
333 if not netperf_hosts:
334 netperf_server='localhost'
335 netperf_client=None
336 elif len(netperf_hosts) == 1:
337 netperf_server=netperf_hosts[0]
338 netperf_client=netperf_hosts[0]
339 else:
340 netperf_server=netperf_hosts[0]
341 netperf_client=netperf_hosts[1]
Craig Tillerc1b54f22016-09-15 08:57:14 -0700342 scenarios.append(Scenario(
343 create_netperf_jobspec(server_host=netperf_server,
344 client_host=netperf_client,
345 bq_result_table=bq_result_table),
Craig Tiller677966a2016-09-26 07:37:28 -0700346 _NO_WORKERS, 'netperf'))
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700347
Jan Tattermuschb2758442016-03-28 09:32:20 -0700348 for language in languages:
Craig Tiller0bda0b32016-03-03 12:51:53 -0800349 for scenario_json in language.scenarios():
Jan Tattermusch38becc22016-04-14 08:00:35 -0700350 if re.search(args.regex, scenario_json['name']):
Craig Tillerb6df2472016-09-13 09:41:26 -0700351 categories = scenario_json.get('CATEGORIES', ['scalable', 'smoketest'])
352 if category in categories or category == 'all':
Craig Tillerc1b54f22016-09-15 08:57:14 -0700353 workers = workers_by_lang[str(language)][:]
Jan Tattermusch427699b2016-05-05 18:10:14 -0700354 # 'SERVER_LANGUAGE' is an indicator for this script to pick
355 # a server in different language.
356 custom_server_lang = scenario_json.get('SERVER_LANGUAGE', None)
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700357 custom_client_lang = scenario_json.get('CLIENT_LANGUAGE', None)
Jan Tattermusch427699b2016-05-05 18:10:14 -0700358 scenario_json = scenario_config.remove_nonproto_fields(scenario_json)
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700359 if custom_server_lang and custom_client_lang:
360 raise Exception('Cannot set both custom CLIENT_LANGUAGE and SERVER_LANGUAGE'
361 'in the same scenario')
Jan Tattermusch427699b2016-05-05 18:10:14 -0700362 if custom_server_lang:
363 if not workers_by_lang.get(custom_server_lang, []):
siddharthshukla0589e532016-07-07 16:08:01 +0200364 print('Warning: Skipping scenario %s as' % scenario_json['name'])
Jan Tattermusch427699b2016-05-05 18:10:14 -0700365 print('SERVER_LANGUAGE is set to %s yet the language has '
366 'not been selected with -l' % custom_server_lang)
367 continue
368 for idx in range(0, scenario_json['num_servers']):
369 # replace first X workers by workers of a different language
370 workers[idx] = workers_by_lang[custom_server_lang][idx]
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700371 if custom_client_lang:
372 if not workers_by_lang.get(custom_client_lang, []):
siddharthshukla0589e532016-07-07 16:08:01 +0200373 print('Warning: Skipping scenario %s as' % scenario_json['name'])
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700374 print('CLIENT_LANGUAGE is set to %s yet the language has '
375 'not been selected with -l' % custom_client_lang)
376 continue
377 for idx in range(scenario_json['num_servers'], len(workers)):
378 # replace all client workers by workers of a different language,
379 # leave num_server workers as they are server workers.
380 workers[idx] = workers_by_lang[custom_client_lang][idx]
Craig Tillerc1b54f22016-09-15 08:57:14 -0700381 scenario = Scenario(
382 create_scenario_jobspec(scenario_json,
383 [w.host_and_port for w in workers],
384 remote_host=remote_host,
Yuxuan Liac87a462016-11-11 12:05:11 -0800385 bq_result_table=bq_result_table,
386 server_cpu_load=server_cpu_load),
Craig Tiller677966a2016-09-26 07:37:28 -0700387 workers,
388 scenario_json['name'])
Jan Tattermusch427699b2016-05-05 18:10:14 -0700389 scenarios.append(scenario)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700390
Jan Tattermuschb2758442016-03-28 09:32:20 -0700391 return scenarios
392
393
394def finish_qps_workers(jobs):
395 """Waits for given jobs to finish and eventually kills them."""
396 retries = 0
Alexander Polcyn49796672016-10-17 10:01:37 -0700397 num_killed = 0
Jan Tattermuschb2758442016-03-28 09:32:20 -0700398 while any(job.is_running() for job in jobs):
399 for job in qpsworker_jobs:
400 if job.is_running():
siddharthshukla0589e532016-07-07 16:08:01 +0200401 print('QPS worker "%s" is still running.' % job.host_and_port)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700402 if retries > 10:
siddharthshukla0589e532016-07-07 16:08:01 +0200403 print('Killing all QPS workers.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700404 for job in jobs:
405 job.kill()
Alexander Polcyn49796672016-10-17 10:01:37 -0700406 num_killed += 1
Jan Tattermuschb2758442016-03-28 09:32:20 -0700407 retries += 1
408 time.sleep(3)
siddharthshukla0589e532016-07-07 16:08:01 +0200409 print('All QPS workers finished.')
Alexander Polcyn49796672016-10-17 10:01:37 -0700410 return num_killed
Jan Tattermuschb2758442016-03-28 09:32:20 -0700411
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700412profile_output_files = []
413
414# Collect perf text reports and flamegraphs if perf_cmd was used
415# Note the base names of perf text reports are used when creating and processing
416# perf data. The scenario name uniqifies the output name in the final
417# perf reports directory.
418# Alos, the perf profiles need to be fetched and processed after each scenario
419# in order to avoid clobbering the output files.
420def run_collect_perf_profile_jobs(hosts_and_base_names, scenario_name):
421 perf_report_jobs = []
422 global profile_output_files
423 for host_and_port in hosts_and_base_names:
424 perf_base_name = hosts_and_base_names[host_and_port]
425 output_filename = '%s-%s' % (scenario_name, perf_base_name)
426 # from the base filename, create .svg output filename
427 host = host_and_port.split(':')[0]
428 profile_output_files.append('%s.svg' % output_filename)
429 perf_report_jobs.append(perf_report_processor_job(host, perf_base_name, output_filename))
430
431 jobset.message('START', 'Collecting perf reports from qps workers', do_newline=True)
432 failures, _ = jobset.run(perf_report_jobs, newline_on_success=True, maxjobs=1)
433 jobset.message('END', 'Collecting perf reports from qps workers', do_newline=True)
434 return failures
435
436
Jan Tattermuschb2758442016-03-28 09:32:20 -0700437argp = argparse.ArgumentParser(description='Run performance tests.')
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700438argp.add_argument('-l', '--language',
Craig Tillerd92d5c52016-04-04 13:49:29 -0700439 choices=['all'] + sorted(scenario_config.LANGUAGES.keys()),
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700440 nargs='+',
Jan Tattermusch427699b2016-05-05 18:10:14 -0700441 required=True,
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700442 help='Languages to benchmark.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700443argp.add_argument('--remote_driver_host',
444 default=None,
445 help='Run QPS driver on given host. By default, QPS driver is run locally.')
446argp.add_argument('--remote_worker_host',
447 nargs='+',
448 default=[],
449 help='Worker hosts where to start QPS workers.')
Craig Tiller677966a2016-09-26 07:37:28 -0700450argp.add_argument('--dry_run',
451 default=False,
452 action='store_const',
453 const=True,
454 help='Just list scenarios to be run, but don\'t run them.')
Jan Tattermusch38becc22016-04-14 08:00:35 -0700455argp.add_argument('-r', '--regex', default='.*', type=str,
456 help='Regex to select scenarios to run.')
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700457argp.add_argument('--bq_result_table', default=None, type=str,
458 help='Bigquery "dataset.table" to upload results to.')
Jan Tattermusch427699b2016-05-05 18:10:14 -0700459argp.add_argument('--category',
Craig Tiller6388da52016-09-07 17:06:29 -0700460 choices=['smoketest','all','scalable','sweep'],
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700461 default='all',
462 help='Select a category of tests to run.')
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700463argp.add_argument('--netperf',
464 default=False,
465 action='store_const',
466 const=True,
467 help='Run netperf benchmark as one of the scenarios.')
Yuxuan Liac87a462016-11-11 12:05:11 -0800468argp.add_argument('--server_cpu_load',
469 default=0, type=int,
470 help='Select a targeted server cpu load to run. 0 means ignore this flag')
Jan Tattermusch88818ae2016-11-18 14:21:33 +0100471argp.add_argument('-x', '--xml_report', default='report.xml', type=str,
472 help='Name of XML report file to generate.')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700473argp.add_argument('--perf_args',
474 help=('Example usage: "--perf_args=record -F 99 -g". '
475 'Wrap QPS workers in a perf command '
476 'with the arguments to perf specified here. '
477 '".svg" flame graph profiles will be '
478 'created for each Qps Worker on each scenario. '
Alexander Polcyn66c67822016-12-09 10:22:50 -0800479 'Files will output to "<repo_root>/<args.flame_graph_reports>" '
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700480 'directory. Output files from running the worker '
481 'under perf are saved in the repo root where its ran. '
482 'Note that the perf "-g" flag is necessary for '
483 'flame graphs generation to work (assuming the binary '
484 'being profiled uses frame pointers, check out '
485 '"--call-graph dwarf" option using libunwind otherwise.) '
486 'Also note that the entire "--perf_args=<arg(s)>" must '
487 'be wrapped in quotes as in the example usage. '
488 'If the "--perg_args" is unspecified, "perf" will '
489 'not be used at all. '
490 'See http://www.brendangregg.com/perf.html '
491 'for more general perf examples.'))
492argp.add_argument('--skip_generate_flamegraphs',
493 default=False,
494 action='store_const',
495 const=True,
496 help=('Turn flame graph generation off. '
497 'May be useful if "perf_args" arguments do not make sense for '
498 'generating flamegraphs (e.g., "--perf_args=stat ...")'))
Alexander Polcyn66c67822016-12-09 10:22:50 -0800499argp.add_argument('-f', '--flame_graph_reports', default='perf_reports', type=str,
500 help='Name of directory to output flame graph profiles to, if any are created.')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700501
Jan Tattermuschb2758442016-03-28 09:32:20 -0700502args = argp.parse_args()
503
Craig Tillerd92d5c52016-04-04 13:49:29 -0700504languages = set(scenario_config.LANGUAGES[l]
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700505 for l in itertools.chain.from_iterable(
Siddharth Shuklad194f592017-03-11 19:12:43 +0100506 six.iterkeys(scenario_config.LANGUAGES) if x == 'all'
507 else [x] for x in args.language))
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700508
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700509
Jan Tattermuschb2758442016-03-28 09:32:20 -0700510# Put together set of remote hosts where to run and build
511remote_hosts = set()
512if args.remote_worker_host:
513 for host in args.remote_worker_host:
514 remote_hosts.add(host)
515if args.remote_driver_host:
516 remote_hosts.add(args.remote_driver_host)
517
Craig Tiller677966a2016-09-26 07:37:28 -0700518if not args.dry_run:
519 if remote_hosts:
520 archive_repo(languages=[str(l) for l in languages])
521 prepare_remote_hosts(remote_hosts, prepare_local=True)
522 else:
523 prepare_remote_hosts([], prepare_local=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700524
525build_local = False
526if not args.remote_driver_host:
527 build_local = True
Craig Tiller677966a2016-09-26 07:37:28 -0700528if not args.dry_run:
529 build_on_remote_hosts(remote_hosts, languages=[str(l) for l in languages], build_local=build_local)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700530
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700531perf_cmd = None
532if args.perf_args:
Alexander Polcyn66c67822016-12-09 10:22:50 -0800533 print('Running workers under perf profiler')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700534 # Expect /usr/bin/perf to be installed here, as is usual
Alexander Polcyn66c67822016-12-09 10:22:50 -0800535 perf_cmd = ['/usr/bin/perf']
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700536 perf_cmd.extend(re.split('\s+', args.perf_args))
537
538qpsworker_jobs = create_qpsworkers(languages, args.remote_worker_host, perf_cmd=perf_cmd)
Jan Tattermusch38becc22016-04-14 08:00:35 -0700539
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700540# get list of worker addresses for each language.
Craig Tillerc1b54f22016-09-15 08:57:14 -0700541workers_by_lang = dict([(str(language), []) for language in languages])
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700542for job in qpsworker_jobs:
Craig Tillerc1b54f22016-09-15 08:57:14 -0700543 workers_by_lang[str(job.language)].append(job)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700544
Craig Tillerc1b54f22016-09-15 08:57:14 -0700545scenarios = create_scenarios(languages,
Craig Tillerd82fccc2016-09-15 09:15:23 -0700546 workers_by_lang=workers_by_lang,
Craig Tillerc1b54f22016-09-15 08:57:14 -0700547 remote_host=args.remote_driver_host,
548 regex=args.regex,
549 category=args.category,
550 bq_result_table=args.bq_result_table,
551 netperf=args.netperf,
Yuxuan Liac87a462016-11-11 12:05:11 -0800552 netperf_hosts=args.remote_worker_host,
553 server_cpu_load=args.server_cpu_load)
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700554
Craig Tillerc1b54f22016-09-15 08:57:14 -0700555if not scenarios:
556 raise Exception('No scenarios to run')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700557
Alex Polcyncac93f62016-10-19 09:27:57 -0700558total_scenario_failures = 0
Alexander Polcyn898a2e92016-10-22 17:41:23 -0700559qps_workers_killed = 0
Jan Tattermusch94d40cb2016-10-24 21:06:40 +0200560merged_resultset = {}
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700561perf_report_failures = 0
562
Craig Tillerc1b54f22016-09-15 08:57:14 -0700563for scenario in scenarios:
Craig Tiller677966a2016-09-26 07:37:28 -0700564 if args.dry_run:
565 print(scenario.name)
566 else:
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700567 scenario_failures = 0
Craig Tiller677966a2016-09-26 07:37:28 -0700568 try:
569 for worker in scenario.workers:
570 worker.start()
Alex Polcynfcf09ea2016-12-06 04:00:05 +0000571 jobs = [scenario.jobspec]
Alex Polcynca5e9242016-12-06 04:21:37 +0000572 if scenario.workers:
Alex Polcynfcf09ea2016-12-06 04:00:05 +0000573 jobs.append(create_quit_jobspec(scenario.workers, remote_host=args.remote_driver_host))
574 scenario_failures, resultset = jobset.run(jobs, newline_on_success=True, maxjobs=1)
Alex Polcyncac93f62016-10-19 09:27:57 -0700575 total_scenario_failures += scenario_failures
Siddharth Shuklad194f592017-03-11 19:12:43 +0100576 merged_resultset = dict(itertools.chain(six.iteritems(merged_resultset),
577 six.iteritems(resultset)))
Craig Tiller677966a2016-09-26 07:37:28 -0700578 finally:
Alexander Polcyn898a2e92016-10-22 17:41:23 -0700579 # Consider qps workers that need to be killed as failures
580 qps_workers_killed += finish_qps_workers(scenario.workers)
Alexander Polcyn49796672016-10-17 10:01:37 -0700581
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700582 if perf_cmd and scenario_failures == 0 and not args.skip_generate_flamegraphs:
583 workers_and_base_names = {}
584 for worker in scenario.workers:
585 if not worker.perf_file_base_name:
586 raise Exception('using perf buf perf report filename is unspecified')
587 workers_and_base_names[worker.host_and_port] = worker.perf_file_base_name
588 perf_report_failures += run_collect_perf_profile_jobs(workers_and_base_names, scenario.name)
589
590
591# Still write the index.html even if some scenarios failed.
592# 'profile_output_files' will only have names for scenarios that passed
593if perf_cmd and not args.skip_generate_flamegraphs:
594 # write the index fil to the output dir, with all profiles from all scenarios/workers
Alexander Polcyn66c67822016-12-09 10:22:50 -0800595 report_utils.render_perf_profiling_results('%s/index.html' % args.flame_graph_reports, profile_output_files)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700596
Alexander Polcyn41fe5792017-02-02 10:46:51 -0800597report_utils.render_junit_xml_report(merged_resultset, args.xml_report,
598 suite_name='benchmarks')
599
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700600if total_scenario_failures > 0 or qps_workers_killed > 0:
601 print('%s scenarios failed and %s qps worker jobs killed' % (total_scenario_failures, qps_workers_killed))
602 sys.exit(1)
Jan Tattermusch94d40cb2016-10-24 21:06:40 +0200603
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700604if perf_report_failures > 0:
605 print('%s perf profile collection jobs failed' % perf_report_failures)
Alex Polcyncac93f62016-10-19 09:27:57 -0700606 sys.exit(1)