Skip to content

Commit ed15917

Browse files
authored
Merge pull request #1255 from pytorch/apachebenchimprovements
apache bench improvements
2 parents 3bb6dad + 6dbef1d commit ed15917

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

benchmarks/benchmark-ab.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
'docker_runtime': '',
3030
'backend_profiling': False,
3131
'config_properties': 'config.properties',
32-
'inference_model_url': 'predictions/benchmark'
32+
'inference_model_url': 'predictions/benchmark',
33+
'report_location': tempfile.gettempdir()
3334
}
34-
TMP_DIR = tempfile.gettempdir()
35+
36+
TMP_DIR = default_ab_params['report_location']
3537
execution_params = default_ab_params.copy()
3638
result_file = os.path.join(TMP_DIR, "benchmark/result.txt")
3739
metric_log = os.path.join(TMP_DIR, "benchmark/logs/model_metrics.log")
@@ -41,7 +43,6 @@ def json_provider(file_path, cmd_name):
4143
with open(file_path) as config_data:
4244
return json.load(config_data)
4345

44-
4546
@click.command()
4647
@click.argument('test_plan', default='custom')
4748
@click.option('--url', '-u', default='https://torchserve.pytorch.org/mar_files/resnet-18.mar',
@@ -70,7 +71,7 @@ def json_provider(file_path, cmd_name):
7071
@click_config_file.configuration_option(provider=json_provider, implicit=False,
7172
help="Read configuration from a JSON file")
7273
def benchmark(test_plan, url, gpus, exec_env, concurrency, requests, batch_size, batch_delay, input, workers,
73-
content_type, image, docker_runtime, backend_profiling, config_properties, inference_model_url):
74+
content_type, image, docker_runtime, backend_profiling, config_properties, inference_model_url, report_location):
7475
input_params = {'url': url,
7576
'gpus': gpus,
7677
'exec_env': exec_env,
@@ -85,12 +86,14 @@ def benchmark(test_plan, url, gpus, exec_env, concurrency, requests, batch_size,
8586
'docker_runtime': docker_runtime,
8687
'backend_profiling': backend_profiling,
8788
'config_properties': config_properties,
88-
'inference_model_url': inference_model_url
89+
'inference_model_url': inference_model_url,
90+
'report_location': report_location
8991
}
9092

9193
# set ab params
9294
update_plan_params[test_plan]()
9395
update_exec_params(input_params)
96+
9497
click.secho("Starting AB benchmark suite...", fg='green')
9598
click.secho(f"\n\nConfigured execution parameters are:", fg='green')
9699
click.secho(f"{execution_params}", fg="blue")
@@ -122,7 +125,7 @@ def check_torchserve_health():
122125
except Exception as e:
123126
retry += 1
124127
time.sleep(3)
125-
failure_exit("Could not connect to Tochserve instance at " + execution_params['inference_url'])
128+
failure_exit("Could not connect to Torchserve instance at " + execution_params['inference_url'])
126129

127130
def warm_up():
128131
register_model()
@@ -135,7 +138,7 @@ def warm_up():
135138

136139

137140
def run_benchmark():
138-
click.secho("\n\nExecuting inference perfromance tests ...", fg='green')
141+
click.secho("\n\nExecuting inference performance tests ...", fg='green')
139142
ab_cmd = f"ab -c {execution_params['concurrency']} -n {execution_params['requests']} -k -p {TMP_DIR}/benchmark/input -T " \
140143
f"{execution_params['content_type']} {execution_params['inference_url']}/{execution_params['inference_model_url']} > {result_file}"
141144

@@ -208,8 +211,8 @@ def docker_torchserve_start():
208211
if execution_params['backend_profiling']:
209212
backend_profiling = '-e TS_BENCHMARK=True'
210213

211-
# delete existing ts conatiner instance
212-
click.secho("*Removing existing ts conatiner instance...", fg='green')
214+
# delete existing ts container instance
215+
click.secho("*Removing existing ts container instance...", fg='green')
213216
execute('docker rm -f ts', wait=True)
214217

215218
click.secho(f"*Starting docker container of image {docker_image} ...", fg='green')
@@ -308,10 +311,17 @@ def generate_csv_output():
308311
line50 = int(batched_requests / 2)
309312
line90 = int(batched_requests * 9 / 10)
310313
line99 = int(batched_requests * 99 / 100)
314+
315+
click.secho(f"Saving benchmark results to {execution_params['report_location']}")
316+
311317
artifacts = {}
312-
with open(f'{TMP_DIR}/benchmark/result.txt') as f:
318+
with open(f"{execution_params['report_location']}/benchmark/result.txt") as f:
313319
data = f.readlines()
320+
314321
artifacts['Benchmark'] = "AB"
322+
artifacts['Batch size'] = execution_params['batch_size']
323+
artifacts['Batch delay'] = execution_params['batch_delay']
324+
artifacts['Workers'] = execution_params['workers']
315325
artifacts['Model'] = execution_params['url']
316326
artifacts['Concurrency'] = execution_params['concurrency']
317327
artifacts['Requests'] = execution_params['requests']
@@ -323,18 +333,18 @@ def generate_csv_output():
323333
artifacts['TS latency mean'] = extract_entity(data, 'Time per request:.*mean\)', -3)
324334
artifacts['TS error rate'] = int(artifacts['TS failed requests']) / execution_params['requests'] * 100
325335

326-
with open(os.path.join(TMP_DIR, 'benchmark/predict.txt')) as f:
336+
with open(os.path.join(execution_params['report_location'], 'benchmark/predict.txt')) as f:
327337
lines = f.readlines()
328338
lines.sort(key=float)
329339
artifacts['Model_p50'] = lines[line50].strip()
330340
artifacts['Model_p90'] = lines[line90].strip()
331341
artifacts['Model_p99'] = lines[line99].strip()
332342

333343
for m in metrics:
334-
df = pd.read_csv(f"{TMP_DIR}/benchmark/{m}", header=None, names=['data'])
344+
df = pd.read_csv(f"{execution_params['report_location']}/benchmark/{m}", header=None, names=['data'])
335345
artifacts[m.split('.txt')[0] + "_mean"] = df['data'].values.mean().round(2)
336346

337-
with open(os.path.join(TMP_DIR, 'benchmark/ab_report.csv'), 'w') as csv_file:
347+
with open(os.path.join(execution_params['report_location'], 'benchmark/ab_report.csv'), 'w') as csv_file:
338348
csvwriter = csv.writer(csv_file)
339349
csvwriter.writerow(artifacts.keys())
340350
csvwriter.writerow(artifacts.values())
@@ -351,7 +361,7 @@ def extract_entity(data, pattern, index, delim=" "):
351361

352362
def generate_latency_graph():
353363
click.secho("*Preparing graphs...", fg='green')
354-
df = pd.read_csv(os.path.join(TMP_DIR, 'benchmark/predict.txt'), header=None, names=['latency'])
364+
df = pd.read_csv(os.path.join(execution_params['report_location'], 'benchmark/predict.txt'), header=None, names=['latency'])
355365
iteration = df.index
356366
latency = df.latency
357367
a4_dims = (11.7, 8.27)
@@ -360,15 +370,15 @@ def generate_latency_graph():
360370
plt.ylabel('Prediction time')
361371
plt.title('Prediction latency')
362372
plt.bar(iteration, latency)
363-
plt.savefig(f"{TMP_DIR}/benchmark/predict_latency.png")
373+
plt.savefig(f"{execution_params['report_location']}/benchmark/predict_latency.png")
364374

365375

366376
def generate_profile_graph():
367377
click.secho("*Preparing Profile graphs...", fg='green')
368378

369379
plot_data = {}
370380
for m in metrics:
371-
df = pd.read_csv(f'{TMP_DIR}/benchmark/{m}', header=None)
381+
df = pd.read_csv(f"{execution_params['report_location']}/benchmark/{m}", header=None)
372382
m = m.split('.txt')[0]
373383
plot_data[f"{m}_index"] = df.index
374384
plot_data[f"{m}_values"] = df.values
@@ -434,7 +444,6 @@ def stop_torchserve():
434444
# Test plans (soak, vgg11_1000r_10c, vgg11_10000r_100c,...)
435445
def soak():
436446
execution_params['requests'] = 100000
437-
438447
execution_params['concurrency'] = 10
439448

440449

@@ -464,6 +473,15 @@ def resnet152_batch_docker():
464473
execution_params['batch_size'] = 4
465474
execution_params['exec_env'] = 'docker'
466475

476+
def bert_batch():
477+
execution_params['url'] = 'https://bert-mar-file.s3.us-west-2.amazonaws.com/BERTSeqClassification.mar'
478+
execution_params['requests'] = 1000
479+
execution_params['concurrency'] = 10
480+
execution_params['batch_size'] = 4
481+
execution_params['input'] = '../examples/Huggingface_Transformers/Seq_classification_artifacts/sample_text.txt'
482+
483+
def workflow_nmt():
484+
pass
467485

468486
def custom():
469487
pass
@@ -475,6 +493,8 @@ def custom():
475493
"vgg11_10000r_100c": vgg11_10000r_100c,
476494
"resnet152_batch": resnet152_batch,
477495
"resnet152_batch_docker": resnet152_batch_docker,
496+
"bert_batch": bert_batch,
497+
"workflow_nmt": workflow_nmt,
478498
"custom": custom
479499
}
480500

0 commit comments

Comments
 (0)