Repository navigation
Expand file tree
/
Copy pathtest.cpp
More file actions
168 lines (153 loc) · 5.23 KB
/
Copy pathtest.cpp
File metadata and controls
168 lines (153 loc) · 5.23 KB
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
161
162
163
164
165
166
167
168
#include <thread>
#include <iostream>
#include <map>
#include <vector>
#include <cstdlib>
#include "wheel_timer.h"
#define ASSERT(x) \
if (!(x)) { \
std::cout << "Assertion failed " << #x << " on line " << __LINE__ << std::endl; \
exit(1); \
}
static std::vector<uint32_t> wait_expired(WheelTimer &t, uint32_t delay_ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
auto ret = t.Update();
if (!ret.empty()) {
return ret;
}
// Alignment to the 10ms tick can require one extra tick after the delay.
std::this_thread::sleep_for(INTERVAL);
return t.Update();
}
int test() {
{
WheelTimer t;
auto id = t.Add(0);
auto ret = t.Update();
ASSERT(ret.size() == 1);
ASSERT(ret[0] == id);
ASSERT(t.Size() == 0);
std::cout << "tid " << id << " zero-delay timeout" << std::endl;
}
{
WheelTimer t;
auto id = t.Add(10);
ASSERT(t.Update().empty());
auto ret = wait_expired(t, 10);
ASSERT(ret.size() == 1);
ASSERT(ret[0] == id);
ASSERT(t.Size() == 0);
std::cout << "tid " << id << " one-tick timeout" << std::endl;
}
WheelTimer t;
auto t1 = t.Add(1000);
auto t2 = t.Add(2000);
auto t3 = t.Add(3000);
ASSERT(t.Size() == 3);
auto ret = wait_expired(t, 1000);
ASSERT(ret.size() == 1);
ASSERT(ret[0] == t1);
ASSERT(t.Size() == 2);
std::cout << "tid " << t1 << " timeout" << std::endl;
ret = wait_expired(t, 1000);
ASSERT(ret.size() == 1);
ASSERT(ret[0] == t2);
ASSERT(t.Size() == 1);
std::cout << "tid " << t2 << " timeout" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
auto ok = t.Del(t3);
ASSERT(ok);
ASSERT(t.Size() == 0);
ret = t.Update();
ASSERT(ret.size() == 0);
return 0;
}
int benchmark() {
WheelTimer t;
std::unordered_set<uint32_t> ids;
auto begin = std::chrono::system_clock::now();
for (int i = 0; i < 1000000; i++) {
auto id = t.Add(1000);
ids.insert(id);
}
ASSERT(t.Size() == 1000000);
std::cout << "add 1000000 timers cost " << std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - begin).count() << "ms" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
WheelTimer tmp;
for (int i = 0; i < 1000000; i++) {
tmp.Add(1000);
}
begin = std::chrono::system_clock::now();
for (auto id: ids) {
auto ok = tmp.Del(id);
ASSERT(ok);
}
ASSERT(tmp.Size() == 0);
std::cout << "del 1000000 timers cost " << std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - begin).count() << "ms" << std::endl;
begin = std::chrono::system_clock::now();
auto ret = t.Update();
std::cout << "update cost " << std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - begin).count() << "ms" << std::endl;
for (auto id: ret) {
ASSERT(ids.find(id) != ids.end());
ids.erase(id);
}
ASSERT(ids.empty());
ASSERT(t.Size() == 0);
std::unordered_map<uint32_t, std::chrono::time_point<std::chrono::system_clock>> id_time;
int count = 0;
std::map<uint32_t, int> diffs;
std::chrono::duration<int64_t, std::nano> max_add_time(0);
std::chrono::duration<int64_t, std::nano> max_update_time(0);
while (true) {
if (count >= 100000) {
if (id_time.empty()) {
break;
}
} else {
auto random_time = rand() % 100000 + 100;
auto now = std::chrono::system_clock::now();
auto id = t.Add(random_time);
auto tmp = std::chrono::system_clock::now();
if ((tmp - now) > max_add_time) {
max_add_time = tmp - now;
}
id_time[id] = now + std::chrono::milliseconds(random_time);
}
count = count + 1;
auto tmp = std::chrono::system_clock::now();
auto ids = t.Update();
auto now = std::chrono::system_clock::now();
if ((now - tmp) > max_update_time) {
max_update_time = now - tmp;
}
for (auto id: ids) {
auto it = id_time.find(id);
ASSERT(it != id_time.end());
auto expect_time_ms = it->second;
auto diff = now - expect_time_ms;
diffs[std::chrono::duration_cast<std::chrono::milliseconds>(diff).count()]++;
if (id % 10000 == 0) {
std::cout << "tid " << id << " diff " << std::chrono::duration_cast<std::chrono::milliseconds>(diff)
.count() << "ms" << std::endl;
}
ASSERT(now >= expect_time_ms);
id_time.erase(it);
}
}
for (auto &it: diffs) {
std::cout << "diff " << it.first << "ms count " << it.second << std::endl;
}
std::cout << "max add time " << max_add_time.count() / 1000000 << "ms" << std::endl;
std::cout << "max update time " << max_update_time.count() / 1000000 << "ms" << std::endl;
std::cout << "benchmark passed" << std::endl;
return 0;
}
int main() {
srand(0);
test();
benchmark();
return 0;
}