成员:陈予曈,朱陈媛
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.

108 lines
3.3 KiB

  1. #include "gtest/gtest.h"
  2. #include "db/NewDB.h" // NewDB 的头文件
  3. #include "leveldb/env.h"
  4. #include "leveldb/db.h"
  5. #include <chrono> // 用于计时
  6. #include <random> // 用于生成随机数
  7. #include <vector> // 用于存储延迟
  8. using namespace std;
  9. using namespace leveldb;
  10. // 随机生成字符串的辅助函数
  11. std::string GenerateRandomString(size_t length) {
  12. const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  13. const size_t max_index = sizeof(charset) - 1;
  14. std::random_device rd;
  15. std::mt19937 generator(rd());
  16. std::uniform_int_distribution<size_t> distribution(0, max_index);
  17. std::string random_str;
  18. for (size_t i = 0; i < length; ++i) {
  19. random_str += charset[distribution(generator)];
  20. }
  21. return random_str;
  22. }
  23. // 打开数据库的辅助函数
  24. Status OpenDB(std::string dbName, NewDB** db) {
  25. Options options;
  26. options.create_if_missing = true;
  27. Status s = NewDB::Open(options, dbName, db);
  28. if (!s.ok()) {
  29. cerr << "Error opening database: " << s.ToString() << endl;
  30. }
  31. return s;
  32. }
  33. std::string testdbname = "testdb_for_performance";
  34. // 延迟单条指令测试
  35. void TestLatency(int num_operations, std::vector<int64_t>& lat_res) {
  36. int64_t latency = 0;
  37. auto end_time = std::chrono::steady_clock::now();
  38. auto last_time = end_time;
  39. for (int i = 0; i < num_operations; ++i) {
  40. // 记录操作的延迟
  41. end_time = std::chrono::steady_clock::now();
  42. latency = std::chrono::duration_cast<std::chrono::milliseconds>(
  43. end_time - last_time).count();
  44. last_time = end_time;
  45. lat_res.emplace_back(latency);
  46. }
  47. }
  48. // 吞吐量测试
  49. TEST(TestNewDB, PutFieldsThroughputTest) {
  50. // 创建 NewDB 实例
  51. NewDB* db;
  52. ASSERT_TRUE(OpenDB(testdbname, &db).ok());
  53. // 插入索引字段
  54. db->CreateIndexOnField("address");
  55. // 延迟测试
  56. vector<int64_t> latencies; // 存储每次插入的延迟
  57. int num_inserts = 100000; // 插入的条数
  58. // 测试插入吞吐量
  59. auto start_time = std::chrono::high_resolution_clock::now(); // 记录开始时间
  60. for (int i = 0; i < num_inserts; ++i) {
  61. std::string key = "k_" + std::to_string(i);
  62. // 随机生成字段内容
  63. FieldArray fields = {
  64. {"name", "Customer#" + std::to_string(i)},
  65. {"address", GenerateRandomString(10)}, // 随机生成地址
  66. {"phone", GenerateRandomString(12)} // 随机生成电话号码
  67. };
  68. db->Put_fields(WriteOptions(), key, fields);
  69. }
  70. // 延迟测试
  71. TestLatency(num_inserts, latencies);
  72. auto end_time = std::chrono::high_resolution_clock::now(); // 记录结束时间
  73. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
  74. // 计算吞吐量并输出
  75. cout << "Throughput: " << num_inserts * 1000 / duration << " OPS" << endl;
  76. // 输出延迟
  77. // for (size_t i = 0; i < latencies.size(); ++i) {
  78. // cout << "Latency for operation " << i + 1 << ": " << latencies[i] << " ms" << endl;
  79. // }
  80. cout << "Total Latency " << duration << " ms" << endl;
  81. // 清理数据库
  82. delete db;
  83. }
  84. // 主函数运行所有测试
  85. int main(int argc, char** argv) {
  86. testing::InitGoogleTest(&argc, argv);
  87. return RUN_ALL_TESTS();
  88. }