blob: ad1fb054819f1dd774db242b06578fd718bd6efd [file] [log] [blame]
Siddharth Shukla8e64d902017-03-12 19:50:18 +01001#!/usr/bin/env python
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2016 gRPC authors.
Jan Tattermuschb2758442016-03-28 09:32:20 -07003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
Jan Tattermuschb2758442016-03-28 09:32:20 -07007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschb2758442016-03-28 09:32:20 -07009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Jan Tattermuschb2758442016-03-28 09:32:20 -070015
16"""Run performance tests locally or remotely."""
17
siddharthshukla0589e532016-07-07 16:08:01 +020018from __future__ import print_function
19
Jan Tattermuschb2758442016-03-28 09:32:20 -070020import argparse
Craig Tilleraccf16b2016-09-15 09:08:32 -070021import collections
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070022import itertools
Craig Tiller0bda0b32016-03-03 12:51:53 -080023import json
Jan Tattermuschb2758442016-03-28 09:32:20 -070024import multiprocessing
25import os
Craig Tiller0bda0b32016-03-03 12:51:53 -080026import pipes
Jan Tattermusch38becc22016-04-14 08:00:35 -070027import re
Jan Tattermuschb2758442016-03-28 09:32:20 -070028import subprocess
29import sys
30import tempfile
31import time
Jan Tattermuschee9032c2016-04-14 08:35:51 -070032import traceback
Jan Tattermuschb2758442016-03-28 09:32:20 -070033import uuid
Siddharth Shuklad194f592017-03-11 19:12:43 +010034import six
Jan Tattermusch5c79a312016-12-20 11:02:50 +010035
36import performance.scenario_config as scenario_config
37import python_utils.jobset as jobset
38import python_utils.report_utils as report_utils
Jan Tattermuschb2758442016-03-28 09:32:20 -070039
40
41_ROOT = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), '../..'))
42os.chdir(_ROOT)
43
44
45_REMOTE_HOST_USERNAME = 'jenkins'
46
47
Jan Tattermuschb2758442016-03-28 09:32:20 -070048class QpsWorkerJob:
49 """Encapsulates a qps worker server job."""
50
Alexander Polcyn9f08d112016-10-24 12:25:02 -070051 def __init__(self, spec, language, host_and_port, perf_file_base_name=None):
Jan Tattermuschb2758442016-03-28 09:32:20 -070052 self._spec = spec
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070053 self.language = language
Jan Tattermuschb2758442016-03-28 09:32:20 -070054 self.host_and_port = host_and_port
Craig Tillerc1b54f22016-09-15 08:57:14 -070055 self._job = None
Alexander Polcyn9f08d112016-10-24 12:25:02 -070056 self.perf_file_base_name = perf_file_base_name
Craig Tillerc1b54f22016-09-15 08:57:14 -070057
58 def start(self):
Craig Tillerc197ec12016-09-15 09:19:33 -070059 self._job = jobset.Job(self._spec, newline_on_success=True, travis=True, add_env={})
Jan Tattermuschb2758442016-03-28 09:32:20 -070060
61 def is_running(self):
62 """Polls a job and returns True if given job is still running."""
Craig Tillerc1b54f22016-09-15 08:57:14 -070063 return self._job and self._job.state() == jobset._RUNNING
Jan Tattermuschb2758442016-03-28 09:32:20 -070064
65 def kill(self):
Craig Tillerc1b54f22016-09-15 08:57:14 -070066 if self._job:
67 self._job.kill()
68 self._job = None
Jan Tattermuschb2758442016-03-28 09:32:20 -070069
70
Alexander Polcyn9f08d112016-10-24 12:25:02 -070071def create_qpsworker_job(language, shortname=None, port=10000, remote_host=None, perf_cmd=None):
72 cmdline = (language.worker_cmdline() + ['--driver_port=%s' % port])
73
Jan Tattermuschb2758442016-03-28 09:32:20 -070074 if remote_host:
Jan Tattermuschb2758442016-03-28 09:32:20 -070075 host_and_port='%s:%s' % (remote_host, port)
76 else:
77 host_and_port='localhost:%s' % port
78
Alexander Polcyn9f08d112016-10-24 12:25:02 -070079 perf_file_base_name = None
80 if perf_cmd:
81 perf_file_base_name = '%s-%s' % (host_and_port, shortname)
82 # specify -o output file so perf.data gets collected when worker stopped
83 cmdline = perf_cmd + ['-o', '%s-perf.data' % perf_file_base_name] + cmdline
84
Alexander Polcyn76be3062017-02-01 12:06:23 -080085 worker_timeout = 3 * 60
Alexander Polcyn9f08d112016-10-24 12:25:02 -070086 if remote_host:
87 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
88 ssh_cmd = ['ssh']
Alexander Polcyn76be3062017-02-01 12:06:23 -080089 cmdline = ['timeout', '%s' % (worker_timeout + 30)] + cmdline
Craig Tiller69218202017-03-07 15:54:33 -080090 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 -070091 cmdline = ssh_cmd
92
Jan Tattermuschb2758442016-03-28 09:32:20 -070093 jobspec = jobset.JobSpec(
Jan Tattermuschbb1a4532016-03-30 18:04:01 -070094 cmdline=cmdline,
95 shortname=shortname,
Alexander Polcyn76be3062017-02-01 12:06:23 -080096 timeout_seconds=worker_timeout, # workers get restarted after each scenario
Jan Tattermusch447548b2016-10-17 12:04:56 +020097 verbose_success=True)
Alexander Polcyn9f08d112016-10-24 12:25:02 -070098 return QpsWorkerJob(jobspec, language, host_and_port, perf_file_base_name)
Jan Tattermuschb2758442016-03-28 09:32:20 -070099
100
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700101def create_scenario_jobspec(scenario_json, workers, remote_host=None,
Yuxuan Liac87a462016-11-11 12:05:11 -0800102 bq_result_table=None, server_cpu_load=0):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700103 """Runs one scenario using QPS driver."""
104 # setting QPS_WORKERS env variable here makes sure it works with SSH too.
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700105 cmd = 'QPS_WORKERS="%s" ' % ','.join(workers)
106 if bq_result_table:
107 cmd += 'BQ_RESULT_TABLE="%s" ' % bq_result_table
108 cmd += 'tools/run_tests/performance/run_qps_driver.sh '
109 cmd += '--scenarios_json=%s ' % pipes.quote(json.dumps({'scenarios': [scenario_json]}))
Yuxuan Liac87a462016-11-11 12:05:11 -0800110 cmd += '--scenario_result_file=scenario_result.json '
111 if server_cpu_load != 0:
112 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 -0700113 if remote_host:
114 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700115 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Jan Tattermuschb2758442016-03-28 09:32:20 -0700116
117 return jobset.JobSpec(
118 cmdline=[cmd],
Craig Tiller0bda0b32016-03-03 12:51:53 -0800119 shortname='qps_json_driver.%s' % scenario_json['name'],
Yuxuan Liac87a462016-11-11 12:05:11 -0800120 timeout_seconds=12*60,
Craig Tiller0bda0b32016-03-03 12:51:53 -0800121 shell=True,
122 verbose_success=True)
123
124
125def 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.
Craig Tiller025972d2016-09-15 09:26:50 -0700128 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 -0800129 if remote_host:
130 user_at_host = '%s@%s' % (_REMOTE_HOST_USERNAME, remote_host)
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700131 cmd = 'ssh %s "cd ~/performance_workspace/grpc/ && "%s' % (user_at_host, pipes.quote(cmd))
Craig Tiller0bda0b32016-03-03 12:51:53 -0800132
133 return jobset.JobSpec(
134 cmdline=[cmd],
vjpai29089c72016-04-20 12:38:16 -0700135 shortname='qps_json_driver.quit',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700136 timeout_seconds=3*60,
137 shell=True,
138 verbose_success=True)
139
140
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700141def 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 Tattermuschad17bf72016-05-11 12:41:37 -0700147 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 Tattermusch4de2c322016-05-10 14:33:07 -0700158 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 Tattermuschde874a12016-04-18 09:21:37 -0700171def archive_repo(languages):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700172 """Archives local version of repo including submodules."""
Jan Tattermuschde874a12016-04-18 09:21:37 -0700173 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 Tattermuschb2758442016-03-28 09:32:20 -0700179 archive_job = jobset.JobSpec(
Jan Tattermuschde874a12016-04-18 09:21:37 -0700180 cmdline=cmdline,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700181 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 Tattermuschde874a12016-04-18 09:21:37 -0700189 'Archive with local repository created successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700190 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 Tattermusch14089202016-04-27 17:55:27 -0700197def prepare_remote_hosts(hosts, prepare_local=False):
198 """Prepares remote hosts (and maybe prepare localhost as well)."""
199 prepare_timeout = 5*60
Jan Tattermuschb2758442016-03-28 09:32:20 -0700200 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 Tattermusch14089202016-04-27 17:55:27 -0700208 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 Tattermuschb2758442016-03-28 09:32:20 -0700217 num_failures, _ = jobset.run(
218 prepare_jobs, newline_on_success=True, maxjobs=10)
219 if num_failures == 0:
220 jobset.message('SUCCESS',
Jan Tattermusch14089202016-04-27 17:55:27 -0700221 'Prepare step completed successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700222 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 Tillerd92d5c52016-04-04 13:49:29 -0700229def build_on_remote_hosts(hosts, languages=scenario_config.LANGUAGES.keys(), build_local=False):
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700230 """Builds performance worker on remote hosts (and maybe also locally)."""
Jan Tattermuschb2758442016-03-28 09:32:20 -0700231 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 Tiller7797e3f2016-04-01 07:41:05 -0700237 cmdline=['tools/run_tests/performance/remote_host_build.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700238 shortname='remote_host_build.%s' % host,
Craig Tiller0a3d5f92017-03-01 17:08:39 -0800239 environ = {'USER_AT_HOST': user_at_host, 'CONFIG': 'opt'},
Jan Tattermuschb2758442016-03-28 09:32:20 -0700240 timeout_seconds=build_timeout))
241 if build_local:
242 # Build locally as well
243 build_jobs.append(
244 jobset.JobSpec(
Craig Tiller7797e3f2016-04-01 07:41:05 -0700245 cmdline=['tools/run_tests/performance/build_performance.sh'] + languages,
Jan Tattermuschb2758442016-03-28 09:32:20 -0700246 shortname='local_build',
Craig Tiller0a3d5f92017-03-01 17:08:39 -0800247 environ = {'CONFIG': 'opt'},
Jan Tattermuschb2758442016-03-28 09:32:20 -0700248 timeout_seconds=build_timeout))
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700249 jobset.message('START', 'Building.', do_newline=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700250 num_failures, _ = jobset.run(
251 build_jobs, newline_on_success=True, maxjobs=10)
252 if num_failures == 0:
253 jobset.message('SUCCESS',
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700254 'Built successfully.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700255 do_newline=True)
256 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700257 jobset.message('FAILED', 'Build failed.',
Jan Tattermuschb2758442016-03-28 09:32:20 -0700258 do_newline=True)
259 sys.exit(1)
260
261
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700262def create_qpsworkers(languages, worker_hosts, perf_cmd=None):
Craig Tillerc1b54f22016-09-15 08:57:14 -0700263 """Creates QPS workers (but does not start them)."""
Jan Tattermuschb2758442016-03-28 09:32:20 -0700264 if not worker_hosts:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700265 # run two workers locally (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700266 workers=[(None, 10000), (None, 10010)]
267 elif len(worker_hosts) == 1:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700268 # run two workers on the remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700269 workers=[(worker_hosts[0], 10000), (worker_hosts[0], 10010)]
270 else:
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700271 # run one worker per each remote host (for each language)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700272 workers=[(worker_host, 10000) for worker_host in worker_hosts]
273
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700274 return [create_qpsworker_job(language,
275 shortname= 'qps_worker_%s_%s' % (language,
276 worker_idx),
277 port=worker[1] + language.worker_port_offset(),
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700278 remote_host=worker[0],
279 perf_cmd=perf_cmd)
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700280 for language in languages
281 for worker_idx, worker in enumerate(workers)]
Jan Tattermuschb2758442016-03-28 09:32:20 -0700282
283
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700284def perf_report_processor_job(worker_host, perf_base_name, output_filename):
285 print('Creating perf report collection job for %s' % worker_host)
286 cmd = ''
287 if worker_host != 'localhost':
288 user_at_host = "%s@%s" % (_REMOTE_HOST_USERNAME, worker_host)
289 cmd = "USER_AT_HOST=%s OUTPUT_FILENAME=%s OUTPUT_DIR=%s PERF_BASE_NAME=%s\
290 tools/run_tests/performance/process_remote_perf_flamegraphs.sh" \
Alexander Polcyn66c67822016-12-09 10:22:50 -0800291 % (user_at_host, output_filename, args.flame_graph_reports, perf_base_name)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700292 else:
293 cmd = "OUTPUT_FILENAME=%s OUTPUT_DIR=%s PERF_BASE_NAME=%s\
294 tools/run_tests/performance/process_local_perf_flamegraphs.sh" \
Alexander Polcyn66c67822016-12-09 10:22:50 -0800295 % (output_filename, args.flame_graph_reports, perf_base_name)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700296
297 return jobset.JobSpec(cmdline=cmd,
298 timeout_seconds=3*60,
299 shell=True,
300 verbose_success=True,
301 shortname='process perf report')
302
303
Craig Tiller677966a2016-09-26 07:37:28 -0700304Scenario = collections.namedtuple('Scenario', 'jobspec workers name')
Craig Tillerc1b54f22016-09-15 08:57:14 -0700305
306
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700307def create_scenarios(languages, workers_by_lang, remote_host=None, regex='.*',
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700308 category='all', bq_result_table=None,
Yuxuan Liac87a462016-11-11 12:05:11 -0800309 netperf=False, netperf_hosts=[], server_cpu_load=0):
Jan Tattermuschb2758442016-03-28 09:32:20 -0700310 """Create jobspecs for scenarios to run."""
Ken Payson0482c102016-04-19 12:08:34 -0700311 all_workers = [worker
312 for workers in workers_by_lang.values()
313 for worker in workers]
Jan Tattermuschb2758442016-03-28 09:32:20 -0700314 scenarios = []
Craig Tillerc1b54f22016-09-15 08:57:14 -0700315 _NO_WORKERS = []
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700316
317 if netperf:
318 if not netperf_hosts:
319 netperf_server='localhost'
320 netperf_client=None
321 elif len(netperf_hosts) == 1:
322 netperf_server=netperf_hosts[0]
323 netperf_client=netperf_hosts[0]
324 else:
325 netperf_server=netperf_hosts[0]
326 netperf_client=netperf_hosts[1]
Craig Tillerc1b54f22016-09-15 08:57:14 -0700327 scenarios.append(Scenario(
328 create_netperf_jobspec(server_host=netperf_server,
329 client_host=netperf_client,
330 bq_result_table=bq_result_table),
Craig Tiller677966a2016-09-26 07:37:28 -0700331 _NO_WORKERS, 'netperf'))
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700332
Jan Tattermuschb2758442016-03-28 09:32:20 -0700333 for language in languages:
Craig Tiller0bda0b32016-03-03 12:51:53 -0800334 for scenario_json in language.scenarios():
Jan Tattermusch38becc22016-04-14 08:00:35 -0700335 if re.search(args.regex, scenario_json['name']):
Craig Tillerb6df2472016-09-13 09:41:26 -0700336 categories = scenario_json.get('CATEGORIES', ['scalable', 'smoketest'])
337 if category in categories or category == 'all':
Craig Tillerc1b54f22016-09-15 08:57:14 -0700338 workers = workers_by_lang[str(language)][:]
Jan Tattermusch427699b2016-05-05 18:10:14 -0700339 # 'SERVER_LANGUAGE' is an indicator for this script to pick
340 # a server in different language.
341 custom_server_lang = scenario_json.get('SERVER_LANGUAGE', None)
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700342 custom_client_lang = scenario_json.get('CLIENT_LANGUAGE', None)
Jan Tattermusch427699b2016-05-05 18:10:14 -0700343 scenario_json = scenario_config.remove_nonproto_fields(scenario_json)
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700344 if custom_server_lang and custom_client_lang:
345 raise Exception('Cannot set both custom CLIENT_LANGUAGE and SERVER_LANGUAGE'
346 'in the same scenario')
Jan Tattermusch427699b2016-05-05 18:10:14 -0700347 if custom_server_lang:
348 if not workers_by_lang.get(custom_server_lang, []):
siddharthshukla0589e532016-07-07 16:08:01 +0200349 print('Warning: Skipping scenario %s as' % scenario_json['name'])
Jan Tattermusch427699b2016-05-05 18:10:14 -0700350 print('SERVER_LANGUAGE is set to %s yet the language has '
351 'not been selected with -l' % custom_server_lang)
352 continue
353 for idx in range(0, scenario_json['num_servers']):
354 # replace first X workers by workers of a different language
355 workers[idx] = workers_by_lang[custom_server_lang][idx]
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700356 if custom_client_lang:
357 if not workers_by_lang.get(custom_client_lang, []):
siddharthshukla0589e532016-07-07 16:08:01 +0200358 print('Warning: Skipping scenario %s as' % scenario_json['name'])
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700359 print('CLIENT_LANGUAGE is set to %s yet the language has '
360 'not been selected with -l' % custom_client_lang)
361 continue
362 for idx in range(scenario_json['num_servers'], len(workers)):
363 # replace all client workers by workers of a different language,
364 # leave num_server workers as they are server workers.
365 workers[idx] = workers_by_lang[custom_client_lang][idx]
Craig Tillerc1b54f22016-09-15 08:57:14 -0700366 scenario = Scenario(
367 create_scenario_jobspec(scenario_json,
368 [w.host_and_port for w in workers],
369 remote_host=remote_host,
Yuxuan Liac87a462016-11-11 12:05:11 -0800370 bq_result_table=bq_result_table,
371 server_cpu_load=server_cpu_load),
Craig Tiller677966a2016-09-26 07:37:28 -0700372 workers,
373 scenario_json['name'])
Jan Tattermusch427699b2016-05-05 18:10:14 -0700374 scenarios.append(scenario)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700375
Jan Tattermuschb2758442016-03-28 09:32:20 -0700376 return scenarios
377
378
379def finish_qps_workers(jobs):
380 """Waits for given jobs to finish and eventually kills them."""
381 retries = 0
Alexander Polcyn49796672016-10-17 10:01:37 -0700382 num_killed = 0
Jan Tattermuschb2758442016-03-28 09:32:20 -0700383 while any(job.is_running() for job in jobs):
384 for job in qpsworker_jobs:
385 if job.is_running():
siddharthshukla0589e532016-07-07 16:08:01 +0200386 print('QPS worker "%s" is still running.' % job.host_and_port)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700387 if retries > 10:
siddharthshukla0589e532016-07-07 16:08:01 +0200388 print('Killing all QPS workers.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700389 for job in jobs:
390 job.kill()
Alexander Polcyn49796672016-10-17 10:01:37 -0700391 num_killed += 1
Jan Tattermuschb2758442016-03-28 09:32:20 -0700392 retries += 1
393 time.sleep(3)
siddharthshukla0589e532016-07-07 16:08:01 +0200394 print('All QPS workers finished.')
Alexander Polcyn49796672016-10-17 10:01:37 -0700395 return num_killed
Jan Tattermuschb2758442016-03-28 09:32:20 -0700396
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700397profile_output_files = []
398
399# Collect perf text reports and flamegraphs if perf_cmd was used
400# Note the base names of perf text reports are used when creating and processing
401# perf data. The scenario name uniqifies the output name in the final
402# perf reports directory.
403# Alos, the perf profiles need to be fetched and processed after each scenario
404# in order to avoid clobbering the output files.
405def run_collect_perf_profile_jobs(hosts_and_base_names, scenario_name):
406 perf_report_jobs = []
407 global profile_output_files
408 for host_and_port in hosts_and_base_names:
409 perf_base_name = hosts_and_base_names[host_and_port]
410 output_filename = '%s-%s' % (scenario_name, perf_base_name)
411 # from the base filename, create .svg output filename
412 host = host_and_port.split(':')[0]
413 profile_output_files.append('%s.svg' % output_filename)
414 perf_report_jobs.append(perf_report_processor_job(host, perf_base_name, output_filename))
415
416 jobset.message('START', 'Collecting perf reports from qps workers', do_newline=True)
417 failures, _ = jobset.run(perf_report_jobs, newline_on_success=True, maxjobs=1)
418 jobset.message('END', 'Collecting perf reports from qps workers', do_newline=True)
419 return failures
420
421
Jan Tattermuschb2758442016-03-28 09:32:20 -0700422argp = argparse.ArgumentParser(description='Run performance tests.')
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700423argp.add_argument('-l', '--language',
Craig Tillerd92d5c52016-04-04 13:49:29 -0700424 choices=['all'] + sorted(scenario_config.LANGUAGES.keys()),
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700425 nargs='+',
Jan Tattermusch427699b2016-05-05 18:10:14 -0700426 required=True,
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700427 help='Languages to benchmark.')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700428argp.add_argument('--remote_driver_host',
429 default=None,
430 help='Run QPS driver on given host. By default, QPS driver is run locally.')
431argp.add_argument('--remote_worker_host',
432 nargs='+',
433 default=[],
434 help='Worker hosts where to start QPS workers.')
Craig Tiller677966a2016-09-26 07:37:28 -0700435argp.add_argument('--dry_run',
436 default=False,
437 action='store_const',
438 const=True,
439 help='Just list scenarios to be run, but don\'t run them.')
Jan Tattermusch38becc22016-04-14 08:00:35 -0700440argp.add_argument('-r', '--regex', default='.*', type=str,
441 help='Regex to select scenarios to run.')
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700442argp.add_argument('--bq_result_table', default=None, type=str,
443 help='Bigquery "dataset.table" to upload results to.')
Jan Tattermusch427699b2016-05-05 18:10:14 -0700444argp.add_argument('--category',
Craig Tiller6388da52016-09-07 17:06:29 -0700445 choices=['smoketest','all','scalable','sweep'],
Jan Tattermusch37a907e2016-05-13 13:49:43 -0700446 default='all',
447 help='Select a category of tests to run.')
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700448argp.add_argument('--netperf',
449 default=False,
450 action='store_const',
451 const=True,
452 help='Run netperf benchmark as one of the scenarios.')
Yuxuan Liac87a462016-11-11 12:05:11 -0800453argp.add_argument('--server_cpu_load',
454 default=0, type=int,
455 help='Select a targeted server cpu load to run. 0 means ignore this flag')
Jan Tattermusch88818ae2016-11-18 14:21:33 +0100456argp.add_argument('-x', '--xml_report', default='report.xml', type=str,
457 help='Name of XML report file to generate.')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700458argp.add_argument('--perf_args',
459 help=('Example usage: "--perf_args=record -F 99 -g". '
460 'Wrap QPS workers in a perf command '
461 'with the arguments to perf specified here. '
462 '".svg" flame graph profiles will be '
463 'created for each Qps Worker on each scenario. '
Alexander Polcyn66c67822016-12-09 10:22:50 -0800464 'Files will output to "<repo_root>/<args.flame_graph_reports>" '
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700465 'directory. Output files from running the worker '
466 'under perf are saved in the repo root where its ran. '
467 'Note that the perf "-g" flag is necessary for '
468 'flame graphs generation to work (assuming the binary '
469 'being profiled uses frame pointers, check out '
470 '"--call-graph dwarf" option using libunwind otherwise.) '
471 'Also note that the entire "--perf_args=<arg(s)>" must '
472 'be wrapped in quotes as in the example usage. '
473 'If the "--perg_args" is unspecified, "perf" will '
474 'not be used at all. '
475 'See http://www.brendangregg.com/perf.html '
476 'for more general perf examples.'))
477argp.add_argument('--skip_generate_flamegraphs',
478 default=False,
479 action='store_const',
480 const=True,
481 help=('Turn flame graph generation off. '
482 'May be useful if "perf_args" arguments do not make sense for '
483 'generating flamegraphs (e.g., "--perf_args=stat ...")'))
Alexander Polcyn66c67822016-12-09 10:22:50 -0800484argp.add_argument('-f', '--flame_graph_reports', default='perf_reports', type=str,
485 help='Name of directory to output flame graph profiles to, if any are created.')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700486
Jan Tattermuschb2758442016-03-28 09:32:20 -0700487args = argp.parse_args()
488
Craig Tillerd92d5c52016-04-04 13:49:29 -0700489languages = set(scenario_config.LANGUAGES[l]
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700490 for l in itertools.chain.from_iterable(
Siddharth Shuklad194f592017-03-11 19:12:43 +0100491 six.iterkeys(scenario_config.LANGUAGES) if x == 'all'
492 else [x] for x in args.language))
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700493
Jan Tattermusch6d7fa552016-04-14 17:42:54 -0700494
Jan Tattermuschb2758442016-03-28 09:32:20 -0700495# Put together set of remote hosts where to run and build
496remote_hosts = set()
497if args.remote_worker_host:
498 for host in args.remote_worker_host:
499 remote_hosts.add(host)
500if args.remote_driver_host:
501 remote_hosts.add(args.remote_driver_host)
502
Craig Tiller677966a2016-09-26 07:37:28 -0700503if not args.dry_run:
504 if remote_hosts:
505 archive_repo(languages=[str(l) for l in languages])
506 prepare_remote_hosts(remote_hosts, prepare_local=True)
507 else:
508 prepare_remote_hosts([], prepare_local=True)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700509
510build_local = False
511if not args.remote_driver_host:
512 build_local = True
Craig Tiller677966a2016-09-26 07:37:28 -0700513if not args.dry_run:
514 build_on_remote_hosts(remote_hosts, languages=[str(l) for l in languages], build_local=build_local)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700515
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700516perf_cmd = None
517if args.perf_args:
Alexander Polcyn66c67822016-12-09 10:22:50 -0800518 print('Running workers under perf profiler')
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700519 # Expect /usr/bin/perf to be installed here, as is usual
Alexander Polcyn66c67822016-12-09 10:22:50 -0800520 perf_cmd = ['/usr/bin/perf']
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700521 perf_cmd.extend(re.split('\s+', args.perf_args))
522
523qpsworker_jobs = create_qpsworkers(languages, args.remote_worker_host, perf_cmd=perf_cmd)
Jan Tattermusch38becc22016-04-14 08:00:35 -0700524
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700525# get list of worker addresses for each language.
Craig Tillerc1b54f22016-09-15 08:57:14 -0700526workers_by_lang = dict([(str(language), []) for language in languages])
Jan Tattermuschbb1a4532016-03-30 18:04:01 -0700527for job in qpsworker_jobs:
Craig Tillerc1b54f22016-09-15 08:57:14 -0700528 workers_by_lang[str(job.language)].append(job)
Jan Tattermuschb2758442016-03-28 09:32:20 -0700529
Craig Tillerc1b54f22016-09-15 08:57:14 -0700530scenarios = create_scenarios(languages,
Craig Tillerd82fccc2016-09-15 09:15:23 -0700531 workers_by_lang=workers_by_lang,
Craig Tillerc1b54f22016-09-15 08:57:14 -0700532 remote_host=args.remote_driver_host,
533 regex=args.regex,
534 category=args.category,
535 bq_result_table=args.bq_result_table,
536 netperf=args.netperf,
Yuxuan Liac87a462016-11-11 12:05:11 -0800537 netperf_hosts=args.remote_worker_host,
538 server_cpu_load=args.server_cpu_load)
Jan Tattermusch4de2c322016-05-10 14:33:07 -0700539
Craig Tillerc1b54f22016-09-15 08:57:14 -0700540if not scenarios:
541 raise Exception('No scenarios to run')
Jan Tattermuschb2758442016-03-28 09:32:20 -0700542
Alex Polcyncac93f62016-10-19 09:27:57 -0700543total_scenario_failures = 0
Alexander Polcyn898a2e92016-10-22 17:41:23 -0700544qps_workers_killed = 0
Jan Tattermusch94d40cb2016-10-24 21:06:40 +0200545merged_resultset = {}
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700546perf_report_failures = 0
547
Craig Tillerc1b54f22016-09-15 08:57:14 -0700548for scenario in scenarios:
Craig Tiller677966a2016-09-26 07:37:28 -0700549 if args.dry_run:
550 print(scenario.name)
551 else:
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700552 scenario_failures = 0
Craig Tiller677966a2016-09-26 07:37:28 -0700553 try:
554 for worker in scenario.workers:
555 worker.start()
Alex Polcynfcf09ea2016-12-06 04:00:05 +0000556 jobs = [scenario.jobspec]
Alex Polcynca5e9242016-12-06 04:21:37 +0000557 if scenario.workers:
Alex Polcynfcf09ea2016-12-06 04:00:05 +0000558 jobs.append(create_quit_jobspec(scenario.workers, remote_host=args.remote_driver_host))
559 scenario_failures, resultset = jobset.run(jobs, newline_on_success=True, maxjobs=1)
Alex Polcyncac93f62016-10-19 09:27:57 -0700560 total_scenario_failures += scenario_failures
Siddharth Shuklad194f592017-03-11 19:12:43 +0100561 merged_resultset = dict(itertools.chain(six.iteritems(merged_resultset),
562 six.iteritems(resultset)))
Craig Tiller677966a2016-09-26 07:37:28 -0700563 finally:
Alexander Polcyn898a2e92016-10-22 17:41:23 -0700564 # Consider qps workers that need to be killed as failures
565 qps_workers_killed += finish_qps_workers(scenario.workers)
Alexander Polcyn49796672016-10-17 10:01:37 -0700566
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700567 if perf_cmd and scenario_failures == 0 and not args.skip_generate_flamegraphs:
568 workers_and_base_names = {}
569 for worker in scenario.workers:
570 if not worker.perf_file_base_name:
571 raise Exception('using perf buf perf report filename is unspecified')
572 workers_and_base_names[worker.host_and_port] = worker.perf_file_base_name
573 perf_report_failures += run_collect_perf_profile_jobs(workers_and_base_names, scenario.name)
574
575
576# Still write the index.html even if some scenarios failed.
577# 'profile_output_files' will only have names for scenarios that passed
578if perf_cmd and not args.skip_generate_flamegraphs:
579 # write the index fil to the output dir, with all profiles from all scenarios/workers
Alexander Polcyn66c67822016-12-09 10:22:50 -0800580 report_utils.render_perf_profiling_results('%s/index.html' % args.flame_graph_reports, profile_output_files)
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700581
Alexander Polcyn41fe5792017-02-02 10:46:51 -0800582report_utils.render_junit_xml_report(merged_resultset, args.xml_report,
583 suite_name='benchmarks')
584
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700585if total_scenario_failures > 0 or qps_workers_killed > 0:
586 print('%s scenarios failed and %s qps worker jobs killed' % (total_scenario_failures, qps_workers_killed))
587 sys.exit(1)
Jan Tattermusch94d40cb2016-10-24 21:06:40 +0200588
Alexander Polcyn9f08d112016-10-24 12:25:02 -0700589if perf_report_failures > 0:
590 print('%s perf profile collection jobs failed' % perf_report_failures)
Alex Polcyncac93f62016-10-19 09:27:57 -0700591 sys.exit(1)