Skip to content

Commit

Permalink
test db module in pkv.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Aug 13, 2023
1 parent d75199b commit 5afa78e
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/wizard_demo/pkv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

###############################################################################
3 changes: 3 additions & 0 deletions app/wizard_demo/pkv/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
6 changes: 6 additions & 0 deletions app/wizard_demo/pkv/db/rocksdb/rdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
6 changes: 6 additions & 0 deletions app/wizard_demo/pkv/db/wt/wdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
6 changes: 6 additions & 0 deletions app/wizard_demo/pkv/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include <signal.h>
#include "test/test_coder.h"
#include "test/test_db.h"
#include "master_service.h"

static void on_sigint(int) {
Expand Down Expand Up @@ -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();

Expand All @@ -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;
}

Expand Down
101 changes: 101 additions & 0 deletions app/wizard_demo/pkv/test/test_db.cpp
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions app/wizard_demo/pkv/test/test_db.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5afa78e

Please sign in to comment.