xref: /aosp_15_r20/external/grpc-grpc/test/core/debug/stats_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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:
delta()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 
TEST(StatsTest,IncSpecificCounter)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 
TEST(StatsTest,IncrementHttp2MetadataSize)55 TEST(StatsTest, IncrementHttp2MetadataSize) {
56   ExecCtx exec_ctx;
57   global_stats().IncrementHttp2MetadataSize(0);
58 }
59 
FindExpectedBucket(const HistogramView & h,int value)60 static int FindExpectedBucket(const HistogramView& h, int value) {
61   if (value < 0) {
62     return 0;
63   }
64   if (value >= h.bucket_boundaries[h.num_buckets]) {
65     return h.num_buckets - 1;
66   }
67   return std::upper_bound(h.bucket_boundaries,
68                           h.bucket_boundaries + h.num_buckets, value) -
69          h.bucket_boundaries - 1;
70 }
71 
72 class HistogramTest : public ::testing::TestWithParam<int> {};
73 
TEST_P(HistogramTest,CheckBucket)74 TEST_P(HistogramTest, CheckBucket) {
75   const GlobalStats::Histogram kHistogram =
76       static_cast<GlobalStats::Histogram>(GetParam());
77   auto some_stats = std::make_unique<GlobalStats>();
78   auto view = some_stats->histogram(kHistogram);
79   const int max_bucket_boundary = view.bucket_boundaries[view.num_buckets];
80   for (int i = -1000; i < max_bucket_boundary + 1000; i++) {
81     ASSERT_EQ(FindExpectedBucket(view, i), view.bucket_for(i))
82         << "i=" << i << " expect_bucket="
83         << view.bucket_boundaries[FindExpectedBucket(view, i)]
84         << " actual_bucket=" << view.bucket_boundaries[view.bucket_for(i)];
85   }
86 }
87 
88 INSTANTIATE_TEST_SUITE_P(
89     HistogramTestCases, HistogramTest,
90     ::testing::Range<int>(0, static_cast<int>(GlobalStats::Histogram::COUNT)));
91 
92 }  // namespace testing
93 }  // namespace grpc_core
94 
main(int argc,char ** argv)95 int main(int argc, char** argv) {
96   grpc::testing::TestEnvironment env(&argc, argv);
97   ::testing::InitGoogleTest(&argc, argv);
98   grpc_init();
99   int ret = RUN_ALL_TESTS();
100   grpc_shutdown();
101   return ret;
102 }
103