-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhttp_reqwest.rs
43 lines (37 loc) · 1.05 KB
/
http_reqwest.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
use anyhow::Result;
use async_trait::async_trait;
use clap::Parser;
use reqwest::{Client, Url};
use rlt::{
cli::BenchCli,
IterReport, {BenchSuite, IterInfo},
};
use tokio::time::Instant;
#[derive(Parser, Clone)]
pub struct HttpBench {
/// Target URL.
pub url: Url,
/// Embed BenchCli into this Opts.
#[command(flatten)]
pub bench_opts: BenchCli,
}
#[async_trait]
impl BenchSuite for HttpBench {
type WorkerState = Client;
async fn state(&self, _: u32) -> Result<Self::WorkerState> {
Ok(Client::new())
}
async fn bench(&mut self, client: &mut Self::WorkerState, _: &IterInfo) -> Result<IterReport> {
let t = Instant::now();
let resp = client.get(self.url.clone()).send().await?;
let status = resp.status().into();
let bytes = resp.bytes().await?.len() as u64;
let duration = t.elapsed();
Ok(IterReport { duration, status, bytes, items: 1 })
}
}
#[tokio::main]
async fn main() -> Result<()> {
let bs = HttpBench::parse();
rlt::cli::run(bs.bench_opts, bs).await
}