-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremain_qps.cc
161 lines (135 loc) · 3.96 KB
/
remain_qps.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "remain_qps.h"
#include "options.h"
#include <atomic>
#include <cstdint>
#include <iostream>
#include <mysql/mysql.h>
#include <thread>
#include <unistd.h>
extern uint detail_log;
extern char *database;
extern uint64_t report_interval;
extern uint64_t test_time;
extern uint64_t concurrency;
extern uint64_t test_qps;
static Statistics qps_state;
static Statistics qps_per_second;
static std::atomic<uint32_t> active_threads{0};
static std::atomic<time_t> pre_time{0};
static std::atomic_bool can_continue{true};
static std::atomic_bool should_quit{false};
void RemainQPSTest::run(const std::vector<std::string> &querys) {
while (!should_quit) {
if (can_continue) {
conns_prepare();
basic_query(querys);
conns_close();
} else {
usleep(1);
}
}
}
int RemainQPSTest::basic_query(const std::vector<std::string> &querys) {
int res = 0;
MYSQL_RES *mysql_res = nullptr;
MYSQL_ROW row;
for (auto &query : querys) {
if (!can_continue) {
break;
}
res = mysql_query(m_conn_, query.data());
if (res != 0) {
qps_state.increase_cnt_failed();
qps_per_second.increase_cnt_failed();
std::cout << "Failed to test consistency, sql: " << query
<< ", errno: " << mysql_errno(m_conn_)
<< ", errmsg: " << mysql_error(m_conn_);
return -1;
}
qps_state.increase_cnt_total();
qps_per_second.increase_cnt_total();
mysql_res = mysql_store_result(m_conn_);
mysql_free_result(mysql_res);
}
return res;
}
void start_detect_qps() {
while (!should_quit) {
time_t now_time = time(nullptr);
if (now_time == pre_time) {
if (qps_per_second.get_cnt_total() >= test_qps) {
can_continue.store(false);
}
} else {
qps_per_second.clear();
can_continue.store(true);
pre_time = now_time;
}
usleep(1);
}
}
void start_remain_qps_test(int thread_id,
const std::vector<std::string> &querys) {
if (detail_log) {
std::cout << "start thread: " << thread_id << std::endl;
}
RemainQPSTest t(database, 0, test_qps);
t.run(querys);
t.cleanup();
active_threads--;
if (detail_log) {
std::cout << "stop thread: " << thread_id << std::endl;
}
return;
}
static void print_result_interval() {
Statistics new_state, pre_state;
pre_state = qps_state;
time_t start_time = time(NULL);
time_t end_time;
if (report_interval != 0) {
while (active_threads.load() != 0) {
sleep(report_interval);
new_state = qps_state;
std::cout << "qps: "
<< (new_state.get_cnt_total() - pre_state.get_cnt_total()) /
report_interval;
std::cout << ", failed qps: "
<< (new_state.get_cnt_failed() - pre_state.get_cnt_failed()) /
report_interval;
std::cout << ", active threads : " << active_threads.load() << std::endl;
pre_state = new_state;
end_time = time(NULL);
uint64_t run_time = end_time - start_time;
if (run_time >= test_time) {
should_quit.store(true);
break;
}
}
}
}
static void print_result_summarize() {
std::cout << "mean qps in all time: " << qps_state.get_cnt_total() / test_time
<< ", failed cnt: " << qps_state.get_cnt_failed() / test_time
<< std::endl;
}
int main_remain_qps() {
std::thread *ct_threads[concurrency];
std::vector<std::string> querys = get_querys_from_file();
pre_time = time(nullptr);
std::thread *detect_qps_thread = new std::thread(start_detect_qps);
for (uint thread_id = 0; thread_id < concurrency; thread_id++) {
ct_threads[thread_id] =
new std::thread(start_remain_qps_test, thread_id, querys);
active_threads++;
}
print_result_interval();
for (uint thread_id = 0; thread_id < concurrency; thread_id++) {
ct_threads[thread_id]->join();
delete ct_threads[thread_id];
}
detect_qps_thread->join();
delete detect_qps_thread;
print_result_summarize();
return 0;
}