-
Notifications
You must be signed in to change notification settings - Fork 0
/
latency_test.cc
169 lines (129 loc) · 4.55 KB
/
latency_test.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
161
162
163
164
165
166
167
168
169
#include "test_common.h"
#include "latency_collector.h"
#include "latency_dump.h"
#include <thread>
#include <stdio.h>
struct test_args : TestSuite::ThreadArgs {
LatencyCollector* lat;
};
LatencyCollector* global_lat;
int insert_thread(TestSuite::ThreadArgs* t_args)
{
collectFuncLatency(global_lat);
test_args *args = (test_args*)t_args;
size_t n = 1024 * 1;
for (size_t i=0; i<n; ++i) {
int item_no = std::rand() % 16;
uint64_t latency = 100 + (std::rand() % 100);
args->lat->addLatency(std::to_string(item_no), latency);
}
return 0;
}
int MT_basic_insert_test() {
global_lat = new LatencyCollector();
size_t i;
size_t n_threads = 8;
LatencyCollector lat;
std::vector<TestSuite::ThreadHolder> t_hdl(n_threads);
std::vector<test_args> args(n_threads);
for (i=0; i<n_threads; ++i) {
args[i].lat = ⪫
t_hdl[i].spawn(&args[i], insert_thread, nullptr);
}
for (i=0; i<n_threads; ++i){
t_hdl[i].join();
}
LatencyDumpDefaultImpl default_dump;
TestSuite::Msg msg_stream;
msg_stream << lat.dump(&default_dump) << std::endl;
msg_stream << global_lat->dump(&default_dump) << std::endl;
delete global_lat;
global_lat = nullptr;
return 0;
}
void inner_function() {
collectFuncLatency(global_lat);
std::this_thread::sleep_for
( std::chrono::milliseconds( (std::rand() % 10) + 1) );
}
void test_function_1ms() {
collectFuncLatency(global_lat);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
inner_function();
}
void test_function_2ms() {
collectFuncLatency(global_lat);
std::this_thread::sleep_for(std::chrono::milliseconds(2));
inner_function();
}
void test_function_3ms() {
collectFuncLatency(global_lat);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
inner_function();
}
void test_function_4ms() {
collectFuncLatency(global_lat);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
inner_function();
}
int latency_macro_test() {
global_lat = new LatencyCollector();
size_t i, j;
std::vector<size_t> n_calls = {23, 19, 13};
std::vector<std::function<void()> >
funcs = {test_function_1ms,
test_function_2ms,
test_function_3ms};
for (i=0; i<n_calls.size(); ++i) {
collectBlockLatency(global_lat, "outer for-loop");
for (j=0; j<n_calls[i]; ++j) {
funcs[i]();
}
}
for (i=0; i<n_calls.size(); ++i) {
funcs[i]();
}
for (i=0; i<n_calls[2]+1; ++i) {
test_function_4ms();
}
LatencyDumpDefaultImpl default_dump;
TestSuite::Msg msg_stream;
LatencyCollectorDumpOptions opt;
opt.view_type = LatencyCollectorDumpOptions::TREE;
opt.sort_by = LatencyCollectorDumpOptions::TOTAL_TIME;
msg_stream << global_lat->dump(&default_dump, opt) << std::endl;
opt.sort_by = LatencyCollectorDumpOptions::AVG_LATENCY;
msg_stream << global_lat->dump(&default_dump, opt) << std::endl;
opt.sort_by = LatencyCollectorDumpOptions::NUM_CALLS;
msg_stream << global_lat->dump(&default_dump, opt) << std::endl;
opt.view_type = LatencyCollectorDumpOptions::FLAT;
opt.sort_by = LatencyCollectorDumpOptions::TOTAL_TIME;
msg_stream << global_lat->dump(&default_dump, opt) << std::endl;
opt.sort_by = LatencyCollectorDumpOptions::AVG_LATENCY;
msg_stream << global_lat->dump(&default_dump, opt) << std::endl;
opt.sort_by = LatencyCollectorDumpOptions::NUM_CALLS;
msg_stream << global_lat->dump(&default_dump, opt) << std::endl;
for ( const std::string& item_name: { std::string("test_function_3ms"),
std::string("inner_function") } ) {
LatencyItem chk = global_lat->getAggrItem(item_name);
msg_stream << item_name << ": "
<< "total " << chk.getTotalTime() << " us, "
<< chk.getNumCalls() << " calls" << std::endl;
std::map<double, uint64_t> hist = chk.dumpHistogram();
for (auto& entry: hist) {
msg_stream << "less than " << entry.first << ": "
<< entry.second << std::endl;
}
}
msg_stream << global_lat->dump(nullptr) << std::endl;
delete global_lat;
global_lat = nullptr;
return 0;
}
int main(int argc, char** argv) {
TestSuite test(argc, argv);;
test.options.printTestMessage = true;
test.doTest("multi thread test", MT_basic_insert_test);
test.doTest("function latency macro test", latency_macro_test);
return 0;
}