Repository navigation
Expand file tree
/
Copy pathhttp_hyper.rs
More file actions
54 lines (48 loc) · 1.51 KB
/
Copy pathhttp_hyper.rs
File metadata and controls
54 lines (48 loc) · 1.51 KB
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
use bytes::Bytes;
use clap::Parser;
use http_body_util::{BodyExt, Full};
use hyper::Uri;
use hyper_tls::HttpsConnector;
use hyper_util::client::legacy::Client;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::rt::TokioExecutor;
use rlt::{BenchResult, BenchSuite, IterInfo, IterReport, Result, bench_cli};
use tokio::time::Instant;
bench_cli!(Opts, {
/// Target URL.
#[clap(long)]
pub url: Uri,
});
#[derive(Clone)]
struct HttpBench {
url: Uri,
}
impl BenchSuite for HttpBench {
type WorkerState = Client<HttpsConnector<HttpConnector>, Full<Bytes>>;
async fn setup(&mut self, _worker_id: u32) -> BenchResult<Self::WorkerState> {
let https = HttpsConnector::new();
let client = Client::builder(TokioExecutor::new()).build(https);
Ok(client)
}
async fn bench(
&mut self,
client: &mut Self::WorkerState,
_: &IterInfo,
) -> BenchResult<IterReport> {
let t = Instant::now();
let mut resp = client.get(self.url.clone()).await?;
let status = resp.status().into();
let mut bytes = 0;
while let Some(next) = resp.frame().await {
bytes += next?.data_ref().map(Bytes::len).unwrap_or_default() as u64;
}
let duration = t.elapsed();
Ok(IterReport { duration, status, bytes, items: 1 })
}
}
#[tokio::main]
async fn main() -> Result<()> {
let opts: Opts = Opts::parse();
let bench = HttpBench { url: opts.url };
rlt::cli::run(opts.bench_opts, bench).await
}