Debian Codegraph @

test/core/debug/stats_test.cc

raw
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 <algorithm>
22 #include <memory>
23
24 #include "gtest/gtest.h"
25
26 #include <grpc/grpc.h>
27
28 #include "src/core/lib/debug/stats_data.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "test/core/util/test_config.h"
31
32 namespace grpc_core {
33 namespace testing {
34
35 class Snapshot {
36 public:
37 std::unique_ptr<GlobalStats> delta() {
38 auto now = global_stats().Collect();
39 return now->Diff(*begin_);
40 }
41
42 private:
43 std::unique_ptr<GlobalStats> begin_ = global_stats().Collect();
44 };
45
46 TEST(StatsTest, IncSpecificCounter) {
47 std::unique_ptr<Snapshot> snapshot(new Snapshot);
48
49 ExecCtx exec_ctx;
50 global_stats().IncrementClientCallsCreated();
51
52 EXPECT_EQ(snapshot->delta()->client_calls_created, 1);
53 }
54
55 static int FindExpectedBucket(const HistogramView& h, int value) {
56 if (value < 0) {
57 return 0;
58 }
59 if (value >= h.bucket_boundaries[h.num_buckets]) {
60 return h.num_buckets - 1;
61 }
62 return std::upper_bound(h.bucket_boundaries,
63 h.bucket_boundaries + h.num_buckets, value) -
64 h.bucket_boundaries - 1;
65 }
66
67 class HistogramTest : public ::testing::TestWithParam<int> {};
68
69 TEST_P(HistogramTest, CheckBucket) {
70 const GlobalStats::Histogram kHistogram =
71 static_cast<GlobalStats::Histogram>(GetParam());
72 auto some_stats = std::make_unique<GlobalStats>();
73 auto view = some_stats->histogram(kHistogram);
74 const int max_bucket_boundary = view.bucket_boundaries[view.num_buckets];
75 for (int i = -1000; i < max_bucket_boundary + 1000; i++) {
76 ASSERT_EQ(FindExpectedBucket(view, i), view.bucket_for(i))
77 << "i=" << i << " expect_bucket="
78 << view.bucket_boundaries[FindExpectedBucket(view, i)]
79 << " actual_bucket=" << view.bucket_boundaries[view.bucket_for(i)];
80 }
81 }
82
83 INSTANTIATE_TEST_SUITE_P(
84 HistogramTestCases, HistogramTest,
85 ::testing::Range<int>(0, static_cast<int>(GlobalStats::Histogram::COUNT)));
86
87 } // namespace testing
88 } // namespace grpc_core
89
90 int main(int argc, char** argv) {
91 grpc::testing::TestEnvironment env(&argc, argv);
92 ::testing::InitGoogleTest(&argc, argv);
93 grpc_init();
94 int ret = RUN_ALL_TESTS();
95 grpc_shutdown();
96 return ret;
97 }