2019-08-12 13:45:57 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
|
|
|
//
|
|
|
|
// 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"
|
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"
|
|
|
|
#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"
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/tests.h"
|
|
|
|
#include "td/utils/TsFileLog.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
|
2019-08-14 02:13:34 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
// Thread safe logging with tests
|
|
|
|
//
|
|
|
|
// LOG uses thread local LogInterface
|
|
|
|
// void append(CSlice slice, int log_level);
|
|
|
|
|
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>
|
|
|
|
class LogBenchmark : public td::Benchmark {
|
|
|
|
public:
|
2019-08-13 22:52:54 +02:00
|
|
|
LogBenchmark(std::string name, int threads_n, std::function<td::unique_ptr<Log>()> creator)
|
2019-08-12 13:45:57 +02:00
|
|
|
: name_(std::move(name)), threads_n_(threads_n), creator_(std::move(creator)) {
|
|
|
|
}
|
|
|
|
std::string get_description() const override {
|
|
|
|
return PSTRING() << name_ << " " << td::tag("threads_n", threads_n_);
|
|
|
|
}
|
|
|
|
void start_up() override {
|
|
|
|
log_ = creator_();
|
|
|
|
threads_.resize(threads_n_);
|
|
|
|
}
|
|
|
|
void tear_down() override {
|
|
|
|
for (auto path : log_->get_file_paths()) {
|
|
|
|
td::unlink(path).ignore();
|
|
|
|
}
|
|
|
|
log_.reset();
|
|
|
|
}
|
|
|
|
void run(int n) override {
|
|
|
|
for (auto &thread : threads_) {
|
|
|
|
thread = td::thread([this, n] { this->run_thread(n); });
|
|
|
|
}
|
|
|
|
for (auto &thread : threads_) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_thread(int n) {
|
|
|
|
auto str = PSTRING() << "#" << n << " : fsjklfdjsklfjdsklfjdksl\n";
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
log_->append(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
td::unique_ptr<Log> log_;
|
|
|
|
int threads_n_{0};
|
|
|
|
std::function<td::unique_ptr<Log>()> creator_;
|
|
|
|
std::vector<td::thread> threads_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F>
|
2019-08-14 02:13:34 +02:00
|
|
|
static void bench_log(std::string name, int threads_n, F &&f) {
|
2019-08-12 13:45:57 +02:00
|
|
|
bench(LogBenchmark<typename decltype(f())::element_type>(std::move(name), threads_n, std::move(f)));
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Log, TsLogger) {
|
|
|
|
bench_log("NewTsFileLog", 4, [] { return td::TsFileLog::create("tmplog").move_as_ok(); });
|
|
|
|
bench_log("TsFileLog", 8, [] {
|
|
|
|
class FileLog : public td::LogInterface {
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
~FileLog() {
|
|
|
|
}
|
|
|
|
void append(td::CSlice slice) override {
|
|
|
|
ts_log_.append(slice, -1);
|
|
|
|
}
|
|
|
|
std::vector<std::string> get_file_paths() override {
|
|
|
|
return file_log_.get_file_paths();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
td::FileLog file_log_;
|
|
|
|
td::TsLog ts_log_{nullptr};
|
|
|
|
};
|
|
|
|
return td::make_unique<FileLog>();
|
|
|
|
});
|
|
|
|
|
|
|
|
bench_log("noop", 4, [] {
|
|
|
|
class NoopLog : public td::LogInterface {
|
|
|
|
public:
|
|
|
|
void append(td::CSlice slice) override {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return td::make_unique<NoopLog>();
|
|
|
|
});
|
|
|
|
|
|
|
|
bench_log("FileLog", 4, [] {
|
|
|
|
class FileLog : public td::LogInterface {
|
|
|
|
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
|
|
|
}
|
|
|
|
~FileLog() {
|
|
|
|
}
|
|
|
|
void append(td::CSlice slice) override {
|
|
|
|
file_log_.append(slice, -1);
|
|
|
|
}
|
|
|
|
std::vector<std::string> get_file_paths() override {
|
|
|
|
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
|