3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "src/core/lib/debug/stats.h"
24 #include <grpc/grpc.h>
25 #include <grpc/support/cpu.h>
26 #include <grpc/support/log.h>
27 #include <gtest/gtest.h>
29 #include "test/core/util/test_config.h"
36 Snapshot() { grpc_stats_collect(&begin_); }
38 grpc_stats_data delta() {
40 grpc_stats_collect(&now);
41 grpc_stats_data delta;
42 grpc_stats_diff(&now, &begin_, &delta);
47 grpc_stats_data begin_;
50 TEST(StatsTest, IncCounters) {
51 for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
52 std::unique_ptr<Snapshot> snapshot(new Snapshot);
54 grpc_core::ExecCtx exec_ctx;
55 GRPC_STATS_INC_COUNTER((grpc_stats_counters)i);
57 EXPECT_EQ(snapshot->delta().counters[i], 1);
61 TEST(StatsTest, IncSpecificCounter) {
62 std::unique_ptr<Snapshot> snapshot(new Snapshot);
64 grpc_core::ExecCtx exec_ctx;
65 GRPC_STATS_INC_SYSCALL_POLL();
67 EXPECT_EQ(snapshot->delta().counters[GRPC_STATS_COUNTER_SYSCALL_POLL], 1);
70 static int FindExpectedBucket(int i, int j) {
74 if (j >= grpc_stats_histo_bucket_boundaries[i][grpc_stats_histo_buckets[i]]) {
75 return grpc_stats_histo_buckets[i] - 1;
77 return std::upper_bound(grpc_stats_histo_bucket_boundaries[i],
78 grpc_stats_histo_bucket_boundaries[i] +
79 grpc_stats_histo_buckets[i],
81 grpc_stats_histo_bucket_boundaries[i] - 1;
84 class HistogramTest : public ::testing::TestWithParam<int> {};
86 TEST_P(HistogramTest, IncHistogram) {
87 const int kHistogram = GetParam();
88 std::vector<std::thread> threads;
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,
94 for (auto j : test_values) {
95 std::unique_ptr<Snapshot> snapshot(new Snapshot);
97 grpc_core::ExecCtx exec_ctx;
98 grpc_stats_inc_histogram[kHistogram](j);
100 auto delta = snapshot->delta();
104 .histograms[grpc_stats_histo_start[kHistogram] + expected_bucket],
106 << "\nhistogram:" << kHistogram
107 << "\nexpected_bucket:" << expected_bucket << "\nj:" << j;
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] -
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;
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
134 run(test_values, cur_bucket);
135 for (auto& t : threads) {
140 INSTANTIATE_TEST_SUITE_P(HistogramTestCases, HistogramTest,
141 ::testing::Range<int>(0, GRPC_STATS_HISTOGRAM_COUNT));
143 } // namespace testing
146 int main(int argc, char** argv) {
147 /* Only run this test if GRPC_COLLECT_STATS is defined or if it is a debug
150 #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
151 grpc::testing::TestEnvironment env(argc, argv);
152 ::testing::InitGoogleTest(&argc, argv);
154 int ret = RUN_ALL_TESTS();
158 // Avoid unused parameter warning for conditional parameters.