File: response-body-compression.rs

package info (click to toggle)
rust-actix-http 3.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,833 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
use std::convert::Infallible;

use actix_http::{encoding::Encoder, ContentEncoding, Request, Response, StatusCode};
use actix_service::{fn_service, Service as _};
use criterion::{black_box, criterion_group, criterion_main, Criterion};

static BODY: &[u8] = include_bytes!("../Cargo.toml");

fn compression_responses(c: &mut Criterion) {
    let mut group = c.benchmark_group("compression responses");

    group.bench_function("identity", |b| {
        let rt = actix_rt::Runtime::new().unwrap();

        let identity_svc = fn_service(|_: Request| async move {
            let mut res = Response::with_body(StatusCode::OK, ());
            let body = black_box(Encoder::response(
                ContentEncoding::Identity,
                res.head_mut(),
                BODY,
            ));
            Ok::<_, Infallible>(black_box(res.set_body(black_box(body))))
        });

        b.iter(|| {
            rt.block_on(identity_svc.call(Request::new())).unwrap();
        });
    });

    group.bench_function("gzip", |b| {
        let rt = actix_rt::Runtime::new().unwrap();

        let identity_svc = fn_service(|_: Request| async move {
            let mut res = Response::with_body(StatusCode::OK, ());
            let body = black_box(Encoder::response(
                ContentEncoding::Gzip,
                res.head_mut(),
                BODY,
            ));
            Ok::<_, Infallible>(black_box(res.set_body(black_box(body))))
        });

        b.iter(|| {
            rt.block_on(identity_svc.call(Request::new())).unwrap();
        });
    });

    group.bench_function("br", |b| {
        let rt = actix_rt::Runtime::new().unwrap();

        let identity_svc = fn_service(|_: Request| async move {
            let mut res = Response::with_body(StatusCode::OK, ());
            let body = black_box(Encoder::response(
                ContentEncoding::Brotli,
                res.head_mut(),
                BODY,
            ));
            Ok::<_, Infallible>(black_box(res.set_body(black_box(body))))
        });

        b.iter(|| {
            rt.block_on(identity_svc.call(Request::new())).unwrap();
        });
    });

    group.bench_function("zstd", |b| {
        let rt = actix_rt::Runtime::new().unwrap();

        let identity_svc = fn_service(|_: Request| async move {
            let mut res = Response::with_body(StatusCode::OK, ());
            let body = black_box(Encoder::response(
                ContentEncoding::Zstd,
                res.head_mut(),
                BODY,
            ));
            Ok::<_, Infallible>(black_box(res.set_body(black_box(body))))
        });

        b.iter(|| {
            rt.block_on(identity_svc.call(Request::new())).unwrap();
        });
    });

    group.finish();
}

criterion_group!(benches, compression_responses);
criterion_main!(benches);