1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "src/core/lib/debug/stats.h"
20
21 #include <mutex>
22 #include <thread>
23
24 #include <grpc/grpc.h>
25 #include <grpc/support/cpu.h>
26 #include <grpc/support/log.h>
27 #include <gtest/gtest.h>
28
29 #include "test/core/util/test_config.h"
30
31 namespace grpc {
32 namespace testing {
33
34 class Snapshot {
35 public:
Snapshot()36 Snapshot() { grpc_stats_collect(&begin_); }
37
delta()38 grpc_stats_data delta() {
39 grpc_stats_data now;
40 grpc_stats_collect(&now);
41 grpc_stats_data delta;
42 grpc_stats_diff(&now, &begin_, &delta);
43 return delta;
44 }
45
46 private:
47 grpc_stats_data begin_;
48 };
49
TEST(StatsTest,IncCounters)50 TEST(StatsTest, IncCounters) {
51 for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
52 std::unique_ptr<Snapshot> snapshot(new Snapshot);
53
54 grpc_core::ExecCtx exec_ctx;
55 GRPC_STATS_INC_COUNTER((grpc_stats_counters)i);
56
57 EXPECT_EQ(snapshot->delta().counters[i], 1);
58 }
59 }
60
TEST(StatsTest,IncSpecificCounter)61 TEST(StatsTest, IncSpecificCounter) {
62 std::unique_ptr<Snapshot> snapshot(new Snapshot);
63
64 grpc_core::ExecCtx exec_ctx;
65 GRPC_STATS_INC_SYSCALL_POLL();
66
67 EXPECT_EQ(snapshot->delta().counters[GRPC_STATS_COUNTER_SYSCALL_POLL], 1);
68 }
69
FindExpectedBucket(int i,int j)70 static int FindExpectedBucket(int i, int j) {
71 if (j < 0) {
72 return 0;
73 }
74 if (j >= grpc_stats_histo_bucket_boundaries[i][grpc_stats_histo_buckets[i]]) {
75 return grpc_stats_histo_buckets[i] - 1;
76 }
77 return std::upper_bound(grpc_stats_histo_bucket_boundaries[i],
78 grpc_stats_histo_bucket_boundaries[i] +
79 grpc_stats_histo_buckets[i],
80 j) -
81 grpc_stats_histo_bucket_boundaries[i] - 1;
82 }
83
84 class HistogramTest : public ::testing::TestWithParam<int> {};
85
TEST_P(HistogramTest,IncHistogram)86 TEST_P(HistogramTest, IncHistogram) {
87 const int kHistogram = GetParam();
88 std::vector<std::thread> threads;
89 int cur_bucket = 0;
90 auto run = [kHistogram](const std::vector<int>& test_values,
91 int expected_bucket) {
92 gpr_log(GPR_DEBUG, "expected_bucket:%d nvalues=%" PRIdPTR, expected_bucket,
93 test_values.size());
94 for (auto j : test_values) {
95 std::unique_ptr<Snapshot> snapshot(new Snapshot);
96
97 grpc_core::ExecCtx exec_ctx;
98 grpc_stats_inc_histogram[kHistogram](j);
99
100 auto delta = snapshot->delta();
101
102 EXPECT_EQ(
103 delta
104 .histograms[grpc_stats_histo_start[kHistogram] + expected_bucket],
105 1)
106 << "\nhistogram:" << kHistogram
107 << "\nexpected_bucket:" << expected_bucket << "\nj:" << j;
108 }
109 };
110 std::vector<int> test_values;
111 // largest bucket boundary for current histogram type.
112 int max_bucket_boundary =
113 grpc_stats_histo_bucket_boundaries[kHistogram]
114 [grpc_stats_histo_buckets[kHistogram] -
115 1];
116 for (int j = -1000; j < max_bucket_boundary + 1000;) {
117 int expected_bucket = FindExpectedBucket(kHistogram, j);
118 if (cur_bucket != expected_bucket) {
119 threads.emplace_back(
120 [test_values, run, cur_bucket]() { run(test_values, cur_bucket); });
121 cur_bucket = expected_bucket;
122 test_values.clear();
123 }
124 test_values.push_back(j);
125 if (j < max_bucket_boundary &&
126 FindExpectedBucket(kHistogram, j + 1000) == expected_bucket &&
127 FindExpectedBucket(kHistogram, j - 1000) == expected_bucket) {
128 // if we are far from bucket boundary, skip values to speed-up the tests
129 j += 500;
130 } else {
131 j++;
132 }
133 }
134 run(test_values, cur_bucket);
135 for (auto& t : threads) {
136 t.join();
137 }
138 }
139
140 INSTANTIATE_TEST_SUITE_P(HistogramTestCases, HistogramTest,
141 ::testing::Range<int>(0, GRPC_STATS_HISTOGRAM_COUNT));
142
143 } // namespace testing
144 } // namespace grpc
145
main(int argc,char ** argv)146 int main(int argc, char** argv) {
147 /* Only run this test if GRPC_COLLECT_STATS is defined or if it is a debug
148 * build.
149 */
150 #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
151 grpc::testing::TestEnvironment env(argc, argv);
152 ::testing::InitGoogleTest(&argc, argv);
153 grpc_init();
154 int ret = RUN_ALL_TESTS();
155 grpc_shutdown();
156 return ret;
157 #else
158 // Avoid unused parameter warning for conditional parameters.
159 (void)argc;
160 (void)argv;
161 #endif
162 }
163