-
Notifications
You must be signed in to change notification settings - Fork 187
/
benchmark.rs
58 lines (49 loc) · 2.02 KB
/
benchmark.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
use std::{env, fs::OpenOptions, io::Write, path::Path, time::Duration};
use tokio::time;
use wrk_api_bench::{BenchmarkBuilder, HistoryPeriod, WrkBuilder};
use crate::helpers::PokemonService;
mod helpers;
#[tokio::test]
async fn benchmark() -> Result<(), Box<dyn std::error::Error>> {
// Benchmarks are expensive, so they run only if the environment
// variable `RUN_BENCHMARKS` is present.
if env::var_os("RUN_BENCHMARKS").is_some() {
let _program = PokemonService::run();
// Give PokémonService some time to start up.
time::sleep(Duration::from_millis(50)).await;
// The history directory is cached inside GitHub actions under
// the running use home directory to allow us to recover historical
// data between runs.
let history_dir = if env::var_os("GITHUB_ACTIONS").is_some() {
home::home_dir().unwrap().join(".wrk-api-bench")
} else {
Path::new(".").join(".wrk-api-bench")
};
let mut wrk = WrkBuilder::default()
.url(String::from("http://localhost:13734/empty-operation"))
.history_dir(history_dir)
.build()?;
// Run a single benchmark with 8 threads and 64 connections for 60 seconds.
let benches = vec![BenchmarkBuilder::default()
.duration(Duration::from_secs(90))
.threads(2)
.connections(32)
.build()?];
wrk.bench(&benches)?;
// Calculate deviation from last run and write it to disk.
if let Ok(deviation) = wrk.deviation(HistoryPeriod::Last) {
let mut deviation_file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open("/tmp/smithy_rs_benchmark_deviation.txt")
.unwrap();
deviation_file.write_all(deviation.to_github_markdown().as_bytes())?;
}
}
Ok(())
}