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/telegram/DialogId.h"
|
|
|
|
#include "td/telegram/MessageId.h"
|
|
|
|
#include "td/telegram/MessagesDb.h"
|
2018-11-28 18:18:50 +01:00
|
|
|
#include "td/telegram/NotificationId.h"
|
2019-11-26 17:33:18 +01:00
|
|
|
#include "td/telegram/ServerMessageId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
#include "td/db/DbKey.h"
|
2019-01-07 01:17:11 +01:00
|
|
|
#include "td/db/SqliteConnectionSafe.h"
|
2019-04-26 00:47:25 +02:00
|
|
|
#include "td/db/SqliteDb.h"
|
2019-01-07 01:17:11 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/benchmark.h"
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
static td::Status init_db(td::SqliteDb &db) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(db.exec("PRAGMA encoding=\"UTF-8\""));
|
|
|
|
TRY_STATUS(db.exec("PRAGMA synchronous=NORMAL"));
|
|
|
|
TRY_STATUS(db.exec("PRAGMA journal_mode=WAL"));
|
|
|
|
TRY_STATUS(db.exec("PRAGMA temp_store=MEMORY"));
|
|
|
|
TRY_STATUS(db.exec("PRAGMA secure_delete=1"));
|
2021-11-06 21:45:02 +01:00
|
|
|
return td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
class MessagesDbBench final : public td::Benchmark {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-11-06 21:45:02 +01:00
|
|
|
td::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "MessagesDb";
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "START UP";
|
|
|
|
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 += 20) {
|
2021-11-06 21:45:02 +01:00
|
|
|
auto dialog_id = td::DialogId(td::UserId(static_cast<td::int64>(td::Random::fast(1, 100))));
|
|
|
|
auto message_id_raw = td::Random::fast(1, 100000);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int j = 0; j < 20; j++) {
|
2021-11-06 21:45:02 +01:00
|
|
|
auto message_id = td::MessageId{td::ServerMessageId{message_id_raw + j}};
|
|
|
|
auto unique_message_id = td::ServerMessageId{i + 1};
|
|
|
|
auto sender_user_id = td::UserId(static_cast<td::int64>(td::Random::fast(1, 1000)));
|
2018-12-31 20:04:05 +01:00
|
|
|
auto random_id = i + 1;
|
|
|
|
auto ttl_expires_at = 0;
|
2021-11-06 21:45:02 +01:00
|
|
|
auto data = td::BufferSlice(td::Random::fast(100, 299));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// use async on same thread.
|
|
|
|
messages_db_async_->add_message({dialog_id, message_id}, unique_message_id, sender_user_id, random_id,
|
2021-11-06 21:45:02 +01:00
|
|
|
ttl_expires_at, 0, 0, "", td::NotificationId(), td::MessageId(),
|
|
|
|
std::move(data), td::Promise<>());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
sql_connection_.reset();
|
|
|
|
messages_db_sync_safe_.reset();
|
|
|
|
messages_db_async_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduler_->finish();
|
|
|
|
scheduler_.reset();
|
|
|
|
LOG(ERROR) << "TEAR DOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-11-06 21:45:02 +01:00
|
|
|
td::unique_ptr<td::ConcurrentScheduler> scheduler_;
|
|
|
|
std::shared_ptr<td::SqliteConnectionSafe> sql_connection_;
|
|
|
|
std::shared_ptr<td::MessagesDbSyncSafeInterface> messages_db_sync_safe_;
|
|
|
|
std::shared_ptr<td::MessagesDbAsyncInterface> messages_db_async_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
td::Status do_start_up() {
|
|
|
|
scheduler_ = td::make_unique<td::ConcurrentScheduler>();
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler_->init(1);
|
|
|
|
|
2018-09-18 15:43:16 +02:00
|
|
|
auto guard = scheduler_->get_main_guard();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
td::string sql_db_name = "testdb.sqlite";
|
|
|
|
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));
|
|
|
|
|
|
|
|
db.exec("BEGIN TRANSACTION").ensure();
|
|
|
|
// version == 0 ==> db will be destroyed
|
|
|
|
TRY_STATUS(init_messages_db(db, 0));
|
|
|
|
db.exec("COMMIT TRANSACTION").ensure();
|
|
|
|
|
2021-11-06 21:45:02 +01:00
|
|
|
messages_db_sync_safe_ = td::create_messages_db_sync(sql_connection_);
|
|
|
|
messages_db_async_ = td::create_messages_db_async(messages_db_sync_safe_, 0);
|
|
|
|
return td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(WARNING));
|
2021-11-06 21:45:02 +01:00
|
|
|
td::bench(MessagesDbBench());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|