summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/benchmark/bench_http_reader.cpp
blob: 403af06bec8bd77120d564246663a8f8bb1b0aa6 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/net/HttpQuery.h"
#include "td/net/HttpReader.h"

#include "td/utils/benchmark.h"
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/find_boundary.h"
#include "td/utils/logging.h"

static std::string http_query = "GET / HTTP/1.1\r\nConnection:keep-alive\r\nhost:127.0.0.1:8080\r\n\r\n";
static const size_t block_size = 2500;

class HttpReaderBench final : public td::Benchmark {
  std::string get_description() const final {
    return "HttpReaderBench";
  }

  void run(int n) final {
    auto cnt = static_cast<int>(block_size / http_query.size());
    td::HttpQuery q;
    int parsed = 0;
    int sent = 0;
    for (int i = 0; i < n; i += cnt) {
      for (int j = 0; j < cnt; j++) {
        writer_.append(http_query);
        sent++;
      }
      reader_.sync_with_writer();
      while (true) {
        auto wait = http_reader_.read_next(&q).ok();
        if (wait != 0) {
          break;
        }
        parsed++;
      }
    }
    CHECK(parsed == sent);
  }
  td::ChainBufferWriter writer_;
  td::ChainBufferReader reader_;
  td::HttpReader http_reader_;

  void start_up() final {
    reader_ = writer_.extract_reader();
    http_reader_.init(&reader_, 10000, 0);
  }
};

class BufferBench final : public td::Benchmark {
  std::string get_description() const final {
    return "BufferBench";
  }

  void run(int n) final {
    auto cnt = static_cast<int>(block_size / http_query.size());
    for (int i = 0; i < n; i += cnt) {
      for (int j = 0; j < cnt; j++) {
        writer_.append(http_query);
      }
      reader_.sync_with_writer();
      for (int j = 0; j < cnt; j++) {
        auto result = reader_.cut_head(http_query.size());
      }
    }
  }
  td::ChainBufferWriter writer_;
  td::ChainBufferReader reader_;
  td::HttpReader http_reader_;

  void start_up() final {
    reader_ = writer_.extract_reader();
  }
};

class FindBoundaryBench final : public td::Benchmark {
  std::string get_description() const final {
    return "FindBoundaryBench";
  }

  void run(int n) final {
    auto cnt = static_cast<int>(block_size / http_query.size());
    for (int i = 0; i < n; i += cnt) {
      for (int j = 0; j < cnt; j++) {
        writer_.append(http_query);
      }
      reader_.sync_with_writer();
      for (int j = 0; j < cnt; j++) {
        size_t len = 0;
        find_boundary(reader_.clone(), "\r\n\r\n", len);
        CHECK(size_t(len) + 4 == http_query.size());
        auto result = reader_.cut_head(len + 2);
        reader_.advance(2);
      }
    }
  }
  td::ChainBufferWriter writer_;
  td::ChainBufferReader reader_;
  td::HttpReader http_reader_;

  void start_up() final {
    reader_ = writer_.extract_reader();
  }
};

int main() {
  SET_VERBOSITY_LEVEL(VERBOSITY_NAME(WARNING));
  td::bench(BufferBench());
  td::bench(FindBoundaryBench());
  td::bench(HttpReaderBench());
}