Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,19 @@ pretty_env_logger = "0.5"

# for benchmarks
criterion = "0.8.2"
reqwest = { version = "0.13", default-features = false, features = ["rustls", "http2"] }
reqwest = { version = "0.13", default-features = false, features = ["rustls"] }

[[bench]]
name = "http1"
path = "bench/http1.rs"
harness = false
required-features = ["wreq/stream", "reqwest/stream"]

[[bench]]
name = "http2"
path = "bench/http2.rs"
harness = false
required-features = ["wreq/stream", "reqwest/stream", "reqwest/http2"]

[[test]]
name = "cookie"
Expand Down
140 changes: 10 additions & 130 deletions bench/http1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,149 +3,29 @@
mod support;

use std::time::Duration;
use support::client::{bench_reqwest, bench_wreq};
use support::server::{spawn_multi_thread_server, spawn_single_thread_server, with_server};
use support::{HttpMode, build_current_thread_runtime, build_multi_thread_runtime};
use support::{HttpMode, bench};

use criterion::{
BenchmarkGroup, Criterion, criterion_group, criterion_main, measurement::WallTime,
};
use criterion::{Criterion, criterion_group, criterion_main};

const NUM_REQUESTS_TO_SEND: usize = 1000;
const CONCURRENT_LIMIT: usize = 100;
const HTTP_MODE: HttpMode = HttpMode::Http1;
const CURRENT_THREAD_LABEL: &str = "current_thread";
const MULTI_THREAD_LABEL: &str = "multi_thread";
const ADDR: &str = "127.0.0.1:5928";

fn run_benches(
group: &mut BenchmarkGroup<'_, WallTime>,
rt: fn() -> tokio::runtime::Runtime,
addr: &'static str,
label_prefix: &str,
) {
let runtime = rt();
bench_wreq(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
false,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);

bench_reqwest(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
false,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);

bench_wreq(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
true,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);

bench_reqwest(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
true,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);
}

// Criterion benchmark functions
#[inline]
fn bench_server_single_thread(c: &mut Criterion) {
let mut group = c.benchmark_group("server_single_thread");
group.sample_size(10);

// single-threaded client
with_server(
"127.0.0.1:5928",
HTTP_MODE,
spawn_single_thread_server,
|| {
run_benches(
&mut group,
build_current_thread_runtime,
"127.0.0.1:5928",
CURRENT_THREAD_LABEL,
);
},
);

// multi-threaded client
with_server(
"127.0.0.1:5930",
HTTP_MODE,
spawn_single_thread_server,
|| {
run_benches(
&mut group,
build_multi_thread_runtime,
"127.0.0.1:5930",
MULTI_THREAD_LABEL,
);
},
);
group.finish();
bench::bench_server_single_thread(c, HTTP_MODE, ADDR)
.expect("Failed to run single-threaded HTTP/1 benchmark server")
}

#[inline]
fn bench_server_multi_thread(c: &mut Criterion) {
let mut group = c.benchmark_group("server_multi_thread");
group.sample_size(10);

// single-threaded client
with_server(
"127.0.0.1:5929",
HTTP_MODE,
spawn_multi_thread_server,
|| {
run_benches(
&mut group,
build_current_thread_runtime,
"127.0.0.1:5929",
CURRENT_THREAD_LABEL,
);
},
);

// multi-threaded client
with_server(
"127.0.0.1:5931",
HTTP_MODE,
spawn_multi_thread_server,
|| {
run_benches(
&mut group,
build_multi_thread_runtime,
"127.0.0.1:5931",
MULTI_THREAD_LABEL,
);
},
);
group.finish();
bench::bench_server_multi_thread(c, HTTP_MODE, ADDR)
.expect("Failed to run multi-threaded HTTP/1 benchmark server")
}

