summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/benchmark/bench_db.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Telegram/tdlib/td/benchmark/bench_db.cpp')
-rw-r--r--protocols/Telegram/tdlib/td/benchmark/bench_db.cpp89
1 files changed, 46 insertions, 43 deletions
diff --git a/protocols/Telegram/tdlib/td/benchmark/bench_db.cpp b/protocols/Telegram/tdlib/td/benchmark/bench_db.cpp
index dc768e9d9d..822a915938 100644
--- a/protocols/Telegram/tdlib/td/benchmark/bench_db.cpp
+++ b/protocols/Telegram/tdlib/td/benchmark/bench_db.cpp
@@ -1,28 +1,35 @@
//
-// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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/actor/actor.h"
-
#include "td/db/binlog/Binlog.h"
+#include "td/db/binlog/ConcurrentBinlog.h"
#include "td/db/BinlogKeyValue.h"
+#include "td/db/DbKey.h"
#include "td/db/SeqKeyValue.h"
+#include "td/db/SqliteConnectionSafe.h"
#include "td/db/SqliteDb.h"
#include "td/db/SqliteKeyValueAsync.h"
+#include "td/db/SqliteKeyValueSafe.h"
+
+#include "td/actor/actor.h"
+#include "td/actor/ConcurrentScheduler.h"
#include "td/utils/benchmark.h"
+#include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
+#include "td/utils/SliceBuilder.h"
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"
#include <memory>
template <class KeyValueT>
-class TdKvBench : public td::Benchmark {
- td::ConcurrentScheduler sched;
+class TdKvBench final : public td::Benchmark {
+ td::ConcurrentScheduler sched{1, 0};
td::string name_;
public:
@@ -30,27 +37,27 @@ class TdKvBench : public td::Benchmark {
name_ = std::move(name);
}
- td::string get_description() const override {
+ td::string get_description() const final {
return name_;
}
- class Main : public td::Actor {
+ class Main final : public td::Actor {
public:
explicit Main(int n) : n_(n) {
}
private:
- void loop() override {
+ void loop() final {
KeyValueT::destroy("test_tddb").ignore();
- class Worker : public Actor {
+ class Worker final : public Actor {
public:
Worker(int n, td::string db_name) : n_(n) {
kv_.init(db_name).ensure();
}
private:
- void loop() override {
+ void loop() final {
for (int i = 0; i < n_; i++) {
kv_.set(td::to_string(i % 10), td::to_string(i));
}
@@ -64,12 +71,11 @@ class TdKvBench : public td::Benchmark {
int n_;
};
- void start_up_n(int n) override {
- sched.init(1);
+ void start_up_n(int n) final {
sched.create_actor_unsafe<Main>(1, "Main", n).release();
}
- void run(int n) override {
+ void run(int n) final {
sched.start();
while (sched.run_main(10)) {
// empty
@@ -77,24 +83,23 @@ class TdKvBench : public td::Benchmark {
sched.finish();
}
- void tear_down() override {
+ void tear_down() final {
}
};
template <bool is_encrypted = false>
-class SqliteKVBench : public td::Benchmark {
+class SqliteKVBench final : public td::Benchmark {
td::SqliteDb db;
- td::string get_description() const override {
+ td::string get_description() const final {
return PSTRING() << "SqliteKV " << td::tag("is_encrypted", is_encrypted);
}
- void start_up() override {
+ void start_up() final {
td::string path = "testdb.sqlite";
td::SqliteDb::destroy(path).ignore();
if (is_encrypted) {
- td::SqliteDb::change_key(path, td::DbKey::password("cucumber"), td::DbKey::empty());
- db = td::SqliteDb::open_with_key(path, td::DbKey::password("cucumber")).move_as_ok();
+ db = td::SqliteDb::change_key(path, true, td::DbKey::password("cucumber"), td::DbKey::empty()).move_as_ok();
} else {
- db = td::SqliteDb::open_with_key(path, td::DbKey::empty()).move_as_ok();
+ db = td::SqliteDb::open_with_key(path, true, td::DbKey::empty()).move_as_ok();
}
db.exec("PRAGMA encoding=\"UTF-8\"").ensure();
db.exec("PRAGMA synchronous=NORMAL").ensure();
@@ -103,7 +108,7 @@ class SqliteKVBench : public td::Benchmark {
db.exec("DROP TABLE IF EXISTS KV").ensure();
db.exec("CREATE TABLE IF NOT EXISTS KV (k BLOB PRIMARY KEY, v BLOB)").ensure();
}
- void run(int n) override {
+ void run(int n) final {
auto stmt = db.get_statement("REPLACE INTO KV (k, v) VALUES(?1, ?2)").move_as_ok();
db.exec("BEGIN TRANSACTION").ensure();
for (int i = 0; i < n; i++) {
@@ -135,17 +140,17 @@ static td::Status init_db(td::SqliteDb &db) {
return td::Status::OK();
}
-class SqliteKeyValueAsyncBench : public td::Benchmark {
+class SqliteKeyValueAsyncBench final : public td::Benchmark {
public:
- td::string get_description() const override {
+ td::string get_description() const final {
return "SqliteKeyValueAsync";
}
- void start_up() override {
+ void start_up() final {
do_start_up().ensure();
scheduler_->start();
}
- void run(int n) override {
- auto guard = scheduler_->get_current_guard();
+ void run(int n) final {
+ auto guard = scheduler_->get_main_guard();
for (int i = 0; i < n; i++) {
auto key = td::to_string(i % 10);
@@ -153,10 +158,10 @@ class SqliteKeyValueAsyncBench : public td::Benchmark {
sqlite_kv_async_->set(key, value, td::Auto());
}
}
- void tear_down() override {
+ void tear_down() final {
scheduler_->run_main(0.1);
{
- auto guard = scheduler_->get_current_guard();
+ auto guard = scheduler_->get_main_guard();
sqlite_kv_async_.reset();
sqlite_kv_safe_.reset();
sql_connection_->close_and_destroy();
@@ -167,21 +172,20 @@ class SqliteKeyValueAsyncBench : public td::Benchmark {
}
private:
- std::unique_ptr<td::ConcurrentScheduler> scheduler_;
+ td::unique_ptr<td::ConcurrentScheduler> scheduler_;
std::shared_ptr<td::SqliteConnectionSafe> sql_connection_;
std::shared_ptr<td::SqliteKeyValueSafe> sqlite_kv_safe_;
- std::unique_ptr<td::SqliteKeyValueAsyncInterface> sqlite_kv_async_;
+ td::unique_ptr<td::SqliteKeyValueAsyncInterface> sqlite_kv_async_;
td::Status do_start_up() {
- scheduler_ = std::make_unique<td::ConcurrentScheduler>();
- scheduler_->init(1);
+ scheduler_ = td::make_unique<td::ConcurrentScheduler>(1, 0);
- auto guard = scheduler_->get_current_guard();
+ auto guard = scheduler_->get_main_guard();
td::string sql_db_name = "testdb.sqlite";
td::SqliteDb::destroy(sql_db_name).ignore();
- sql_connection_ = std::make_shared<td::SqliteConnectionSafe>(sql_db_name);
+ sql_connection_ = std::make_shared<td::SqliteConnectionSafe>(sql_db_name, td::DbKey::empty());
auto &db = sql_connection_->get();
TRY_STATUS(init_db(db));
@@ -192,31 +196,31 @@ class SqliteKeyValueAsyncBench : public td::Benchmark {
}
};
-class SeqKvBench : public td::Benchmark {
- td::string get_description() const override {
+class SeqKvBench final : public td::Benchmark {
+ td::string get_description() const final {
return "SeqKvBench";
}
td::SeqKeyValue kv;
- void run(int n) override {
+ void run(int n) final {
for (int i = 0; i < n; i++) {
- kv.set(td::to_string(i % 10), td::to_string(i));
+ kv.set(PSLICE() << i % 10, PSLICE() << i);
}
}
};
template <bool is_encrypted = false>
-class BinlogKeyValueBench : public td::Benchmark {
- td::string get_description() const override {
+class BinlogKeyValueBench final : public td::Benchmark {
+ td::string get_description() const final {
return PSTRING() << "BinlogKeyValue " << td::tag("is_encrypted", is_encrypted);
}
td::BinlogKeyValue<td::Binlog> kv;
- void start_up() override {
+ void start_up() final {
td::SqliteDb::destroy("test_binlog").ignore();
kv.init("test_binlog", is_encrypted ? td::DbKey::password("cucumber") : td::DbKey::empty()).ensure();
}
- void run(int n) override {
+ void run(int n) final {
for (int i = 0; i < n; i++) {
kv.set(td::to_string(i % 10), td::to_string(i));
}
@@ -233,5 +237,4 @@ int main() {
bench(TdKvBench<td::BinlogKeyValue<td::Binlog>>("BinlogKeyValue<Binlog>"));
bench(TdKvBench<td::BinlogKeyValue<td::ConcurrentBinlog>>("BinlogKeyValue<ConcurrentBinlog>"));
bench(SeqKvBench());
- return 0;
}