2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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)
|
|
|
|
//
|
2018-02-03 13:58:18 +01:00
|
|
|
#include "td/utils/benchmark.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-02-03 13:58:18 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <ostream>
|
|
|
|
#include <streambuf>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
std::string create_tmp_file() {
|
|
|
|
#if TD_ANDROID
|
|
|
|
std::string name = "/data/local/tmp/large_file.txt";
|
|
|
|
unlink(name.c_str());
|
|
|
|
return name;
|
|
|
|
#else
|
|
|
|
char file_name[] = "largefileXXXXXX";
|
|
|
|
int fd = mkstemp(file_name);
|
|
|
|
if (fd == -1) {
|
|
|
|
perror("Can't cretate temporary file");
|
|
|
|
}
|
|
|
|
CHECK(fd != -1);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return file_name;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class IostreamWriteBench final : public td::Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
protected:
|
|
|
|
std::string file_name_;
|
|
|
|
std::ofstream stream;
|
2020-06-15 03:52:56 +02:00
|
|
|
static constexpr std::size_t BUFFER_SIZE = 1 << 20;
|
|
|
|
char buffer[BUFFER_SIZE];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "ostream (to file, no buf, no flush)";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
file_name_ = create_tmp_file();
|
|
|
|
stream.open(file_name_.c_str());
|
|
|
|
CHECK(stream.is_open());
|
2020-06-15 03:52:56 +02:00
|
|
|
// stream.rdbuf()->pubsetbuf(buffer, BUFFER_SIZE);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
stream << "This is just for test" << 987654321 << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
stream.close();
|
|
|
|
unlink(file_name_.c_str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FILEWriteBench final : public td::Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
protected:
|
|
|
|
std::string file_name_;
|
|
|
|
FILE *file;
|
2020-06-15 03:52:56 +02:00
|
|
|
static constexpr std::size_t BUFFER_SIZE = 1 << 20;
|
|
|
|
char buffer[BUFFER_SIZE];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-02-11 15:07:16 +01:00
|
|
|
return "std::fprintf (to file, no buf, no flush)";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
file_name_ = create_tmp_file();
|
|
|
|
file = fopen(file_name_.c_str(), "w");
|
2020-06-15 03:52:56 +02:00
|
|
|
// setvbuf(file, buffer, _IOFBF, BUFFER_SIZE);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
std::fprintf(file, "This is just for test%d\n", 987654321);
|
|
|
|
// std::fflush(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::fclose(file);
|
|
|
|
unlink(file_name_.c_str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if TD_ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#define ALOG(...) __android_log_print(ANDROID_LOG_VERBOSE, "XXX", __VA_ARGS__)
|
2021-07-04 04:58:54 +02:00
|
|
|
class ALogWriteBench final : public td::Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "android_log";
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
ALOG("This is just for test%d\n", 987654321);
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class LogWriteBench final : public td::Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
protected:
|
|
|
|
std::string file_name_;
|
|
|
|
std::ofstream stream;
|
|
|
|
std::streambuf *old_buf;
|
2020-06-15 03:52:56 +02:00
|
|
|
static constexpr std::size_t BUFFER_SIZE = 1 << 20;
|
|
|
|
char buffer[BUFFER_SIZE];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "td_log (slow in debug mode)";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
file_name_ = create_tmp_file();
|
|
|
|
stream.open(file_name_.c_str());
|
|
|
|
CHECK(stream.is_open());
|
|
|
|
old_buf = std::cerr.rdbuf(stream.rdbuf());
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
LOG(DEBUG) << "This is just for test" << 987654321;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
stream.close();
|
|
|
|
unlink(file_name_.c_str());
|
|
|
|
std::cerr.rdbuf(old_buf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
td::bench(LogWriteBench());
|
|
|
|
#if TD_ANDROID
|
|
|
|
td::bench(ALogWriteBench());
|
|
|
|
#endif
|
|
|
|
td::bench(IostreamWriteBench());
|
|
|
|
td::bench(FILEWriteBench());
|
|
|
|
}
|