forked from apache/brpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbvar_recorder_unittest.cpp
195 lines (171 loc) · 6.22 KB
/
bvar_recorder_unittest.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) 2014 Baidu, Inc.
// Author Zhangyi Chen ([email protected])
// Date 2014/10/13 19:47:59
#include <pthread.h> // pthread_*
#include <cstddef>
#include <memory>
#include <iostream>
#include "butil/time.h"
#include "butil/macros.h"
#include "bvar/recorder.h"
#include "bvar/latency_recorder.h"
#include <gtest/gtest.h>
namespace {
TEST(RecorderTest, test_complement) {
LOG(INFO) << "sizeof(LatencyRecorder)=" << sizeof(bvar::LatencyRecorder)
<< " " << sizeof(bvar::detail::Percentile)
<< " " << sizeof(bvar::Maxer<int64_t>)
<< " " << sizeof(bvar::IntRecorder)
<< " " << sizeof(bvar::Window<bvar::IntRecorder>)
<< " " << sizeof(bvar::Window<bvar::detail::Percentile>);
for (int a = -10000000; a < 10000000; ++a) {
const uint64_t complement = bvar::IntRecorder::_get_complement(a);
const int64_t b = bvar::IntRecorder::_extend_sign_bit(complement);
ASSERT_EQ(a, b);
}
}
TEST(RecorderTest, test_compress) {
const uint64_t num = 125345;
const uint64_t sum = 26032906;
const uint64_t compressed = bvar::IntRecorder::_compress(num, sum);
ASSERT_EQ(num, bvar::IntRecorder::_get_num(compressed));
ASSERT_EQ(sum, bvar::IntRecorder::_get_sum(compressed));
}
TEST(RecorderTest, test_compress_negtive_number) {
for (int a = -10000000; a < 10000000; ++a) {
const uint64_t sum = bvar::IntRecorder::_get_complement(a);
const uint64_t num = 123456;
const uint64_t compressed = bvar::IntRecorder::_compress(num, sum);
ASSERT_EQ(num, bvar::IntRecorder::_get_num(compressed));
ASSERT_EQ(a, bvar::IntRecorder::_extend_sign_bit(bvar::IntRecorder::_get_sum(compressed)));
}
}
TEST(RecorderTest, sanity) {
{
bvar::IntRecorder recorder;
ASSERT_TRUE(recorder.valid());
ASSERT_EQ(0, recorder.expose("var1"));
for (size_t i = 0; i < 100; ++i) {
recorder << 2;
}
ASSERT_EQ(2l, (int64_t)recorder.average());
ASSERT_EQ("2", bvar::Variable::describe_exposed("var1"));
std::vector<std::string> vars;
bvar::Variable::list_exposed(&vars);
ASSERT_EQ(1UL, vars.size());
ASSERT_EQ("var1", vars[0]);
ASSERT_EQ(1UL, bvar::Variable::count_exposed());
}
ASSERT_EQ(0UL, bvar::Variable::count_exposed());
}
TEST(RecorderTest, window) {
bvar::IntRecorder c1;
ASSERT_TRUE(c1.valid());
bvar::Window<bvar::IntRecorder> w1(&c1, 1);
bvar::Window<bvar::IntRecorder> w2(&c1, 2);
bvar::Window<bvar::IntRecorder> w3(&c1, 3);
const int N = 10000;
int64_t last_time = butil::gettimeofday_us();
for (int i = 1; i <= N; ++i) {
c1 << i;
int64_t now = butil::gettimeofday_us();
if (now - last_time >= 1000000L) {
last_time = now;
LOG(INFO) << "c1=" << c1 << " w1=" << w1 << " w2=" << w2 << " w3=" << w3;
} else {
usleep(950);
}
}
}
TEST(RecorderTest, negative) {
bvar::IntRecorder recorder;
ASSERT_TRUE(recorder.valid());
for (size_t i = 0; i < 3; ++i) {
recorder << -2;
}
ASSERT_EQ(-2, recorder.average());
}
TEST(RecorderTest, positive_overflow) {
bvar::IntRecorder recorder1;
ASSERT_TRUE(recorder1.valid());
for (int i = 0; i < 5; ++i) {
recorder1 << std::numeric_limits<int64_t>::max();
}
ASSERT_EQ(std::numeric_limits<int>::max(), recorder1.average());
bvar::IntRecorder recorder2;
ASSERT_TRUE(recorder2.valid());
recorder2.set_debug_name("recorder2");
for (int i = 0; i < 5; ++i) {
recorder2 << std::numeric_limits<int64_t>::max();
}
ASSERT_EQ(std::numeric_limits<int>::max(), recorder2.average());
bvar::IntRecorder recorder3;
ASSERT_TRUE(recorder3.valid());
recorder3.expose("recorder3");
for (int i = 0; i < 5; ++i) {
recorder3 << std::numeric_limits<int64_t>::max();
}
ASSERT_EQ(std::numeric_limits<int>::max(), recorder3.average());
bvar::LatencyRecorder latency1;
latency1.expose("latency1");
latency1 << std::numeric_limits<int64_t>::max();
bvar::LatencyRecorder latency2;
latency2 << std::numeric_limits<int64_t>::max();
}
TEST(RecorderTest, negtive_overflow) {
bvar::IntRecorder recorder1;
ASSERT_TRUE(recorder1.valid());
for (int i = 0; i < 5; ++i) {
recorder1 << std::numeric_limits<int64_t>::min();
}
ASSERT_EQ(std::numeric_limits<int>::min(), recorder1.average());
bvar::IntRecorder recorder2;
ASSERT_TRUE(recorder2.valid());
recorder2.set_debug_name("recorder2");
for (int i = 0; i < 5; ++i) {
recorder2 << std::numeric_limits<int64_t>::min();
}
ASSERT_EQ(std::numeric_limits<int>::min(), recorder2.average());
bvar::IntRecorder recorder3;
ASSERT_TRUE(recorder3.valid());
recorder3.expose("recorder3");
for (int i = 0; i < 5; ++i) {
recorder3 << std::numeric_limits<int64_t>::min();
}
ASSERT_EQ(std::numeric_limits<int>::min(), recorder3.average());
bvar::LatencyRecorder latency1;
latency1.expose("latency1");
latency1 << std::numeric_limits<int64_t>::min();
bvar::LatencyRecorder latency2;
latency2 << std::numeric_limits<int64_t>::min();
}
const size_t OPS_PER_THREAD = 20000000;
static void *thread_counter(void *arg) {
bvar::IntRecorder *recorder = (bvar::IntRecorder *)arg;
butil::Timer timer;
timer.start();
for (int i = 0; i < (int)OPS_PER_THREAD; ++i) {
*recorder << i;
}
timer.stop();
return (void *)(timer.n_elapsed());
}
TEST(RecorderTest, perf) {
bvar::IntRecorder recorder;
ASSERT_TRUE(recorder.valid());
pthread_t threads[8];
for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
pthread_create(&threads[i], NULL, &thread_counter, (void *)&recorder);
}
long totol_time = 0;
for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
void *ret;
pthread_join(threads[i], &ret);
totol_time += (long)ret;
}
ASSERT_EQ(((int64_t)OPS_PER_THREAD - 1) / 2, recorder.average());
LOG(INFO) << "Recorder takes " << totol_time / (OPS_PER_THREAD * ARRAY_SIZE(threads))
<< "ns per sample with " << ARRAY_SIZE(threads)
<< " threads";
}
} // namespace