2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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/db/binlog/Binlog.h"
|
2019-01-07 00:44:29 +01:00
|
|
|
#include "td/db/binlog/ConcurrentBinlog.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/BinlogKeyValue.h"
|
2019-10-22 19:01:15 +02:00
|
|
|
#include "td/db/DbKey.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/SeqKeyValue.h"
|
2019-01-07 01:17:11 +01:00
|
|
|
#include "td/db/SqliteConnectionSafe.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/db/SqliteDb.h"
|
|
|
|
#include "td/db/SqliteKeyValueAsync.h"
|
2019-02-22 21:15:43 +01:00
|
|
|
#include "td/db/SqliteKeyValueSafe.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/benchmark.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
template <class KeyValueT>
|
2021-07-04 04:58:54 +02:00
|
|
|
class TdKvBench final : public td::Benchmark {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched{1, 0};
|
2018-12-31 20:04:05 +01:00
|
|
|
td::string name_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit TdKvBench(td::string name) {
|
|
|
|
name_ = std::move(name);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
td::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class Main final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit Main(int n) : n_(n) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
KeyValueT::destroy("test_tddb").ignore();
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class Worker final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
Worker(int n, td::string db_name) : n_(n) {
|
|
|
|
kv_.init(db_name).ensure();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
kv_.set(td::to_string(i % 10), td::to_string(i));
|
|
|
|
}
|
|
|
|
td::Scheduler::instance()->finish();
|
|
|
|
}
|
|
|
|
int n_;
|
|
|
|
KeyValueT kv_;
|
|
|
|
};
|
|
|
|
td::create_actor_on_scheduler<Worker>("Worker", 0, n_, "test_tddb").release();
|
|
|
|
}
|
|
|
|
int n_;
|
|
|
|
};
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up_n(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.create_actor_unsafe<Main>(1, "Main", n).release();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <bool is_encrypted = false>
|
2021-07-04 04:58:54 +02:00
|
|
|
class SqliteKVBench final : public td::Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
td::SqliteDb db;
|
2021-07-03 22:51:36 +02:00
|
|
|
td::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return PSTRING() << "SqliteKV " << td::tag("is_encrypted", is_encrypted);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
td::string path = "testdb.sqlite";
|
|
|
|
td::SqliteDb::destroy(path).ignore();
|
|
|
|
if (is_encrypted) {
|
2021-09-22 18:04:56 +02:00
|
|
|
db = td::SqliteDb::change_key(path, true, td::DbKey::password("cucumber"), td::DbKey::empty()).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2021-09-22 18:04:56 +02:00
|
|
|
db = td::SqliteDb::open_with_key(path, true, td::DbKey::empty()).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
db.exec("PRAGMA encoding=\"UTF-8\"").ensure();
|
|
|
|
db.exec("PRAGMA synchronous=NORMAL").ensure();
|
|
|
|
db.exec("PRAGMA journal_mode=WAL").ensure();
|
|
|
|
db.exec("PRAGMA temp_store=MEMORY").ensure();
|
|
|
|
db.exec("DROP TABLE IF EXISTS KV").ensure();
|
|
|
|
db.exec("CREATE TABLE IF NOT EXISTS KV (k BLOB PRIMARY KEY, v BLOB)").ensure();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
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++) {
|
|
|
|
auto key = td::to_string(i % 10);
|
|
|
|
auto value = td::to_string(i);
|
|
|
|
stmt.bind_blob(1, key).ensure();
|
|
|
|
stmt.bind_blob(2, value).ensure();
|
|
|
|
stmt.step().ensure();
|
|
|
|
CHECK(!stmt.can_step());
|
|
|
|
stmt.reset();
|
|
|
|
|
|
|
|
if (i % 10 == 0) {
|
|
|
|
db.exec("COMMIT TRANSACTION").ensure();
|
|
|
|
db.exec("BEGIN TRANSACTION").ensure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.exec("COMMIT TRANSACTION").ensure();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static td::Status init_db(td::SqliteDb &db) {
|
|
|
|
TRY_STATUS(db.exec("PRAGMA encoding=\"UTF-8\""));
|
|
|
|
TRY_STATUS(db.exec("PRAGMA journal_mode=WAL"));
|
|
|
|
|
|
|
|
TRY_STATUS(db.exec("PRAGMA synchronous=NORMAL"));
|
|
|
|
TRY_STATUS(db.exec("PRAGMA temp_store=MEMORY"));
|
|
|
|
// TRY_STATUS(db.exec("PRAGMA secure_delete=1"));
|
|
|
|
|
|
|
|
return td::Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SqliteKeyValueAsyncBench final : public td::Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
td::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "SqliteKeyValueAsync";
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
do_start_up().ensure();
|
|
|
|
scheduler_->start();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2018-09-18 15:43:16 +02:00
|
|
|
auto guard = scheduler_->get_main_guard();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
auto key = td::to_string(i % 10);
|
|
|
|
auto value = td::to_string(i);
|
|
|
|
sqlite_kv_async_->set(key, value, td::Auto());
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler_->run_main(0.1);
|
|
|
|
{
|
2018-09-18 15:43:16 +02:00
|
|
|
auto guard = scheduler_->get_main_guard();
|
2018-12-31 20:04:05 +01:00
|
|
|
sqlite_kv_async_.reset();
|
|
|
|
sqlite_kv_safe_.reset();
|
|
|
|
sql_connection_->close_and_destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduler_->finish();
|
|
|
|
scheduler_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-27 03:19:03 +02:00
|
|
|
td::unique_ptr<td::ConcurrentScheduler> scheduler_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::shared_ptr<td::SqliteConnectionSafe> sql_connection_;
|
|
|
|
std::shared_ptr<td::SqliteKeyValueSafe> sqlite_kv_safe_;
|
2018-09-27 03:19:03 +02:00
|
|
|
td::unique_ptr<td::SqliteKeyValueAsyncInterface> sqlite_kv_async_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
td::Status do_start_up() {
|
2022-09-14 14:06:52 +02:00
|
|
|
scheduler_ = td::make_unique<td::ConcurrentScheduler>(1, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-18 15:43:16 +02:00
|
|
|
auto guard = scheduler_->get_main_guard();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
td::string sql_db_name = "testdb.sqlite";
|
|
|
|
td::SqliteDb::destroy(sql_db_name).ignore();
|
|
|
|
|
2021-09-22 16:20:10 +02:00
|
|
|
sql_connection_ = std::make_shared<td::SqliteConnectionSafe>(sql_db_name, td::DbKey::empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &db = sql_connection_->get();
|
|
|
|
TRY_STATUS(init_db(db));
|
|
|
|
|
|
|
|
sqlite_kv_safe_ = std::make_shared<td::SqliteKeyValueSafe>("common", sql_connection_);
|
|
|
|
sqlite_kv_async_ = create_sqlite_key_value_async(sqlite_kv_safe_, 0);
|
|
|
|
|
|
|
|
return td::Status::OK();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SeqKvBench final : public td::Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
td::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "SeqKvBench";
|
|
|
|
}
|
|
|
|
|
|
|
|
td::SeqKeyValue kv;
|
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
|
|
|
kv.set(PSLICE() << i % 10, PSLICE() << i);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <bool is_encrypted = false>
|
2021-07-04 04:58:54 +02:00
|
|
|
class BinlogKeyValueBench final : public td::Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
td::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return PSTRING() << "BinlogKeyValue " << td::tag("is_encrypted", is_encrypted);
|
|
|
|
}
|
|
|
|
|
|
|
|
td::BinlogKeyValue<td::Binlog> kv;
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
td::SqliteDb::destroy("test_binlog").ignore();
|
|
|
|
kv.init("test_binlog", is_encrypted ? td::DbKey::password("cucumber") : td::DbKey::empty()).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++) {
|
|
|
|
kv.set(td::to_string(i % 10), td::to_string(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(WARNING));
|
|
|
|
bench(BinlogKeyValueBench<true>());
|
|
|
|
bench(BinlogKeyValueBench<false>());
|
|
|
|
bench(SqliteKVBench<false>());
|
|
|
|
bench(SqliteKVBench<true>());
|
|
|
|
bench(SqliteKeyValueAsyncBench());
|
|
|
|
bench(TdKvBench<td::BinlogKeyValue<td::Binlog>>("BinlogKeyValue<Binlog>"));
|
|
|
|
bench(TdKvBench<td::BinlogKeyValue<td::ConcurrentBinlog>>("BinlogKeyValue<ConcurrentBinlog>"));
|
|
|
|
bench(SeqKvBench());
|
|
|
|
}
|