File: lib.rs

package info (click to toggle)
rust-oxhttp 0.3.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 328 kB
  • sloc: makefile: 2; sh: 1
file content (104 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
use oxhttp::model::{Body, Request, Response, Uri};
use oxhttp::{Client, Server};
use std::io;
use std::io::Read;
use std::net::{Ipv4Addr, SocketAddrV4};

fn client_server_no_body(c: &mut Criterion) {
    Server::new(|_| Response::builder().body(Body::empty()).unwrap())
        .bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3456))
        .spawn()
        .unwrap();

    let client = Client::new();
    let uri = Uri::try_from("http://localhost:3456").unwrap();

    c.bench_function("client_server_no_body", |b| {
        b.iter(|| {
            client
                .request(Request::builder().uri(uri.clone()).body(()).unwrap())
                .unwrap();
        })
    });
}

fn client_server_fixed_body(c: &mut Criterion) {
    Server::new(|request| {
        let mut body = Vec::new();
        request.body_mut().read_to_end(&mut body).unwrap();
        Response::builder().body(Body::from(body)).unwrap()
    })
    .bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3457))
    .spawn()
    .unwrap();

    let client = Client::new();
    let uri = Uri::try_from("http://localhost:3456").unwrap();
    let body = vec![16u8; 1024];

    c.bench_function("client_server_fixed_body", |b| {
        b.iter(|| {
            client
                .request(
                    Request::builder()
                        .uri(uri.clone())
                        .body(body.clone())
                        .unwrap(),
                )
                .unwrap();
        })
    });
}

fn client_server_chunked_body(c: &mut Criterion) {
    Server::new(|request| {
        let mut body = Vec::new();
        request.body_mut().read_to_end(&mut body).unwrap();
        Response::builder().body(Body::empty()).unwrap()
    })
    .bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 3458))
    .spawn()
    .unwrap();

    let client = Client::new();
    let uri = Uri::try_from("http://localhost:3456").unwrap();

    c.bench_function("client_server_chunked_body", |b| {
        b.iter(|| {
            client
                .request(
                    Request::builder()
                        .uri(uri.clone())
                        .body(Body::from_read(ChunkedReader::default()))
                        .unwrap(),
                )
                .unwrap();
        })
    });
}

criterion_group!(
    client_server,
    client_server_no_body,
    client_server_fixed_body,
    client_server_chunked_body
);

criterion_main!(client_server);

#[derive(Default)]
struct ChunkedReader {
    counter: usize,
}

impl Read for ChunkedReader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if self.counter >= 10 {
            return Ok(0);
        }
        self.counter += 1;
        buf.fill(12);
        Ok(buf.len())
    }
}