2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#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;
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class HttpReaderBench final : public td::Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "HttpReaderBench";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2021-10-20 00:27:02 +02:00
|
|
|
auto cnt = static_cast<int>(block_size / http_query.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
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_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2023-11-24 21:32:59 +01:00
|
|
|
writer_ = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
reader_ = writer_.extract_reader();
|
|
|
|
http_reader_.init(&reader_, 10000, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class BufferBench final : public td::Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "BufferBench";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2021-10-20 00:27:02 +02:00
|
|
|
auto cnt = static_cast<int>(block_size / http_query.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
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++) {
|
2018-10-26 16:11:20 +02:00
|
|
|
auto result = reader_.cut_head(http_query.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
td::ChainBufferWriter writer_;
|
|
|
|
td::ChainBufferReader reader_;
|
|
|
|
td::HttpReader http_reader_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2023-11-24 21:32:59 +01:00
|
|
|
writer_ = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
reader_ = writer_.extract_reader();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FindBoundaryBench final : public td::Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "FindBoundaryBench";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2021-10-20 00:27:02 +02:00
|
|
|
auto cnt = static_cast<int>(block_size / http_query.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
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());
|
2018-10-26 16:11:20 +02:00
|
|
|
auto result = reader_.cut_head(len + 2);
|
2018-12-31 20:04:05 +01:00
|
|
|
reader_.advance(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
td::ChainBufferWriter writer_;
|
|
|
|
td::ChainBufferReader reader_;
|
|
|
|
td::HttpReader http_reader_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2023-11-24 21:32:59 +01:00
|
|
|
writer_ = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
reader_ = writer_.extract_reader();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(WARNING));
|
|
|
|
td::bench(BufferBench());
|
|
|
|
td::bench(FindBoundaryBench());
|
|
|
|
td::bench(HttpReaderBench());
|
|
|
|
}
|