criterion_group!(
name = benches;
config = Criterion::default()
.measurement_time(Duration::from_secs(10))
.sample_size(10)
.warm_up_time(Duration::from_secs(3));
targets =
bench_server_single_thread,
Expand Down
140 changes: 10 additions & 130 deletions bench/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,149 +3,29 @@
mod support;

use std::time::Duration;
use support::client::{bench_reqwest, bench_wreq};
use support::server::{spawn_multi_thread_server, spawn_single_thread_server, with_server};
use support::{HttpMode, build_current_thread_runtime, build_multi_thread_runtime};
use support::{HttpMode, bench};

use criterion::{
BenchmarkGroup, Criterion, criterion_group, criterion_main, measurement::WallTime,
};
use criterion::{Criterion, criterion_group, criterion_main};

const NUM_REQUESTS_TO_SEND: usize = 1000;
const CONCURRENT_LIMIT: usize = 100;
const HTTP_MODE: HttpMode = HttpMode::Http2;
const CURRENT_THREAD_LABEL: &str = "current_thread";
const MULTI_THREAD_LABEL: &str = "multi_thread";
const ADDR: &str = "127.0.0.1:6928";

fn run_benches(
group: &mut BenchmarkGroup<'_, WallTime>,
rt: fn() -> tokio::runtime::Runtime,
addr: &'static str,
label_prefix: &str,
) {
let runtime = rt();
bench_wreq(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
false,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);

bench_reqwest(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
false,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);

bench_wreq(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
true,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);

bench_reqwest(
group,
&runtime,
addr,
HTTP_MODE,
label_prefix,
true,
NUM_REQUESTS_TO_SEND,
CONCURRENT_LIMIT,
);
}

// Criterion benchmark functions
#[inline]
fn bench_server_single_thread(c: &mut Criterion) {
let mut group = c.benchmark_group("server_single_thread");
group.sample_size(10);

// single-threaded client
with_server(
"127.0.0.1:6928",
HTTP_MODE,
spawn_single_thread_server,
|| {
run_benches(
&mut group,
build_current_thread_runtime,
"127.0.0.1:6928",
CURRENT_THREAD_LABEL,
);
},
);

// multi-threaded client
with_server(
"127.0.0.1:6930",
HTTP_MODE,
spawn_single_thread_server,
|| {
run_benches(
&mut group,
build_multi_thread_runtime,
"127.0.0.1:6930",
MULTI_THREAD_LABEL,
);
},
);
group.finish();
bench::bench_server_single_thread(c, HTTP_MODE, ADDR)
.expect("Failed to run single-threaded HTTP/2 benchmark server")
}

#[inline]
fn bench_server_multi_thread(c: &mut Criterion) {
let mut group = c.benchmark_group("server_multi_thread");
group.sample_size(10);

// single-threaded client
with_server(
"127.0.0.1:6929",
HTTP_MODE,
spawn_multi_thread_server,
|| {
run_benches(
&mut group,
build_current_thread_runtime,
"127.0.0.1:6929",
CURRENT_THREAD_LABEL,
);
},
);

// multi-threaded client
with_server(
"127.0.0.1:6931",
HTTP_MODE,
spawn_multi_thread_server,
|| {
run_benches(
&mut group,
build_multi_thread_runtime,
"127.0.0.1:6931",
MULTI_THREAD_LABEL,
);
},
);
group.finish();
bench::bench_server_multi_thread(c, HTTP_MODE, ADDR)
.expect("Failed to run multi-threaded HTTP/2 benchmark server")
}

criterion_group!(
name = benches;
config = Criterion::default()
.measurement_time(Duration::from_secs(10))
.sample_size(10)
.warm_up_time(Duration::from_secs(3));
targets =
bench_server_single_thread,
Expand Down
13 changes: 13 additions & 0 deletions bench/support.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
pub mod bench;
pub mod client;
pub mod server;

use std::fmt;

#[allow(unused)]
#[derive(Clone, Copy, Debug)]
pub enum HttpMode {
Http1,
Http2,
}

impl fmt::Display for HttpMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let value = match self {
HttpMode::Http1 => "http1",
HttpMode::Http2 => "http2",
};
f.write_str(value)
}
}

pub fn build_current_thread_runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand Down
Loading
Loading