forked from apache/brpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbvar_sampler_unittest.cpp
124 lines (115 loc) · 3.45 KB
/
bvar_sampler_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
// Copyright (c) 2014 Baidu, Inc.
#include <limits> //std::numeric_limits
#include "bvar/detail/sampler.h"
#include "butil/time.h"
#include "butil/logging.h"
#include <gtest/gtest.h>
namespace {
TEST(SamplerTest, linked_list) {
butil::LinkNode<bvar::detail::Sampler> n1, n2;
n1.InsertBeforeAsList(&n2);
ASSERT_EQ(n1.next(), &n2);
ASSERT_EQ(n1.previous(), &n2);
ASSERT_EQ(n2.next(), &n1);
ASSERT_EQ(n2.previous(), &n1);
butil::LinkNode<bvar::detail::Sampler> n3, n4;
n3.InsertBeforeAsList(&n4);
ASSERT_EQ(n3.next(), &n4);
ASSERT_EQ(n3.previous(), &n4);
ASSERT_EQ(n4.next(), &n3);
ASSERT_EQ(n4.previous(), &n3);
n1.InsertBeforeAsList(&n3);
ASSERT_EQ(n1.next(), &n2);
ASSERT_EQ(n2.next(), &n3);
ASSERT_EQ(n3.next(), &n4);
ASSERT_EQ(n4.next(), &n1);
ASSERT_EQ(&n1, n2.previous());
ASSERT_EQ(&n2, n3.previous());
ASSERT_EQ(&n3, n4.previous());
ASSERT_EQ(&n4, n1.previous());
}
class DebugSampler : public bvar::detail::Sampler {
public:
DebugSampler() : _ncalled(0) {}
~DebugSampler() {
++_s_ndestroy;
}
void take_sample() {
++_ncalled;
}
int called_count() const { return _ncalled; }
private:
int _ncalled;
static int _s_ndestroy;
};
int DebugSampler::_s_ndestroy = 0;
TEST(SamplerTest, single_threaded) {
#if !BRPC_WITH_GLOG
logging::StringSink log_str;
logging::LogSink* old_sink = logging::SetLogSink(&log_str);
#endif
const int N = 100;
DebugSampler* s[N];
for (int i = 0; i < N; ++i) {
s[i] = new DebugSampler;
s[i]->schedule();
}
usleep(1010000);
for (int i = 0; i < N; ++i) {
// LE: called once every second, may be called more than once
ASSERT_LE(1, s[i]->called_count()) << "i=" << i;
}
EXPECT_EQ(0, DebugSampler::_s_ndestroy);
for (int i = 0; i < N; ++i) {
s[i]->destroy();
}
usleep(1010000);
EXPECT_EQ(N, DebugSampler::_s_ndestroy);
#if !BRPC_WITH_GLOG
ASSERT_EQ(&log_str, logging::SetLogSink(old_sink));
if (log_str.find("Removed ") != std::string::npos) {
ASSERT_NE(std::string::npos, log_str.find("Removed 0, sampled 100"));
ASSERT_NE(std::string::npos, log_str.find("Removed 100, sampled 0"));
}
#endif
}
static void* check(void*) {
const int N = 100;
DebugSampler* s[N];
for (int i = 0; i < N; ++i) {
s[i] = new DebugSampler;
s[i]->schedule();
}
usleep(1010000);
for (int i = 0; i < N; ++i) {
EXPECT_LE(1, s[i]->called_count()) << "i=" << i;
}
for (int i = 0; i < N; ++i) {
s[i]->destroy();
}
return NULL;
}
TEST(SamplerTest, multi_threaded) {
#if !BRPC_WITH_GLOG
logging::StringSink log_str;
logging::LogSink* old_sink = logging::SetLogSink(&log_str);
#endif
pthread_t th[10];
DebugSampler::_s_ndestroy = 0;
for (size_t i = 0; i < arraysize(th); ++i) {
ASSERT_EQ(0, pthread_create(&th[i], NULL, check, NULL));
}
for (size_t i = 0; i < arraysize(th); ++i) {
ASSERT_EQ(0, pthread_join(th[i], NULL));
}
sleep(1);
EXPECT_EQ(100 * arraysize(th), (size_t)DebugSampler::_s_ndestroy);
#if !BRPC_WITH_GLOG
ASSERT_EQ(&log_str, logging::SetLogSink(old_sink));
if (log_str.find("Removed ") != std::string::npos) {
ASSERT_NE(std::string::npos, log_str.find("Removed 0, sampled 1000"));
ASSERT_NE(std::string::npos, log_str.find("Removed 1000, sampled 0"));
}
#endif
}
} // namespace