-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhttp_hyper.rs
61 lines (54 loc) · 1.58 KB
/
http_hyper.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
59
60
61
use anyhow::Result;
use async_trait::async_trait;
use bytes::Bytes;
use clap::Parser;
use http_body_util::{BodyExt, Full};
use hyper::Uri;
use hyper_tls::HttpsConnector;
use hyper_util::{
client::legacy::{connect::HttpConnector, Client},
rt::TokioExecutor,
};
use rlt::{
cli::BenchCli,
IterReport, {BenchSuite, IterInfo},
};
use tokio::time::Instant;
#[derive(Parser, Clone)]
pub struct Opts {
/// Target URL.
pub url: Uri,
/// Embed BenchCli into this Opts.
#[command(flatten)]
pub bench_opts: BenchCli,
}
#[derive(Clone)]
struct HttpBench {
url: Uri,
}
#[async_trait]
impl BenchSuite for HttpBench {
type WorkerState = Client<HttpsConnector<HttpConnector>, Full<Bytes>>;
async fn state(&self, _: u32) -> Result<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) -> Result<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
}