You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

85 lines
2.4 KiB

#include <chrono>
#include <iomanip>
#include <iostream>
#include <leveldb/db.h>
#include <leveldb/options.h>
#include <numeric>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include "leveldb/env.h"
#include "gtest/gtest.h"
using namespace leveldb;
Status OpenDB(std::string dbName, DB** db) {
Options options;
options.create_if_missing = true;
return DB::Open(options, dbName, db);
}
// 测试函数,使用多线程进行并发读写测试
TEST(ConcurrentTest, Basic) {
DB* db;
WriteOptions writeOptions;
ReadOptions readOptions;
if (!OpenDB("testdb6", &db).ok()) {
std::cerr << "open db failed" << std::endl;
abort();
}
const int numThreads = 10;
const int numOperationsPerThread = 100;
std::vector<std::thread> threads;
// 创建写线程
for (int i = 0; i < numThreads / 2; ++i) {
threads.emplace_back([&db, i, numOperationsPerThread]() {
WriteOptions writeOptions;
for (int j = 0; j < numOperationsPerThread; ++j) {
std::string key = "key_" + std::to_string(i * numOperationsPerThread + j);
FieldArray fields = {
{"name", "Customer" + std::to_string(i * numOperationsPerThread + j)},
{"address", "Address" + std::to_string(i * numOperationsPerThread + j)},
{"phone", "1234567890"}
};
db->Put_Fields(writeOptions, key, fields);
}
});
}
// 创建读线程
for (int i = 0; i < numThreads / 2; ++i) {
threads.emplace_back([&db, i, numOperationsPerThread]() {
ReadOptions readOptions;
Field queryField = {"name", "Customer"};
for (int j = 0; j < numOperationsPerThread; ++j) {
std::string key = "key_" + std::to_string(i * numOperationsPerThread + j % (numOperationsPerThread * (numThreads / 2)));
FieldArray fields;
db->Get_Fields(readOptions, key, fields);
bool found = false;
for (const auto& field : fields) {
if (field.name == queryField.name &&
field.value.substr(0, queryField.value.size()) == queryField.value) { // 部分匹配以测试前缀搜索的效果
found = true;
break;
}
}
}
});
}
// 等待所有线程完成
for (auto& thread : threads) {
thread.join();
}
delete db;
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}