Debian Codegraph @

benches/lib.rs

raw symbols SCIP
1 use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
2 use oxhttp::model::{Body, Request, Response, Uri};
3 use oxhttp::{Client, Server};
4 use std::io;
5 use std::io::Read;
6 use std::net::{Ipv4Addr, SocketAddrV4};
7
8 fn client_server_no_body(c: &mut Criterion) {
9 Server::new(|_| Response::builder().body(Body::empty()).unwrap())
10 .bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3456))
11 .spawn()
12 .unwrap();
13
14 let client = Client::new();
15 let uri = Uri::try_from("http://localhost:3456").unwrap();
16
17 c.bench_function("client_server_no_body", |b| {
18 b.iter(|| {
19 client
20 .request(Request::builder().uri(uri.clone()).body(()).unwrap())
21 .unwrap();
22 })
23 });
24 }
25
26 fn client_server_fixed_body(c: &mut Criterion) {
27 Server::new(|request| {
28 let mut body = Vec::new();
29 request.body_mut().read_to_end(&mut body).unwrap();
30 Response::builder().body(Body::from(body)).unwrap()
31 })
32 .bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3457))
33 .spawn()
34 .unwrap();
35
36 let client = Client::new();
37 let uri = Uri::try_from("http://localhost:3456").unwrap();
38 let body = vec![16u8; 1024];
39
40 c.bench_function("client_server_fixed_body", |b| {
41 b.iter(|| {
42 client
43 .request(
44 Request::builder()
45 .uri(uri.clone())
46 .body(body.clone())
47 .unwrap(),
48 )
49 .unwrap();
50 })
51 });
52 }
53
54 fn client_server_chunked_body(c: &mut Criterion) {
55 Server::new(|request| {
56 let mut body = Vec::new();
57 request.body_mut().read_to_end(&mut body).unwrap();
58 Response::builder().body(Body::empty()).unwrap()
59 })
60 .bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3458))
61 .spawn()
62 .unwrap();
63
64 let client = Client::new();
65 let uri = Uri::try_from("http://localhost:3456").unwrap();
66
67 c.bench_function("client_server_chunked_body", |b| {
68 b.iter(|| {
69 client
70 .request(
71 Request::builder()
72 .uri(uri.clone())
73 .body(Body::from_read(ChunkedReader::default()))
74 .unwrap(),
75 )
76 .unwrap();
77 })
78 });
79 }
80
81 criterion_group!(
82 client_server,
83 client_server_no_body,
84 client_server_fixed_body,
85 client_server_chunked_body
86 );
87
88 criterion_main!(client_server);
89
90 #[derive(Default)]
91 struct ChunkedReader {
92 counter: usize,
93 }
94
95 impl Read for ChunkedReader {
96 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
97 if self.counter >= 10 {
98 return Ok(0);
99 }
100 self.counter += 1;
101 buf.fill(12);
102 Ok(buf.len())
103 }
104 }