1 // Copyright 2019 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/strings/internal/cordz_functions.h"
16
17 #include <thread> // NOLINT we need real clean new threads
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "absl/base/config.h"
22
23 namespace absl {
24 ABSL_NAMESPACE_BEGIN
25 namespace cord_internal {
26 namespace {
27
28 using ::testing::Eq;
29 using ::testing::Ge;
30 using ::testing::Le;
31
TEST(CordzFunctionsTest,SampleRate)32 TEST(CordzFunctionsTest, SampleRate) {
33 int32_t orig_sample_rate = get_cordz_mean_interval();
34 int32_t expected_sample_rate = 123;
35 set_cordz_mean_interval(expected_sample_rate);
36 EXPECT_THAT(get_cordz_mean_interval(), Eq(expected_sample_rate));
37 set_cordz_mean_interval(orig_sample_rate);
38 }
39
40 // Cordz is disabled when we don't have thread_local. All calls to
41 // should_profile will return false when cordz is diabled, so we might want to
42 // avoid those tests.
43 #ifdef ABSL_INTERNAL_CORDZ_ENABLED
44
TEST(CordzFunctionsTest,ShouldProfileDisable)45 TEST(CordzFunctionsTest, ShouldProfileDisable) {
46 int32_t orig_sample_rate = get_cordz_mean_interval();
47
48 set_cordz_mean_interval(0);
49 cordz_set_next_sample_for_testing(0);
50 EXPECT_FALSE(cordz_should_profile());
51 // 1 << 16 is from kIntervalIfDisabled in cordz_functions.cc.
52 EXPECT_THAT(cordz_next_sample, Eq(1 << 16));
53
54 set_cordz_mean_interval(orig_sample_rate);
55 }
56
TEST(CordzFunctionsTest,ShouldProfileAlways)57 TEST(CordzFunctionsTest, ShouldProfileAlways) {
58 int32_t orig_sample_rate = get_cordz_mean_interval();
59
60 set_cordz_mean_interval(1);
61 cordz_set_next_sample_for_testing(1);
62 EXPECT_TRUE(cordz_should_profile());
63 EXPECT_THAT(cordz_next_sample, Le(1));
64
65 set_cordz_mean_interval(orig_sample_rate);
66 }
67
TEST(CordzFunctionsTest,DoesNotAlwaysSampleFirstCord)68 TEST(CordzFunctionsTest, DoesNotAlwaysSampleFirstCord) {
69 // Set large enough interval such that the chance of 'tons' of threads
70 // randomly sampling the first call is infinitely small.
71 set_cordz_mean_interval(10000);
72 int tries = 0;
73 bool sampled = false;
74 do {
75 ++tries;
76 ASSERT_THAT(tries, Le(1000));
77 std::thread thread([&sampled] {
78 sampled = cordz_should_profile();
79 });
80 thread.join();
81 } while (sampled);
82 }
83
TEST(CordzFunctionsTest,ShouldProfileRate)84 TEST(CordzFunctionsTest, ShouldProfileRate) {
85 static constexpr int kDesiredMeanInterval = 1000;
86 static constexpr int kSamples = 10000;
87 int32_t orig_sample_rate = get_cordz_mean_interval();
88
89 set_cordz_mean_interval(kDesiredMeanInterval);
90
91 int64_t sum_of_intervals = 0;
92 for (int i = 0; i < kSamples; i++) {
93 // Setting next_sample to 0 will force cordz_should_profile to generate a
94 // new value for next_sample each iteration.
95 cordz_set_next_sample_for_testing(0);
96 cordz_should_profile();
97 sum_of_intervals += cordz_next_sample;
98 }
99
100 // The sum of independent exponential variables is an Erlang distribution,
101 // which is a gamma distribution where the shape parameter is equal to the
102 // number of summands. The distribution used for cordz_should_profile is
103 // actually floor(Exponential(1/mean)) which introduces bias. However, we can
104 // apply the squint-really-hard correction factor. That is, when mean is
105 // large, then if we squint really hard the shape of the distribution between
106 // N and N+1 looks like a uniform distribution. On average, each value for
107 // next_sample will be about 0.5 lower than we would expect from an
108 // exponential distribution. This squint-really-hard correction approach won't
109 // work when mean is smaller than about 10 but works fine when mean is 1000.
110 //
111 // We can use R to calculate a confidence interval. This
112 // shows how to generate a confidence interval with a false positive rate of
113 // one in a billion.
114 //
115 // $ R -q
116 // > mean = 1000
117 // > kSamples = 10000
118 // > errorRate = 1e-9
119 // > correction = -kSamples / 2
120 // > low = qgamma(errorRate/2, kSamples, 1/mean) + correction
121 // > high = qgamma(1 - errorRate/2, kSamples, 1/mean) + correction
122 // > low
123 // [1] 9396115
124 // > high
125 // [1] 10618100
126 EXPECT_THAT(sum_of_intervals, Ge(9396115));
127 EXPECT_THAT(sum_of_intervals, Le(10618100));
128
129 set_cordz_mean_interval(orig_sample_rate);
130 }
131
132 #else // ABSL_INTERNAL_CORDZ_ENABLED
133
TEST(CordzFunctionsTest,ShouldProfileDisabled)134 TEST(CordzFunctionsTest, ShouldProfileDisabled) {
135 int32_t orig_sample_rate = get_cordz_mean_interval();
136
137 set_cordz_mean_interval(1);
138 cordz_set_next_sample_for_testing(0);
139 EXPECT_FALSE(cordz_should_profile());
140
141 set_cordz_mean_interval(orig_sample_rate);
142 }
143
144 #endif // ABSL_INTERNAL_CORDZ_ENABLED
145
146 } // namespace
147 } // namespace cord_internal
148 ABSL_NAMESPACE_END
149 } // namespace absl
150