diff --git a/app/wizard_demo/pkv/CMakeLists.txt b/app/wizard_demo/pkv/CMakeLists.txt index a3fb55b6c..616d6fad5 100644 --- a/app/wizard_demo/pkv/CMakeLists.txt +++ b/app/wizard_demo/pkv/CMakeLists.txt @@ -135,7 +135,7 @@ set(output_path ${CMAKE_CURRENT_SOURCE_DIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${output_path}) link_directories(${output_path}) -add_executable(pkv ${src_files} test/test_coder.cpp test/test_coder.h) +add_executable(pkv ${src_files} test/test_coder.cpp test/test_coder.h test/test_db.cpp test/test_db.h) target_link_libraries(pkv ${lib_all}) ############################################################################### diff --git a/app/wizard_demo/pkv/db/db.h b/app/wizard_demo/pkv/db/db.h index 8ffcbf180..ad445f49b 100644 --- a/app/wizard_demo/pkv/db/db.h +++ b/app/wizard_demo/pkv/db/db.h @@ -15,6 +15,9 @@ class db { virtual bool get(const std::string& key, std::string& value) = 0; virtual bool del(const std::string& key) = 0; +public: + virtual const char* get_dbtype() const = 0; + static shared_db create_rdb(); static shared_db create_wdb(); }; diff --git a/app/wizard_demo/pkv/db/rocksdb/rdb.h b/app/wizard_demo/pkv/db/rocksdb/rdb.h index e8761fdc9..38f9c1a49 100644 --- a/app/wizard_demo/pkv/db/rocksdb/rdb.h +++ b/app/wizard_demo/pkv/db/rocksdb/rdb.h @@ -28,6 +28,12 @@ class rdb : public db { // @override bool del(const std::string& key) override; +public: + // @override + const char* get_dbtype() const override { + return "rdb"; + } + private: std::string path_; rocksdb::DB* db_; diff --git a/app/wizard_demo/pkv/db/wt/wdb.h b/app/wizard_demo/pkv/db/wt/wdb.h index 0d6f15a56..8dd711667 100644 --- a/app/wizard_demo/pkv/db/wt/wdb.h +++ b/app/wizard_demo/pkv/db/wt/wdb.h @@ -32,6 +32,12 @@ class wdb : public db { // @override bool del(const std::string& key) override; +public: + // @override + const char* get_dbtype() const override { + return "wdb"; + } + public: WT_CONNECTION* get_db() const { return db_; diff --git a/app/wizard_demo/pkv/main.cpp b/app/wizard_demo/pkv/main.cpp index e868e5665..4b31e1269 100644 --- a/app/wizard_demo/pkv/main.cpp +++ b/app/wizard_demo/pkv/main.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include #include "test/test_coder.h" +#include "test/test_db.h" #include "master_service.h" static void on_sigint(int) { @@ -51,6 +52,10 @@ static bool test_redis_coder(const char* file, size_t max) { return true; } +static void test_db(const char* dbpath, size_t max) { + pkv::test_db::bench(dbpath, max); +} + int main(int argc, char *argv[]) { acl::acl_cpp_init(); @@ -65,6 +70,7 @@ int main(int argc, char *argv[]) { max = std::atoi(argv[3]); } test_redis_coder(file, max); + test_db("./data/test", max); return 0; } diff --git a/app/wizard_demo/pkv/test/test_db.cpp b/app/wizard_demo/pkv/test/test_db.cpp new file mode 100644 index 000000000..3c2009449 --- /dev/null +++ b/app/wizard_demo/pkv/test/test_db.cpp @@ -0,0 +1,101 @@ +// +// Created by zsx on 2023/8/13. +// + +#include "stdafx.h" + +#include "test_db.h" + +namespace pkv { + +test_db::test_db() {} + +test_db::~test_db() {} + +void test_db::bench(const std::string &path, size_t max) { + rdb_bench(path, max); + wdb_bench(path, max); +} + +void test_db::rdb_bench(const std::string &path, size_t max) { + auto db = db::create_rdb(); + if (!db->open(path.c_str())) { + printf("%s: open %s db error %s\r\n", + __func__, path.c_str(), acl::last_serror()); + } else { + bench(db, max); + // Close the db + db = nullptr; + } +} +void test_db::wdb_bench(const std::string &path, size_t max) { + auto db = db::create_wdb(); + if (!db->open(path.c_str())) { + printf("%s: open %s db error %s\r\n", + __func__, path.c_str(), acl::last_serror()); + } else { + bench(db, max); + // Close the db + db = nullptr; + } +} + +void test_db::bench(shared_db &db, size_t max) { + printf("Begin test %s, count=%zd\r\n", db->get_dbtype(), max); + + struct timeval begin; + gettimeofday(&begin, nullptr); + + size_t n = bench_set(db, max); + + struct timeval end; + gettimeofday(&end, nullptr); + double cost = acl::stamp_sub(end, begin); + double speed = (n * 1000) / (cost > 0 ? cost : 0.0001); + printf("%s: count=%zd, cost=%.2f ms, set speed=%.2f\r\n", + db->get_dbtype(), n, cost, speed); + + gettimeofday(&begin, nullptr); + n = bench_get(db, max); + gettimeofday(&end, nullptr); + + cost = acl::stamp_sub(end, begin); + speed = (n * 1000) / (cost > 0 ? cost : 0.0001); + printf("%s: count=%zd, cost=%.2f ms, get speed=%.2f\r\n", + db->get_dbtype(), n, cost, speed); +} + +size_t test_db::bench_set(pkv::shared_db &db, size_t max) { + size_t i; + for (i = 0; i < max; i++) { + std::string key("key-"); + key += std::to_string(i); + + std::string val("val-"); + val += std::to_string(i); + if (!db->set(key, val)) { + printf("%s: dbtype=%s, set error, key=%s, val=%s\r\n", + __func__, db->get_dbtype(), key.c_str(), val.c_str()); + break; + } + } + return i; +} + +size_t test_db::bench_get(pkv::shared_db &db, size_t max) { + size_t i; + for (i = 0; i < max; i++) { + std::string key("key-"); + key += std::to_string(i); + + std::string val; + if (!db->get(key, val)) { + printf("%s: dbtype=%s, set error, key=%s\r\n", + __func__, db->get_dbtype(), key.c_str()); + break; + } + } + return i; +} + +} // namespace pkv \ No newline at end of file diff --git a/app/wizard_demo/pkv/test/test_db.h b/app/wizard_demo/pkv/test/test_db.h new file mode 100644 index 000000000..ce1204bd3 --- /dev/null +++ b/app/wizard_demo/pkv/test/test_db.h @@ -0,0 +1,23 @@ +// +// Created by zsx on 2023/8/13. +// + +#pragma once +#include "db/db.h" + +namespace pkv { + +class test_db { +public: + test_db(); + ~test_db(); + + static void bench(const std::string& path, size_t max); + static void bench(shared_db& db, size_t max); + static size_t bench_set(shared_db& db, size_t max); + static size_t bench_get(shared_db& db, size_t max); + static void rdb_bench(const std::string& path, size_t max); + static void wdb_bench(const std::string& path, size_t max); +}; + +} // namespace pkv \ No newline at end of file