diff options
author | Mataes <mataes2007@gmail.com> | 2018-04-27 20:39:22 +0300 |
---|---|---|
committer | Mataes <mataes2007@gmail.com> | 2018-04-27 20:39:22 +0300 |
commit | b9ce1d4d98525490ca1a38e2d9fd4f3369adb3e0 (patch) | |
tree | 787c80a909776c1c4d099b638c83c7977bb070e2 /libs/tdlib/td/benchmark/bench_http_reader.cpp | |
parent | 5ed0126c16d061d6e87aa20c718e14608c66feec (diff) |
added tdlib library
Diffstat (limited to 'libs/tdlib/td/benchmark/bench_http_reader.cpp')
-rw-r--r-- | libs/tdlib/td/benchmark/bench_http_reader.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/libs/tdlib/td/benchmark/bench_http_reader.cpp b/libs/tdlib/td/benchmark/bench_http_reader.cpp new file mode 100644 index 0000000000..2afe2d73ff --- /dev/null +++ b/libs/tdlib/td/benchmark/bench_http_reader.cpp @@ -0,0 +1,118 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// +// 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/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 : public td::Benchmark { + std::string get_description() const override { + return "HttpReaderBench"; + } + + void run(int n) override { + int 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() override { + writer_ = td::ChainBufferWriter::create_empty(); + reader_ = writer_.extract_reader(); + http_reader_.init(&reader_, 10000, 0); + } +}; + +class BufferBench : public td::Benchmark { + std::string get_description() const override { + return "BufferBench"; + } + + void run(int n) override { + int 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++) { + reader_.cut_head(http_query.size()); + } + } + } + td::ChainBufferWriter writer_; + td::ChainBufferReader reader_; + td::HttpReader http_reader_; + + void start_up() override { + writer_ = td::ChainBufferWriter::create_empty(); + reader_ = writer_.extract_reader(); + } +}; + +class FindBoundaryBench : public td::Benchmark { + std::string get_description() const override { + return "FindBoundaryBench"; + } + + void run(int n) override { + int 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()); + reader_.cut_head(len + 2); + reader_.advance(2); + } + } + } + td::ChainBufferWriter writer_; + td::ChainBufferReader reader_; + td::HttpReader http_reader_; + + void start_up() override { + writer_ = td::ChainBufferWriter::create_empty(); + reader_ = writer_.extract_reader(); + } +}; + +int main() { + SET_VERBOSITY_LEVEL(VERBOSITY_NAME(WARNING)); + td::bench(BufferBench()); + td::bench(FindBoundaryBench()); + td::bench(HttpReaderBench()); +} |