• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace grpc {
30 namespace testing {
31 
32 class Snapshot {
33  public:
Snapshot()34   Snapshot() { grpc_stats_collect(&begin_); }
35 
delta()36   grpc_stats_data delta() {
37     grpc_stats_data now;
38     grpc_stats_collect(&now);
39     grpc_stats_data delta;
40     grpc_stats_diff(&now, &begin_, &delta);
41     return delta;
42   }
43 
44  private:
45   grpc_stats_data begin_;
46 };
47 
TEST(StatsTest,IncCounters)48 TEST(StatsTest, IncCounters) {
49   for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
50     std::unique_ptr<Snapshot> snapshot(new Snapshot);
51 
52     grpc_core::ExecCtx exec_ctx;
53     GRPC_STATS_INC_COUNTER((grpc_stats_counters)i);
54 
55     EXPECT_EQ(snapshot->delta().counters[i], 1);
56   }
57 }
58 
TEST(StatsTest,IncSpecificCounter)59 TEST(StatsTest, IncSpecificCounter) {
60   std::unique_ptr<Snapshot> snapshot(new Snapshot);
61 
62   grpc_core::ExecCtx exec_ctx;
63   GRPC_STATS_INC_SYSCALL_POLL();
64 
65   EXPECT_EQ(snapshot->delta().counters[GRPC_STATS_COUNTER_SYSCALL_POLL], 1);
66 }
67 
FindExpectedBucket(int i,int j)68 static int FindExpectedBucket(int i, int j) {
69   if (j < 0) {
70     return 0;
71   }
72   if (j >= grpc_stats_histo_bucket_boundaries[i][grpc_stats_histo_buckets[i]]) {
73     return grpc_stats_histo_buckets[i] - 1;
74   }
75   return std::upper_bound(grpc_stats_histo_bucket_boundaries[i],
76                           grpc_stats_histo_bucket_boundaries[i] +
77                               grpc_stats_histo_buckets[i],
78                           j) -
79          grpc_stats_histo_bucket_boundaries[i] - 1;
80 }
81 
82 class HistogramTest : public ::testing::TestWithParam<int> {};
83 
TEST_P(HistogramTest,IncHistogram)84 TEST_P(HistogramTest, IncHistogram) {
85   const int kHistogram = GetParam();
86   std::vector<std::thread> threads;
87   int cur_bucket = 0;
88   auto run = [kHistogram](const std::vector<int>& test_values,
89                           int expected_bucket) {
90     gpr_log(GPR_DEBUG, "expected_bucket:%d nvalues=%" PRIdPTR, expected_bucket,
91             test_values.size());
92     for (auto j : test_values) {
93       std::unique_ptr<Snapshot> snapshot(new Snapshot);
94 
95       grpc_core::ExecCtx exec_ctx;
96       grpc_stats_inc_histogram[kHistogram](j);
97 
98       auto delta = snapshot->delta();
99 
100       EXPECT_EQ(
101           delta
102               .histograms[grpc_stats_histo_start[kHistogram] + expected_bucket],
103           1)
104           << "\nhistogram:" << kHistogram
105           << "\nexpected_bucket:" << expected_bucket << "\nj:" << j;
106     }
107   };
108   std::vector<int> test_values;
109   // largest bucket boundary for current histogram type.
110   int max_bucket_boundary =
111       grpc_stats_histo_bucket_boundaries[kHistogram]
112                                         [grpc_stats_histo_buckets[kHistogram] -
113                                          1];
114   for (int j = -1000; j < max_bucket_boundary + 1000;) {
115     int expected_bucket = FindExpectedBucket(kHistogram, j);
116     if (cur_bucket != expected_bucket) {
117       threads.emplace_back(
118           [test_values, run, cur_bucket]() { run(test_values, cur_bucket); });
119       cur_bucket = expected_bucket;
120       test_values.clear();
121     }
122     test_values.push_back(j);
123     if (j < max_bucket_boundary &&
124         FindExpectedBucket(kHistogram, j + 1000) == expected_bucket &&
125         FindExpectedBucket(kHistogram, j - 1000) == expected_bucket) {
126       // if we are far from bucket boundary, skip values to speed-up the tests
127       j += 500;
128     } else {
129       j++;
130     }
131   }
132   run(test_values, cur_bucket);
133   for (auto& t : threads) {
134     t.join();
135   }
136 }
137 
138 INSTANTIATE_TEST_CASE_P(HistogramTestCases, HistogramTest,
139                         ::testing::Range<int>(0, GRPC_STATS_HISTOGRAM_COUNT));
140 
141 }  // namespace testing
142 }  // namespace grpc
143 
main(int argc,char ** argv)144 int main(int argc, char** argv) {
145 /* Only run this test if GRPC_COLLECT_STATS is defined or if it is a debug
146  * build.
147  */
148 #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
149   ::testing::InitGoogleTest(&argc, argv);
150   grpc_init();
151   int ret = RUN_ALL_TESTS();
152   grpc_shutdown();
153   return ret;
154 #endif
155 }
156