2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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/utils/benchmark.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/Clocks.h"
|
|
|
|
#include "td/utils/port/EventFd.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/port/path.h"
|
|
|
|
#include "td/utils/port/RwMutex.h"
|
|
|
|
#include "td/utils/port/Stat.h"
|
|
|
|
#include "td/utils/port/thread.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
#include "td/telegram/telegram_api.hpp"
|
|
|
|
|
|
|
|
#if !TD_WINDOWS
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_LINUX || TD_ANDROID || TD_TIZEN
|
|
|
|
#include <semaphore.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class F {
|
|
|
|
uint32 ∑
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit F(uint32 &sum) : sum(sum) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void operator()(const T &x) const {
|
|
|
|
sum += static_cast<uint32>(x.get_id());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCH(Call, "TL Call") {
|
2019-11-21 15:53:39 +01:00
|
|
|
tl_object_ptr<telegram_api::Function> x = make_tl_object<telegram_api::account_getWallPapers>(0);
|
2018-12-31 20:04:05 +01:00
|
|
|
uint32 res = 0;
|
|
|
|
F f(res);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
downcast_call(*x, f);
|
|
|
|
}
|
|
|
|
do_not_optimize_away(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !TD_EVENTFD_UNSUPPORTED
|
|
|
|
BENCH(EventFd, "EventFd") {
|
|
|
|
EventFd fd;
|
|
|
|
fd.init();
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
fd.release();
|
|
|
|
fd.acquire();
|
|
|
|
}
|
|
|
|
fd.close();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BENCH(NewInt, "new int + delete") {
|
|
|
|
std::uintptr_t res = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
int *x = new int;
|
|
|
|
res += reinterpret_cast<std::uintptr_t>(x);
|
|
|
|
delete x;
|
|
|
|
}
|
|
|
|
do_not_optimize_away(res);
|
|
|
|
}
|
|
|
|
|
2019-10-19 20:20:16 +02:00
|
|
|
BENCH(NewObj, "new struct, then delete") {
|
2018-12-31 20:04:05 +01:00
|
|
|
struct A {
|
|
|
|
int32 a = 0;
|
|
|
|
int32 b = 0;
|
|
|
|
int32 c = 0;
|
|
|
|
int32 d = 0;
|
|
|
|
};
|
|
|
|
std::uintptr_t res = 0;
|
|
|
|
A **ptr = new A *[n];
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
ptr[i] = new A();
|
|
|
|
res += reinterpret_cast<std::uintptr_t>(ptr[i]);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
delete ptr[i];
|
|
|
|
}
|
|
|
|
delete[] ptr;
|
|
|
|
do_not_optimize_away(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
2019-10-19 20:20:16 +02:00
|
|
|
BENCH(ThreadNew, "new struct, then delete in several threads") {
|
2018-12-31 20:04:05 +01:00
|
|
|
td::NewObjBench a, b;
|
|
|
|
thread ta([&] { a.run(n / 2); });
|
|
|
|
thread tb([&] { b.run(n - n / 2); });
|
|
|
|
ta.join();
|
|
|
|
tb.join();
|
|
|
|
}
|
|
|
|
#endif
|
2018-03-28 00:13:04 +02:00
|
|
|
/*
|
|
|
|
// Too hard for clang (?)
|
2018-12-31 20:04:05 +01:00
|
|
|
BENCH(Time, "Clocks::monotonic") {
|
|
|
|
double res = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
res += Clocks::monotonic();
|
|
|
|
}
|
|
|
|
do_not_optimize_away(res);
|
|
|
|
}
|
2018-03-28 00:13:04 +02:00
|
|
|
*/
|
2018-12-31 20:04:05 +01:00
|
|
|
#if !TD_WINDOWS
|
2021-07-04 04:58:54 +02:00
|
|
|
class PipeBench final : public Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
int p[2];
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "pipe write + read int32";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-10-25 17:48:17 +02:00
|
|
|
int res = pipe(p);
|
|
|
|
CHECK(res == 0);
|
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
|
|
|
int res = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
int val = 1;
|
2018-10-25 17:48:17 +02:00
|
|
|
auto write_len = write(p[1], &val, sizeof(val));
|
|
|
|
CHECK(write_len == sizeof(val));
|
|
|
|
auto read_len = read(p[0], &val, sizeof(val));
|
|
|
|
CHECK(read_len == sizeof(val));
|
2018-12-31 20:04:05 +01:00
|
|
|
res += val;
|
|
|
|
}
|
|
|
|
do_not_optimize_away(res);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_LINUX || TD_ANDROID || TD_TIZEN
|
2021-07-04 04:58:54 +02:00
|
|
|
class SemBench final : public Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
sem_t sem;
|
|
|
|
|
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "sem post + wait";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
int err = sem_init(&sem, 0, 0);
|
|
|
|
CHECK(err != -1);
|
|
|
|
}
|
|
|
|
|
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++) {
|
|
|
|
sem_post(&sem);
|
|
|
|
sem_wait(&sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
sem_destroy(&sem);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !TD_WINDOWS
|
2021-07-04 04:58:54 +02:00
|
|
|
class UtimeBench final : public Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
FileFd::open("test", FileFd::Flags::Create | FileFd::Flags::Write).move_as_ok().close();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "utime";
|
|
|
|
}
|
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++) {
|
|
|
|
int err = utime("test", nullptr);
|
|
|
|
CHECK(err >= 0);
|
|
|
|
utimbuf buf;
|
|
|
|
buf.modtime = 123;
|
|
|
|
buf.actime = 321;
|
|
|
|
err = utime("test", &buf);
|
|
|
|
CHECK(err >= 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BENCH(Pwrite, "pwrite") {
|
|
|
|
auto fd = FileFd::open("test", FileFd::Flags::Create | FileFd::Flags::Write).move_as_ok();
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
fd.pwrite("a", 0).ok();
|
|
|
|
}
|
|
|
|
fd.close();
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class CreateFileBench final : public Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "create_file";
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
mkdir("A").ensure();
|
|
|
|
}
|
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++) {
|
2018-07-08 01:47:46 +02:00
|
|
|
FileFd::open(PSLICE() << "A/" << i, FileFd::Flags::Write | FileFd::Flags::Create).move_as_ok().close();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2019-09-28 04:14:21 +02:00
|
|
|
td::walk_path("A/", [&](CSlice path, auto type) {
|
|
|
|
if (type == td::WalkPath::Type::ExitDir) {
|
|
|
|
rmdir(path).ignore();
|
|
|
|
} else if (type == td::WalkPath::Type::NotDir) {
|
|
|
|
unlink(path).ignore();
|
|
|
|
}
|
|
|
|
}).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
2018-07-08 01:47:46 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class WalkPathBench final : public Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "walk_path";
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up_n(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
mkdir("A").ensure();
|
|
|
|
for (int i = 0; i < n; i++) {
|
2018-07-08 01:47:46 +02:00
|
|
|
FileFd::open(PSLICE() << "A/" << i, FileFd::Flags::Write | FileFd::Flags::Create).move_as_ok().close();
|
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
|
|
|
int cnt = 0;
|
2019-09-28 04:14:21 +02:00
|
|
|
td::walk_path("A/", [&](CSlice path, auto type) {
|
|
|
|
if (type == td::WalkPath::Type::EnterDir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stat(path).ok();
|
|
|
|
cnt++;
|
|
|
|
}).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2019-09-28 04:14:21 +02:00
|
|
|
td::walk_path("A/", [&](CSlice path, auto type) {
|
|
|
|
if (type == td::WalkPath::Type::ExitDir) {
|
|
|
|
rmdir(path).ignore();
|
|
|
|
} else if (type == td::WalkPath::Type::NotDir) {
|
|
|
|
unlink(path).ignore();
|
|
|
|
}
|
|
|
|
}).ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
|
|
|
template <int ThreadN = 2>
|
2021-07-04 04:58:54 +02:00
|
|
|
class AtomicReleaseIncBench final : public Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return PSTRING() << "AtomicReleaseInc" << ThreadN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::atomic<uint64> a_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<thread> threads;
|
|
|
|
for (int i = 0; i < ThreadN; i++) {
|
|
|
|
threads.emplace_back([&] {
|
|
|
|
for (int i = 0; i < n / ThreadN; i++) {
|
|
|
|
a_.fetch_add(1, std::memory_order_release);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <int ThreadN>
|
|
|
|
std::atomic<uint64> AtomicReleaseIncBench<ThreadN>::a_;
|
|
|
|
|
|
|
|
template <int ThreadN = 2>
|
2021-07-04 04:58:54 +02:00
|
|
|
class AtomicReleaseCasIncBench final : public Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return PSTRING() << "AtomicReleaseCasInc" << ThreadN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::atomic<uint64> a_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<thread> threads;
|
|
|
|
for (int i = 0; i < ThreadN; i++) {
|
|
|
|
threads.emplace_back([&] {
|
|
|
|
for (int i = 0; i < n / ThreadN; i++) {
|
|
|
|
auto value = a_.load(std::memory_order_relaxed);
|
|
|
|
while (!a_.compare_exchange_strong(value, value + 1, std::memory_order_release, std::memory_order_relaxed)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <int ThreadN>
|
|
|
|
std::atomic<uint64> AtomicReleaseCasIncBench<ThreadN>::a_;
|
|
|
|
|
|
|
|
template <int ThreadN = 2>
|
2021-07-04 04:58:54 +02:00
|
|
|
class RwMutexReadBench final : public Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return PSTRING() << "RwMutexRead" << ThreadN;
|
|
|
|
}
|
|
|
|
RwMutex mutex_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<thread> threads;
|
|
|
|
for (int i = 0; i < ThreadN; i++) {
|
|
|
|
threads.emplace_back([&] {
|
|
|
|
for (int i = 0; i < n / ThreadN; i++) {
|
|
|
|
mutex_.lock_read().ensure();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <int ThreadN = 2>
|
2021-07-04 04:58:54 +02:00
|
|
|
class RwMutexWriteBench final : public Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return PSTRING() << "RwMutexWrite" << ThreadN;
|
|
|
|
}
|
|
|
|
RwMutex mutex_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<thread> threads;
|
|
|
|
for (int i = 0; i < ThreadN; i++) {
|
|
|
|
threads.emplace_back([&] {
|
|
|
|
for (int i = 0; i < n / ThreadN; i++) {
|
|
|
|
mutex_.lock_write().ensure();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(DEBUG));
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
|
|
|
td::bench(td::AtomicReleaseIncBench<1>());
|
|
|
|
td::bench(td::AtomicReleaseIncBench<2>());
|
|
|
|
td::bench(td::AtomicReleaseCasIncBench<1>());
|
|
|
|
td::bench(td::AtomicReleaseCasIncBench<2>());
|
|
|
|
td::bench(td::RwMutexWriteBench<1>());
|
|
|
|
td::bench(td::RwMutexReadBench<1>());
|
|
|
|
td::bench(td::RwMutexWriteBench<>());
|
|
|
|
td::bench(td::RwMutexReadBench<>());
|
|
|
|
#endif
|
|
|
|
#if !TD_WINDOWS
|
|
|
|
td::bench(td::UtimeBench());
|
|
|
|
#endif
|
|
|
|
td::bench(td::WalkPathBench());
|
|
|
|
td::bench(td::CreateFileBench());
|
|
|
|
td::bench(td::PwriteBench());
|
|
|
|
|
|
|
|
td::bench(td::CallBench());
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
|
|
|
td::bench(td::ThreadNewBench());
|
|
|
|
#endif
|
|
|
|
#if !TD_EVENTFD_UNSUPPORTED
|
|
|
|
td::bench(td::EventFdBench());
|
|
|
|
#endif
|
|
|
|
td::bench(td::NewObjBench());
|
|
|
|
td::bench(td::NewIntBench());
|
|
|
|
#if !TD_WINDOWS
|
|
|
|
td::bench(td::PipeBench());
|
|
|
|
#endif
|
|
|
|
#if TD_LINUX || TD_ANDROID || TD_TIZEN
|
|
|
|
td::bench(td::SemBench());
|
|
|
|
#endif
|
|
|
|
}
|