aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/io/benchmark/main.cpp
blob: 2a3a07b3bbfb9c18d99adbd71873e5489695eb01 (plain) (blame)
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
#include <library/cpp/http/io/headers.h>

#include <benchmark/benchmark.h>

#include <util/stream/str.h>

void FindHeaderFirstMatch(benchmark::State& state) {
    THttpHeaders headers;
    headers.AddHeader("Host", "example.com");
    Y_ENSURE(headers.FindHeader("Host"));
    for (auto _ : state) {
        auto header = headers.FindHeader("Host");
        benchmark::DoNotOptimize(header);
    }
}

void FindHeaderNoMatchSameSize(benchmark::State& state) {
    THttpHeaders headers;
    for (char c = 'a'; c <= 'z'; ++c) {
        headers.AddHeader(TString::Join(c, "aaa"), "some value");
    }
    Y_ENSURE(!headers.FindHeader("Host"));
    for (auto _ : state) {
        auto header = headers.FindHeader("Host");
        benchmark::DoNotOptimize(header);
    }
}

void FindHeaderNoMatchDifferentSizesNoCommonPrefix(benchmark::State& state) {
    THttpHeaders headers;
    for (char c = 'a'; c <= 'z'; ++c) {  // same number of headers as above
        headers.AddHeader("aaaaa", "some value");
    }
    Y_ENSURE(!headers.FindHeader("Host"));
    for (auto _ : state) {
        auto header = headers.FindHeader("Host");
        benchmark::DoNotOptimize(header);
    }
}

void FindHeaderNoMatchDifferentSizesCommonPrefix(benchmark::State& state) {
    THttpHeaders headers;
    for (char c = 'a'; c <= 'z'; ++c) {
        headers.AddHeader("Host2", "some value");
    }
    Y_ENSURE(!headers.FindHeader("Host"));
    for (auto _ : state) {
        auto header = headers.FindHeader("Host");
        benchmark::DoNotOptimize(header);
    }
}

void FindHeaderMoreRealisticUseCase(benchmark::State& state) {
    TString requestHeaders(R"(Host: yandex.ru
User-Agent: Mozilla/5.0 ...
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: text/plain;charset=UTF-8
Content-Length: 1234
Origin: https://a.yandex-team.ru
Connection: keep-alive
Referer: https://a.yandex-team.ru/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: cross-site
TE: trailers)");
    TStringInput stream(requestHeaders);
    THttpHeaders headers(&stream);
    Y_ENSURE(headers.FindHeader("Content-Type"));
    for (auto _ : state) {
        auto header = headers.FindHeader("Content-Type");
        benchmark::DoNotOptimize(header);
    }
}

BENCHMARK(FindHeaderFirstMatch);
BENCHMARK(FindHeaderNoMatchSameSize);
BENCHMARK(FindHeaderNoMatchDifferentSizesNoCommonPrefix);
BENCHMARK(FindHeaderNoMatchDifferentSizesCommonPrefix);
BENCHMARK(FindHeaderMoreRealisticUseCase);