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