2019-08-12 13:45:57 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2019-08-12 13:45:57 +02: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)
|
|
|
|
//
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/benchmark.h"
|
2021-05-17 15:53:35 +02:00
|
|
|
#include "td/utils/CombinedLog.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
#include "td/utils/FileLog.h"
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/format.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
#include "td/utils/logging.h"
|
2020-08-02 03:26:37 +02:00
|
|
|
#include "td/utils/MemoryLog.h"
|
2021-05-18 16:06:22 +02:00
|
|
|
#include "td/utils/NullLog.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
#include "td/utils/port/path.h"
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/port/thread.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/tests.h"
|
|
|
|
#include "td/utils/TsFileLog.h"
|
2021-05-18 16:40:37 +02:00
|
|
|
#include "td/utils/TsLog.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
|
2019-08-14 02:13:34 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
|
2019-11-17 21:42:48 +01:00
|
|
|
char disable_linker_warning_about_empty_file_tdutils_test_log_cpp TD_UNUSED;
|
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
2019-08-12 13:45:57 +02:00
|
|
|
template <class Log>
|
2021-07-04 04:58:54 +02:00
|
|
|
class LogBenchmark final : public td::Benchmark {
|
2019-08-12 13:45:57 +02:00
|
|
|
public:
|
2020-08-03 22:08:03 +02:00
|
|
|
LogBenchmark(std::string name, int threads_n, bool test_full_logging, std::function<td::unique_ptr<Log>()> creator)
|
|
|
|
: name_(std::move(name))
|
|
|
|
, threads_n_(threads_n)
|
|
|
|
, test_full_logging_(test_full_logging)
|
|
|
|
, creator_(std::move(creator)) {
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2020-08-03 22:08:03 +02:00
|
|
|
return PSTRING() << name_ << " " << (test_full_logging_ ? "ERROR" : "PLAIN") << " "
|
|
|
|
<< td::tag("threads_n", threads_n_);
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2019-08-12 13:45:57 +02:00
|
|
|
log_ = creator_();
|
|
|
|
threads_.resize(threads_n_);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2022-10-03 14:11:23 +02:00
|
|
|
if (log_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-21 11:51:16 +02:00
|
|
|
for (const auto &path : log_->get_file_paths()) {
|
2019-08-12 13:45:57 +02:00
|
|
|
td::unlink(path).ignore();
|
|
|
|
}
|
|
|
|
log_.reset();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2020-08-03 22:08:03 +02:00
|
|
|
auto old_log_interface = td::log_interface;
|
2022-10-03 14:11:23 +02:00
|
|
|
if (log_ != nullptr) {
|
|
|
|
td::log_interface = log_.get();
|
|
|
|
}
|
2020-08-03 22:08:03 +02:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
for (auto &thread : threads_) {
|
|
|
|
thread = td::thread([this, n] { this->run_thread(n); });
|
|
|
|
}
|
|
|
|
for (auto &thread : threads_) {
|
|
|
|
thread.join();
|
|
|
|
}
|
2020-08-03 22:08:03 +02:00
|
|
|
|
|
|
|
td::log_interface = old_log_interface;
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void run_thread(int n) {
|
|
|
|
auto str = PSTRING() << "#" << n << " : fsjklfdjsklfjdsklfjdksl\n";
|
|
|
|
for (int i = 0; i < n; i++) {
|
2022-10-03 14:11:23 +02:00
|
|
|
if (i % 10000 == 0 && log_ != nullptr) {
|
2021-05-17 16:58:33 +02:00
|
|
|
log_->after_rotation();
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
2020-08-03 22:08:03 +02:00
|
|
|
if (test_full_logging_) {
|
|
|
|
LOG(ERROR) << str;
|
|
|
|
} else {
|
|
|
|
LOG(PLAIN) << str;
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string name_;
|
2020-08-02 03:26:37 +02:00
|
|
|
td::unique_ptr<td::LogInterface> log_;
|
2019-08-12 13:45:57 +02:00
|
|
|
int threads_n_{0};
|
2020-08-03 22:08:03 +02:00
|
|
|
bool test_full_logging_{false};
|
2019-08-12 13:45:57 +02:00
|
|
|
std::function<td::unique_ptr<Log>()> creator_;
|
|
|
|
std::vector<td::thread> threads_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F>
|
2020-08-02 03:34:49 +02:00
|
|
|
static void bench_log(std::string name, F &&f) {
|
2020-08-03 22:08:03 +02:00
|
|
|
for (auto test_full_logging : {false, true}) {
|
|
|
|
for (auto threads_n : {1, 4, 8}) {
|
|
|
|
bench(LogBenchmark<typename decltype(f())::element_type>(name, threads_n, test_full_logging, f));
|
|
|
|
}
|
2020-08-02 03:34:49 +02:00
|
|
|
}
|
2021-08-30 22:27:32 +02:00
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
|
2020-08-02 03:26:37 +02:00
|
|
|
TEST(Log, Bench) {
|
2020-08-02 03:34:49 +02:00
|
|
|
bench_log("NullLog", [] { return td::make_unique<td::NullLog>(); });
|
|
|
|
|
2022-10-03 14:11:23 +02:00
|
|
|
// bench_log("Default", []() -> td::unique_ptr<td::NullLog> { return nullptr; });
|
|
|
|
|
2020-08-02 03:34:49 +02:00
|
|
|
bench_log("MemoryLog", [] { return td::make_unique<td::MemoryLog<1 << 20>>(); });
|
2020-08-02 03:26:37 +02:00
|
|
|
|
2021-05-17 15:53:35 +02:00
|
|
|
bench_log("CombinedLogEmpty", [] { return td::make_unique<td::CombinedLog>(); });
|
|
|
|
|
|
|
|
bench_log("CombinedLogMemory", [] {
|
|
|
|
auto result = td::make_unique<td::CombinedLog>();
|
|
|
|
static td::NullLog null_log;
|
|
|
|
static td::MemoryLog<1 << 20> memory_log;
|
|
|
|
result->set_first(&null_log);
|
|
|
|
result->set_second(&memory_log);
|
|
|
|
result->set_first_verbosity_level(VERBOSITY_NAME(DEBUG));
|
|
|
|
result->set_second_verbosity_level(VERBOSITY_NAME(DEBUG));
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2020-08-02 03:34:49 +02:00
|
|
|
bench_log("TsFileLog",
|
2020-06-24 13:47:36 +02:00
|
|
|
[] { return td::TsFileLog::create("tmplog", std::numeric_limits<td::int64>::max(), false).move_as_ok(); });
|
2020-08-02 03:26:37 +02:00
|
|
|
|
2020-08-02 03:34:49 +02:00
|
|
|
bench_log("FileLog + TsLog", [] {
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileLog final : public td::LogInterface {
|
2019-08-12 13:45:57 +02:00
|
|
|
public:
|
|
|
|
FileLog() {
|
2019-09-05 17:32:45 +02:00
|
|
|
file_log_.init("tmplog", std::numeric_limits<td::int64>::max(), false).ensure();
|
2019-08-12 13:45:57 +02:00
|
|
|
ts_log_.init(&file_log_);
|
|
|
|
}
|
2021-05-17 15:18:19 +02:00
|
|
|
void do_append(int log_level, td::CSlice slice) final {
|
|
|
|
static_cast<td::LogInterface &>(ts_log_).do_append(log_level, slice);
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
2021-05-17 15:18:19 +02:00
|
|
|
std::vector<std::string> get_file_paths() final {
|
2019-08-12 13:45:57 +02:00
|
|
|
return file_log_.get_file_paths();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
td::FileLog file_log_;
|
|
|
|
td::TsLog ts_log_{nullptr};
|
|
|
|
};
|
|
|
|
return td::make_unique<FileLog>();
|
|
|
|
});
|
|
|
|
|
2020-08-02 03:34:49 +02:00
|
|
|
bench_log("FileLog", [] {
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileLog final : public td::LogInterface {
|
2019-08-12 13:45:57 +02:00
|
|
|
public:
|
|
|
|
FileLog() {
|
2019-09-05 17:32:45 +02:00
|
|
|
file_log_.init("tmplog", std::numeric_limits<td::int64>::max(), false).ensure();
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
2021-05-17 15:18:19 +02:00
|
|
|
void do_append(int log_level, td::CSlice slice) final {
|
|
|
|
static_cast<td::LogInterface &>(file_log_).do_append(log_level, slice);
|
2019-08-12 13:45:57 +02:00
|
|
|
}
|
2021-05-17 15:18:19 +02:00
|
|
|
std::vector<std::string> get_file_paths() final {
|
2019-08-12 13:45:57 +02:00
|
|
|
return file_log_.get_file_paths();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
td::FileLog file_log_;
|
|
|
|
};
|
|
|
|
return td::make_unique<FileLog>();
|
|
|
|
});
|
|
|
|
}
|
2019-11-17 21:42:48 +01:00
|
|
|
#endif
|