blob: e47c9a40d1cdf412cb6fc49660042916fcb1bd49 [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
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
Jan Tattermusch5c79a312016-12-20 11:02:50 +010049
50import performance.scenario_config as scenario_config
51import python_utils.jobset as jobset
52import python_utils.report_utils as report_utils
Jan Tattermuschb2758442016-03-28 09:32:20 -070053
54
55_ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
56os.chdir(_ROOT)
57
58
59_REMOTE_HOST_USERNAME = 'jenkins'
60
61
Jan Tattermuschb2758442016-03-28 09:32:20 -070062class QpsWorkerJob:
63 """Encapsulates a qps worker server job."""
64
Alexander Polcyn9f08d112016-10-24 12:25:02 -070065 def __init__(self, spec, language, host_and_port, perf_file_base_name=None):
Jan Tattermuschb2758442016-03-28 09:32:20 -070066 self._spec = spec
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070067 self.language = language
Jan Tattermuschb2758442016-03-28 09:32:20 -070068 self.host_and_port = host_and_port
Craig Tillerc1b54f22016-09-15 08:57:14 -070069 self._job = None
Alexander Polcyn9f08d112016-10-24 12:25:02 -070070 self.perf_file_base_name = perf_file_base_name
Craig Tillerc1b54f22016-09-15 08:57:14 -070071
72 def start(self):
Craig Tillerc197ec12016-09-15 09:19:33 -070073 self._job = jobset.Job(self._spec, newline_on_success=True, travis=True, add_env={})
Jan Tattermuschb2758442016-03-28 09:32:20 -070074
75 def is_running(self):
76 """Polls a job and returns True if given job is still running."""
Craig Tillerc1b54f22016-09-15 08:57:14 -070077 return self._job and self._job.state() == jobset._RUNNING
Jan Tattermuschb2758442016-03-28 09:32:20 -070078
79 def kill(self):
Craig Tillerc1b54f22016-09-15 08:57:14 -070080 if self._job:
81 self._job.kill()
82 self._job = None
Jan Tattermuschb2758442016-03-28 09:32:20 -070083
84
Alexander Polcyn9f08d112016-10-24 12:25:02 -070085def create_qpsworker_job(language, shortname=None, port=10000, remote_host=None, perf_cmd=None):
86 cmdline = (language.worker_cmdline() + ['--driver_port=%s' % port])
87
Jan Tattermuschb2758442016-03-28 09:32:20 -070088 if remote_host:
Jan Tattermuschb2758442016-03-28 09:32:20 -070089 host_and_port='%s:%s' % (remote_host, port)
90 else:
91 host_and_port='localhost:%s' % port
92
Alexander Polcyn9f08d112016-10-24 12:25:02 -070093 perf_file_base_name = None
94 if perf_cmd:
95 perf_file_base_name = '%s-%s' % (host_and_port, shortname)
96 # specify -o output file so perf.data gets collected when worker stopped
97 cmdline = perf_cmd + ['-o', '%s-perf.data' % perf_file_base_name] + cmdline
98
99 if remote_host:
100 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
101 ssh_cmd = ['ssh']
102 ssh_cmd.extend([str(user_at_host), 'cd ~/performance_workspace/grpc/ && %s' % ' '.join(cmdline)])
103 cmdline = ssh_cmd
104
Jan Tattermuschb2758442016-03-28 09:32:20 -0700105 jobspec = jobset.JobSpec(
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700106 cmdline=cmdline,
107 shortname=shortname,
Jan Tattermusch447548b2016-10-17 12:04:56 +0200108 timeout_seconds=5*60, # workers get restarted after each scenario
109 verbose_success=True)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700110 return QpsWorkerJob(jobspec, language, host_and_port, perf_file_base_name)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700111
112
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700113def create_scenario_jobspec(scenario_json, workers, remote_host=None,
Yuxuan Liac87a462016-11-11 12:05:11 -0800114 bq_result_table=None, server_cpu_load=0):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700115 """Runs one scenario using QPS driver."""
116 # setting QPS_WORKERS env variable here makes sure it works with SSH too.
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700117 cmd = 'QPS_WORKERS="%s" ' % ','.join(workers)
118 if bq_result_table:
119 cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table
120 cmd += 'tools/run_tests/performance/run_qps_driver.sh '
121 cmd += '--scenarios_json=%s ' % pipes.quote(json.dumps({'scenarios': [scenario_json]}))
Yuxuan Liac87a462016-11-11 12:05:11 -0800122 cmd += '--scenario_result_file=scenario_result.json '
123 if server_cpu_load != 0:
124 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 -0700125 if remote_host:
126 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700127 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Jan Tattermuschb2758442016-03-28 09:32:20 -0700128
129 return jobset.JobSpec(
130 cmdline=[cmd],
Craig Tiller0bda0b32016-03-03 12:51:53 -0800131 shortname='qps_json_driver.%s' % scenario_json['name'],
Yuxuan Liac87a462016-11-11 12:05:11 -0800132 timeout_seconds=12*60,
Craig Tiller0bda0b32016-03-03 12:51:53 -0800133 shell=True,
134 verbose_success=True)
135
136
137def create_quit_jobspec(workers, remote_host=None):
138 """Runs quit using QPS driver."""
139 # setting QPS_WORKERS env variable here makes sure it works with SSH too.
Craig Tiller025972d2016-09-15 09:26:50 -0700140 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 -0800141 if remote_host:
142 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700143 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Craig Tiller0bda0b32016-03-03 12:51:53 -0800144
145 return jobset.JobSpec(
146 cmdline=[cmd],
vjpai29089c72016-04-20 12:38:16 -0700147 shortname='qps_json_driver.quit',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700148 timeout_seconds=3*60,
149 shell=True,
150 verbose_success=True)
151
152
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700153def create_netperf_jobspec(server_host='localhost', client_host=None,
154 bq_result_table=None):
155 """Runs netperf benchmark."""
156 cmd = 'NETPERF_SERVER_HOST="%s" ' % server_host
157 if bq_result_table:
158 cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table
Jan Tattermuschad17bf72016-05-11 12:41:37 -0700159 if client_host:
160 # If netperf is running remotely, the env variables populated by Jenkins
161 # won't be available on the client, but we need them for uploading results
162 # to BigQuery.
163 jenkins_job_name = os.getenv('JOB_NAME')
164 if jenkins_job_name:
165 cmd += 'JOB_NAME="%s" ' % jenkins_job_name
166 jenkins_build_number = os.getenv('BUILD_NUMBER')
167 if jenkins_build_number:
168 cmd += 'BUILD_NUMBER="%s" ' % jenkins_build_number
169
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700170 cmd += 'tools/run_tests/performance/run_netperf.sh'
171 if client_host:
172 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, client_host)
173 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
174
175 return jobset.JobSpec(
176 cmdline=[cmd],
177 shortname='netperf',
178 timeout_seconds=60,
179 shell=True,
180 verbose_success=True)
181
182
Jan Tattermuschde874a12016-04-18 09:21:37 -0700183def archive_repo(languages):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700184 """Archives local version of repo including submodules."""
Jan Tattermuschde874a12016-04-18 09:21:37 -0700185 cmdline=['tar', '-cf', '../grpc.tar', '../grpc/']
186 if 'java' in languages:
187 cmdline.append('../grpc-java')
188 if 'go' in languages:
189 cmdline.append('../grpc-go')
190
Jan Tattermuschb2758442016-03-28 09:32:20 -0700191 archive_job = jobset.JobSpec(
Jan Tattermuschde874a12016-04-18 09:21:37 -0700192 cmdline=cmdline,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700193 shortname='archive_repo',
194 timeout_seconds=3*60)
195
196 jobset.message('START', 'Archiving local repository.', do_newline=True)
197 num_failures, _ = jobset.run(
198 [archive_job], newline_on_success=True, maxjobs=1)
199 if num_failures == 0:
200 jobset.message('SUCCESS',
Jan Tattermuschde874a12016-04-18 09:21:37 -0700201 'Archive with local repository created successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700202 do_newline=True)
203 else:
204 jobset.message('FAILED', 'Failed to archive local repository.',
205 do_newline=True)
206 sys.exit(1)
207
208
Jan Tattermusch14089202016-04-27 17:55:27 -0700209def prepare_remote_hosts(hosts, prepare_local=False):
210 """Prepares remote hosts (and maybe prepare localhost as well)."""
211 prepare_timeout = 5*60
Jan Tattermuschb2758442016-03-28 09:32:20 -0700212 prepare_jobs = []
213 for host in hosts:
214 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host)
215 prepare_jobs.append(
216 jobset.JobSpec(
217 cmdline=['tools/run_tests/performance/remote_host_prepare.sh'],
218 shortname='remote_host_prepare.%s' % host,
219 environ = {'USER_AT_HOST': user_at_host},
Jan Tattermusch14089202016-04-27 17:55:27 -0700220 timeout_seconds=prepare_timeout))
221 if prepare_local:
222 # Prepare localhost as well
223 prepare_jobs.append(
224 jobset.JobSpec(
225 cmdline=['tools/run_tests/performance/kill_workers.sh'],
226 shortname='local_prepare',
227 timeout_seconds=prepare_timeout))
228 jobset.message('START', 'Preparing hosts.', do_newline=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700229 num_failures, _ = jobset.run(
230 prepare_jobs, newline_on_success=True, maxjobs=10)
231 if num_failures == 0:
232 jobset.message('SUCCESS',
Jan Tattermusch14089202016-04-27 17:55:27 -0700233 'Prepare step completed successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700234 do_newline=True)
235 else:
236 jobset.message('FAILED', 'Failed to prepare remote hosts.',
237 do_newline=True)
238 sys.exit(1)
239
240
Craig Tillerd92d5c52016-04-04 13:49:29 -0700241def build_on_remote_hosts(hosts, languages=scenario_config.LANGUAGES.keys(), build_local=False):
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700242 """Builds performance worker on remote hosts (and maybe also locally)."""
Jan Tattermuschb2758442016-03-28 09:32:20 -0700243 build_timeout = 15*60
244 build_jobs = []
245 for host in hosts:
246 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, host)
247 build_jobs.append(
248 jobset.JobSpec(
Craig Tiller7797e3f2016-04-01 07:41:05 -0700249 cmdline=['tools/run_tests/performance/remote_host_build.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700250 shortname='remote_host_build.%s' % host,
251 environ = {'USER_AT_HOST': user_at_host, 'CONFIG': 'opt'},
252 timeout_seconds=build_timeout))
253 if build_local:
254 # Build locally as well
255 build_jobs.append(
256 jobset.JobSpec(
Craig Tiller7797e3f2016-04-01 07:41:05 -0700257 cmdline=['tools/run_tests/performance/build_performance.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700258 shortname='local_build',
259 environ = {'CONFIG': 'opt'},
260 timeout_seconds=build_timeout))
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700261 jobset.message('START', 'Building.', do_newline=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700262 num_failures, _ = jobset.run(
263 build_jobs, newline_on_success=True, maxjobs=10)
264 if num_failures == 0:
265 jobset.message('SUCCESS',
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700266 'Built successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700267 do_newline=True)
268 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700269 jobset.message('FAILED', 'Build failed.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700270 do_newline=True)
271 sys.exit(1)
272
273
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700274def create_qpsworkers(languages, worker_hosts, perf_cmd=None):
Craig Tillerc1b54f22016-09-15 08:57:14 -0700275 """Creates QPS workers (but does not start them)."""
Jan Tattermuschb2758442016-03-28 09:32:20 -0700276 if not worker_hosts:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700277 # run two workers locally (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700278 workers=[(None, 10000), (None, 10010)]
279 elif len(worker_hosts) == 1:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700280 # run two workers on the remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700281 workers=[(worker_hosts[0], 10000), (worker_hosts[0], 10010)]
282 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700283 # run one worker per each remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700284 workers=[(worker_host, 10000) for worker_host in worker_hosts]
285
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700286 return [create_qpsworker_job(language,
287 shortname= 'qps_worker_%s_%s' % (language,
288 worker_idx),
289 port=worker[1] + language.worker_port_offset(),
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700290 remote_host=worker[0],
291 perf_cmd=perf_cmd)
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700292 for language in languages
293 for worker_idx, worker in enumerate(workers)]
Jan Tattermuschb2758442016-03-28 09:32:20 -0700294
295
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700296def perf_report_processor_job(worker_host, perf_base_name, output_filename):
297 print('Creating perf report collection job for %s' % worker_host)
298 cmd = ''
299 if worker_host != 'localhost':
300 user_at_host = "%s@%s" % (_REMOTE_HOST_USERNAME, worker_host)
301 cmd = "USER_AT_HOST=%s OUTPUT_FILENAME=%s OUTPUT_DIR=%s PERF_BASE_NAME=%s\
302 tools/run_tests/performance/process_remote_perf_flamegraphs.sh" \
Alexander Polcyn66c67822016-12-09 10:22:50 -0800303 % (user_at_host, output_filename, args.flame_graph_reports, perf_base_name)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700304 else:
305 cmd = "OUTPUT_FILENAME=%s OUTPUT_DIR=%s PERF_BASE_NAME=%s\
306 tools/run_tests/performance/process_local_perf_flamegraphs.sh" \
Alexander Polcyn66c67822016-12-09 10:22:50 -0800307 % (output_filename, args.flame_graph_reports, perf_base_name)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700308
309 return jobset.JobSpec(cmdline=cmd,
310 timeout_seconds=3*60,
311 shell=True,
312 verbose_success=True,
313 shortname='process perf report')
314
315
Craig Tiller677966a2016-09-26 07:37:28 -0700316Scenario = collections.namedtuple('Scenario', 'jobspec workers name')
Craig Tillerc1b54f22016-09-15 08:57:14 -0700317
318
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700319def create_scenarios(languages, workers_by_lang, remote_host=None, regex='.*',
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700320 category='all', bq_result_table=None,
Yuxuan Liac87a462016-11-11 12:05:11 -0800321 netperf=False, netperf_hosts=[], server_cpu_load=0):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700322 """Create jobspecs for scenarios to run."""
Ken Payson0482c102016-04-19 12:08:34 -0700323 all_workers = [worker
324 for workers in workers_by_lang.values()
325 for worker in workers]
Jan Tattermuschb2758442016-03-28 09:32:20 -0700326 scenarios = []
Craig Tillerc1b54f22016-09-15 08:57:14 -0700327 _NO_WORKERS = []
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700328
329 if netperf:
330 if not netperf_hosts:
331 netperf_server='localhost'
332 netperf_client=None
333 elif len(netperf_hosts) == 1:
334 netperf_server=netperf_hosts[0]
335 netperf_client=netperf_hosts[0]
336 else:
337 netperf_server=netperf_hosts[0]
338 netperf_client=netperf_hosts[1]
Craig Tillerc1b54f22016-09-15 08:57:14 -0700339 scenarios.append(Scenario(
340 create_netperf_jobspec(server_host=netperf_server,
341 client_host=netperf_client,
342 bq_result_table=bq_result_table),
Craig Tiller677966a2016-09-26 07:37:28 -0700343 _NO_WORKERS, 'netperf'))
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700344
Jan Tattermuschb2758442016-03-28 09:32:20 -0700345 for language in languages:
Craig Tiller0bda0b32016-03-03 12:51:53 -0800346 for scenario_json in language.scenarios():
Jan Tattermusch38becc22016-04-14 08:00:35 -0700347 if re.search(args.regex, scenario_json['name']):
Craig Tillerb6df2472016-09-13 09:41:26 -0700348 categories = scenario_json.get('CATEGORIES', ['scalable', 'smoketest'])
349 if category in categories or category == 'all':
Craig Tillerc1b54f22016-09-15 08:57:14 -0700350 workers = workers_by_lang[str(language)][:]
Jan Tattermusch427699b2016-05-05 18:10:14 -0700351 # 'SERVER_LANGUAGE' is an indicator for this script to pick
352 # a server in different language.
353 custom_server_lang = scenario_json.get('SERVER_LANGUAGE', None)
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700354 custom_client_lang = scenario_json.get('CLIENT_LANGUAGE', None)
Jan Tattermusch427699b2016-05-05 18:10:14 -0700355 scenario_json = scenario_config.remove_nonproto_fields(scenario_json)
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700356 if custom_server_lang and custom_client_lang:
357 raise Exception('Cannot set both custom CLIENT_LANGUAGE and SERVER_LANGUAGE'
358 'in the same scenario')
Jan Tattermusch427699b2016-05-05 18:10:14 -0700359 if custom_server_lang:
360 if not workers_by_lang.get(custom_server_lang, []):
siddharthshukla0589e532016-07-07 16:08:01 +0200361 print('Warning: Skipping scenario %s as' % scenario_json['name'])
Jan Tattermusch427699b2016-05-05 18:10:14 -0700362 print('SERVER_LANGUAGE is set to %s yet the language has '
363 'not been selected with -l' % custom_server_lang)
364 continue
365 for idx in range(0, scenario_json['num_servers']):
366 # replace first X workers by workers of a different language
367 workers[idx] = workers_by_lang[custom_server_lang][idx]
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700368 if custom_client_lang:
369 if not workers_by_lang.get(custom_client_lang, []):
siddharthshukla0589e532016-07-07 16:08:01 +0200370 print('Warning: Skipping scenario %s as' % scenario_json['name'])
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700371 print('CLIENT_LANGUAGE is set to %s yet the language has '
372 'not been selected with -l' % custom_client_lang)
373 continue
374 for idx in range(scenario_json['num_servers'], len(workers)):
375 # replace all client workers by workers of a different language,
376 # leave num_server workers as they are server workers.
377 workers[idx] = workers_by_lang[custom_client_lang][idx]
Craig Tillerc1b54f22016-09-15 08:57:14 -0700378 scenario = Scenario(
379 create_scenario_jobspec(scenario_json,
380 [w.host_and_port for w in workers],
381 remote_host=remote_host,
Yuxuan Liac87a462016-11-11 12:05:11 -0800382 bq_result_table=bq_result_table,
383 server_cpu_load=server_cpu_load),
Craig Tiller677966a2016-09-26 07:37:28 -0700384 workers,
385 scenario_json['name'])
Jan Tattermusch427699b2016-05-05 18:10:14 -0700386 scenarios.append(scenario)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700387
Jan Tattermuschb2758442016-03-28 09:32:20 -0700388 return scenarios
389
390
391def finish_qps_workers(jobs):
392 """Waits for given jobs to finish and eventually kills them."""
393 retries = 0
Alexander Polcyn49796672016-10-17 10:01:37 -0700394 num_killed = 0
Jan Tattermuschb2758442016-03-28 09:32:20 -0700395 while any(job.is_running() for job in jobs):
396 for job in qpsworker_jobs:
397 if job.is_running():
siddharthshukla0589e532016-07-07 16:08:01 +0200398 print('QPS worker "%s" is still running.' % job.host_and_port)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700399 if retries > 10:
siddharthshukla0589e532016-07-07 16:08:01 +0200400 print('Killing all QPS workers.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700401 for job in jobs:
402 job.kill()
Alexander Polcyn49796672016-10-17 10:01:37 -0700403 num_killed += 1
Jan Tattermuschb2758442016-03-28 09:32:20 -0700404 retries += 1
405 time.sleep(3)
siddharthshukla0589e532016-07-07 16:08:01 +0200406 print('All QPS workers finished.')
Alexander Polcyn49796672016-10-17 10:01:37 -0700407 return num_killed
Jan Tattermuschb2758442016-03-28 09:32:20 -0700408
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700409profile_output_files = []
410
411# Collect perf text reports and flamegraphs if perf_cmd was used
412# Note the base names of perf text reports are used when creating and processing
413# perf data. The scenario name uniqifies the output name in the final
414# perf reports directory.
415# Alos, the perf profiles need to be fetched and processed after each scenario
416# in order to avoid clobbering the output files.
417def run_collect_perf_profile_jobs(hosts_and_base_names, scenario_name):
418 perf_report_jobs = []
419 global profile_output_files
420 for host_and_port in hosts_and_base_names:
421 perf_base_name = hosts_and_base_names[host_and_port]
422 output_filename = '%s-%s' % (scenario_name, perf_base_name)
423 # from the base filename, create .svg output filename
424 host = host_and_port.split(':')[0]
425 profile_output_files.append('%s.svg' % output_filename)
426 perf_report_jobs.append(perf_report_processor_job(host, perf_base_name, output_filename))
427
428 jobset.message('START', 'Collecting perf reports from qps workers', do_newline=True)
429 failures, _ = jobset.run(perf_report_jobs, newline_on_success=True, maxjobs=1)
430 jobset.message('END', 'Collecting perf reports from qps workers', do_newline=True)
431 return failures
432
433
Jan Tattermuschb2758442016-03-28 09:32:20 -0700434argp = argparse.ArgumentParser(description='Run performance tests.')
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700435argp.add_argument('-l', '--language',
Craig Tillerd92d5c52016-04-04 13:49:29 -0700436 choices=['all'] + sorted(scenario_config.LANGUAGES.keys()),
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700437 nargs='+',
Jan Tattermusch427699b2016-05-05 18:10:14 -0700438 required=True,
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700439 help='Languages to benchmark.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700440argp.add_argument('--remote_driver_host',
441 default=None,
442 help='Run QPS driver on given host. By default, QPS driver is run locally.')
443argp.add_argument('--remote_worker_host',
444 nargs='+',
445 default=[],
446 help='Worker hosts where to start QPS workers.')
Craig Tiller677966a2016-09-26 07:37:28 -0700447argp.add_argument('--dry_run',
448 default=False,
449 action='store_const',
450 const=True,
451 help='Just list scenarios to be run, but don\'t run them.')
Jan Tattermusch38becc22016-04-14 08:00:35 -0700452argp.add_argument('-r', '--regex', default='.*', type=str,
453 help='Regex to select scenarios to run.')
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700454argp.add_argument('--bq_result_table', default=None, type=str,
455 help='Bigquery "dataset.table" to upload results to.')
Jan Tattermusch427699b2016-05-05 18:10:14 -0700456argp.add_argument('--category',
Craig Tiller6388da52016-09-07 17:06:29 -0700457 choices=['smoketest','all','scalable','sweep'],
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700458 default='all',
459 help='Select a category of tests to run.')
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700460argp.add_argument('--netperf',
461 default=False,
462 action='store_const',
463 const=True,
464 help='Run netperf benchmark as one of the scenarios.')
Yuxuan Liac87a462016-11-11 12:05:11 -0800465argp.add_argument('--server_cpu_load',
466 default=0, type=int,
467 help='Select a targeted server cpu load to run. 0 means ignore this flag')
Jan Tattermusch88818ae2016-11-18 14:21:33 +0100468argp.add_argument('-x', '--xml_report', default='report.xml', type=str,
469 help='Name of XML report file to generate.')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700470argp.add_argument('--perf_args',
471 help=('Example usage: "--perf_args=record -F 99 -g". '
472 'Wrap QPS workers in a perf command '
473 'with the arguments to perf specified here. '
474 '".svg" flame graph profiles will be '
475 'created for each Qps Worker on each scenario. '
Alexander Polcyn66c67822016-12-09 10:22:50 -0800476 'Files will output to "<repo_root>/<args.flame_graph_reports>" '
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700477 'directory. Output files from running the worker '
478 'under perf are saved in the repo root where its ran. '
479 'Note that the perf "-g" flag is necessary for '
480 'flame graphs generation to work (assuming the binary '
481 'being profiled uses frame pointers, check out '
482 '"--call-graph dwarf" option using libunwind otherwise.) '
483 'Also note that the entire "--perf_args=<arg(s)>" must '
484 'be wrapped in quotes as in the example usage. '
485 'If the "--perg_args" is unspecified, "perf" will '
486 'not be used at all. '
487 'See http://www.brendangregg.com/perf.html '
488 'for more general perf examples.'))
489argp.add_argument('--skip_generate_flamegraphs',
490 default=False,
491 action='store_const',
492 const=True,
493 help=('Turn flame graph generation off. '
494 'May be useful if "perf_args" arguments do not make sense for '
495 'generating flamegraphs (e.g., "--perf_args=stat ...")'))
Alexander Polcyn66c67822016-12-09 10:22:50 -0800496argp.add_argument('-f', '--flame_graph_reports', default='perf_reports', type=str,
497 help='Name of directory to output flame graph profiles to, if any are created.')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700498
Jan Tattermuschb2758442016-03-28 09:32:20 -0700499args = argp.parse_args()
500
Craig Tillerd92d5c52016-04-04 13:49:29 -0700501languages = set(scenario_config.LANGUAGES[l]
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700502 for l in itertools.chain.from_iterable(
Craig Tillerd92d5c52016-04-04 13:49:29 -0700503 scenario_config.LANGUAGES.iterkeys() if x == 'all' else [x]
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700504 for x in args.language))
505
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700506
Jan Tattermuschb2758442016-03-28 09:32:20 -0700507# Put together set of remote hosts where to run and build
508remote_hosts = set()
509if args.remote_worker_host:
510 for host in args.remote_worker_host:
511 remote_hosts.add(host)
512if args.remote_driver_host:
513 remote_hosts.add(args.remote_driver_host)
514
Craig Tiller677966a2016-09-26 07:37:28 -0700515if not args.dry_run:
516 if remote_hosts:
517 archive_repo(languages=[str(l) for l in languages])
518 prepare_remote_hosts(remote_hosts, prepare_local=True)
519 else:
520 prepare_remote_hosts([], prepare_local=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700521
522build_local = False
523if not args.remote_driver_host:
524 build_local = True
Craig Tiller677966a2016-09-26 07:37:28 -0700525if not args.dry_run:
526 build_on_remote_hosts(remote_hosts, languages=[str(l) for l in languages], build_local=build_local)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700527
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700528perf_cmd = None
529if args.perf_args:
Alexander Polcyn66c67822016-12-09 10:22:50 -0800530 print('Running workers under perf profiler')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700531 # Expect /usr/bin/perf to be installed here, as is usual
Alexander Polcyn66c67822016-12-09 10:22:50 -0800532 perf_cmd = ['/usr/bin/perf']
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700533 perf_cmd.extend(re.split('\s+', args.perf_args))
534
535qpsworker_jobs = create_qpsworkers(languages, args.remote_worker_host, perf_cmd=perf_cmd)
Jan Tattermusch38becc22016-04-14 08:00:35 -0700536
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700537# get list of worker addresses for each language.
Craig Tillerc1b54f22016-09-15 08:57:14 -0700538workers_by_lang = dict([(str(language), []) for language in languages])
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700539for job in qpsworker_jobs:
Craig Tillerc1b54f22016-09-15 08:57:14 -0700540 workers_by_lang[str(job.language)].append(job)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700541
Craig Tillerc1b54f22016-09-15 08:57:14 -0700542scenarios = create_scenarios(languages,
Craig Tillerd82fccc2016-09-15 09:15:23 -0700543 workers_by_lang=workers_by_lang,
Craig Tillerc1b54f22016-09-15 08:57:14 -0700544 remote_host=args.remote_driver_host,
545 regex=args.regex,
546 category=args.category,
547 bq_result_table=args.bq_result_table,
548 netperf=args.netperf,
Yuxuan Liac87a462016-11-11 12:05:11 -0800549 netperf_hosts=args.remote_worker_host,
550 server_cpu_load=args.server_cpu_load)
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700551
Craig Tillerc1b54f22016-09-15 08:57:14 -0700552if not scenarios:
553 raise Exception('No scenarios to run')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700554
Alex Polcyncac93f62016-10-19 09:27:57 -0700555total_scenario_failures = 0
Alexander Polcyn898a2e92016-10-22 17:41:23 -0700556qps_workers_killed = 0
Jan Tattermusch94d40cb2016-10-24 21:06:40 +0200557merged_resultset = {}
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700558perf_report_failures = 0
559
Craig Tillerc1b54f22016-09-15 08:57:14 -0700560for scenario in scenarios:
Craig Tiller677966a2016-09-26 07:37:28 -0700561 if args.dry_run:
562 print(scenario.name)
563 else:
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700564 scenario_failures = 0
Craig Tiller677966a2016-09-26 07:37:28 -0700565 try:
566 for worker in scenario.workers:
567 worker.start()
Alex Polcynfcf09ea2016-12-06 04:00:05 +0000568 jobs = [scenario.jobspec]
Alex Polcynca5e9242016-12-06 04:21:37 +0000569 if scenario.workers:
Alex Polcynfcf09ea2016-12-06 04:00:05 +0000570 jobs.append(create_quit_jobspec(scenario.workers, remote_host=args.remote_driver_host))
571 scenario_failures, resultset = jobset.run(jobs, newline_on_success=True, maxjobs=1)
Alex Polcyncac93f62016-10-19 09:27:57 -0700572 total_scenario_failures += scenario_failures
Jan Tattermusch94d40cb2016-10-24 21:06:40 +0200573 merged_resultset = dict(itertools.chain(merged_resultset.iteritems(),
574 resultset.iteritems()))
Craig Tiller677966a2016-09-26 07:37:28 -0700575 finally:
Alexander Polcyn898a2e92016-10-22 17:41:23 -0700576 # Consider qps workers that need to be killed as failures
577 qps_workers_killed += finish_qps_workers(scenario.workers)
Alexander Polcyn49796672016-10-17 10:01:37 -0700578
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700579 if perf_cmd and scenario_failures == 0 and not args.skip_generate_flamegraphs:
580 workers_and_base_names = {}
581 for worker in scenario.workers:
582 if not worker.perf_file_base_name:
583 raise Exception('using perf buf perf report filename is unspecified')
584 workers_and_base_names[worker.host_and_port] = worker.perf_file_base_name
585 perf_report_failures += run_collect_perf_profile_jobs(workers_and_base_names, scenario.name)
586
587
588# Still write the index.html even if some scenarios failed.
589# 'profile_output_files' will only have names for scenarios that passed
590if perf_cmd and not args.skip_generate_flamegraphs:
591 # write the index fil to the output dir, with all profiles from all scenarios/workers
Alexander Polcyn66c67822016-12-09 10:22:50 -0800592 report_utils.render_perf_profiling_results('%s/index.html' % args.flame_graph_reports, profile_output_files)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700593
Alexander Polcyn41fe5792017-02-02 10:46:51 -0800594report_utils.render_junit_xml_report(merged_resultset, args.xml_report,
595 suite_name='benchmarks')
596
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700597if total_scenario_failures > 0 or qps_workers_killed > 0:
598 print('%s scenarios failed and %s qps worker jobs killed' % (total_scenario_failures, qps_workers_killed))
599 sys.exit(1)
Jan Tattermusch94d40cb2016-10-24 21:06:40 +0200600
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700601if perf_report_failures > 0:
602 print('%s perf profile collection jobs failed' % perf_report_failures)
Alex Polcyncac93f62016-10-19 09:27:57 -0700603 sys.exit(1)