Skip to content

Commit c86d47b

Browse files
committed
refactor(.gitignore): 更新 .gitignore 文件,忽略所有 .sh 脚本
perf(ThreadPool): 调整线程池默认线程数为硬件并发数 feat(rmdb): 添加函数执行时间计时功能 - 在 rmdb.cpp 中加入 ENABLE_TIMER 宏定义 - 添加 GetFunctionTime 相关代码以进行函数计时
1 parent d0db427 commit c86d47b

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ performance_testing/*.folded
7878

7979
.cunzhi-memory/
8080

81-
make.sh
81+
*.sh

src/common/getFunctionTime.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <atomic>
4+
#include <cstddef>
5+
#include <chrono>
6+
#include <iostream>
7+
8+
namespace timedetail {
9+
10+
class TotTime {
11+
public:
12+
static TotTime& getInstance() {
13+
static TotTime instance;
14+
return instance;
15+
}
16+
17+
void add(int64_t time) { tot_time.fetch_add(time); }
18+
19+
int64_t get() { return tot_time; }
20+
21+
void print() {
22+
std::clog << "\033[0m\033[1;31m" << "Total time: " << tot_time.load() / 1e6 << " ms" << "\033[0m" << std::endl;
23+
}
24+
25+
private:
26+
std::atomic_int64_t tot_time = 0;
27+
TotTime() = default;
28+
};
29+
30+
}
31+
32+
class GetTime {
33+
private:
34+
std::chrono::high_resolution_clock::time_point start;
35+
36+
public:
37+
GetTime() { start = std::chrono::high_resolution_clock::now(); }
38+
~GetTime() {
39+
auto now = std::chrono::high_resolution_clock::now();
40+
auto use = std::chrono::duration_cast<std::chrono::nanoseconds>(now - start).count();
41+
timedetail::TotTime::getInstance().add(use);
42+
}
43+
};
44+
#ifndef AcquisitionTime
45+
#define AcquisitionTime GetTime();
46+
#endif
47+
48+
#ifndef PrintFunctionTime
49+
#define PrintFunctionTime timedetail::TotTime::getInstance().print();
50+
#endif

src/common/parallel/ThreadPool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ThreadPool {
3737
std::condition_variable condition;
3838
};
3939

40-
inline size_t ThreadPool::thread_num_ = std::max(4u, std::thread::hardware_concurrency() / 2);
40+
inline size_t ThreadPool::thread_num_ = std::max(4u, std::thread::hardware_concurrency());
4141

4242
inline ThreadPool::ThreadPool(size_t threads) : stop(false) {
4343
for (size_t i = 0; i < threads; ++i)

src/rmdb.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@
3535
#include "portal.h"
3636
#include "recovery/log_recovery.h"
3737
#include "common/utils/Format.h"
38+
#include "common/getFunctionTime.h"
3839

3940
#define SOCK_PORT 8765
4041
#define MAX_CONN_LIMIT 8
4142

4243
// 是否开启 std::cout
4344
constexpr bool ENABLE_COUT = false;
45+
// 是否输出函数计时结果
46+
constexpr bool ENABLE_TIMER = true;
4447

4548
// #define ENABLE_SERIALIZE
4649

@@ -92,6 +95,9 @@ void sigint_handler(int signo) {
9295
should_exit = true;
9396
// log_manager->flush_log_to_disk();
9497
Print("The Server receive Crtl+C, will been closed\n");
98+
if constexpr (ENABLE_TIMER) {
99+
PrintFunctionTime
100+
}
95101
longjmp(jmpbuf, 1);
96102
}
97103

0 commit comments

Comments
 (0)