stats_test.cc
Go to the documentation of this file.
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 
20 
21 #include <mutex>
22 #include <thread>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpc/grpc.h>
27 #include <grpc/support/cpu.h>
28 #include <grpc/support/log.h>
29 
31 
32 namespace grpc {
33 namespace testing {
34 
35 class Snapshot {
36  public:
38 
44  return delta;
45  }
46 
47  private:
49 };
50 
51 TEST(StatsTest, IncCounters) {
52  for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
53  std::unique_ptr<Snapshot> snapshot(new Snapshot);
54 
57 
58  EXPECT_EQ(snapshot->delta().counters[i], 1);
59  }
60 }
61 
62 TEST(StatsTest, IncSpecificCounter) {
63  std::unique_ptr<Snapshot> snapshot(new Snapshot);
64 
67 
68  EXPECT_EQ(snapshot->delta().counters[GRPC_STATS_COUNTER_SYSCALL_POLL], 1);
69 }
70 
71 static int FindExpectedBucket(int i, int j) {
72  if (j < 0) {
73  return 0;
74  }
76  return grpc_stats_histo_buckets[i] - 1;
77  }
78  return std::upper_bound(grpc_stats_histo_bucket_boundaries[i],
81  j) -
83 }
84 
85 class HistogramTest : public ::testing::TestWithParam<int> {};
86 
87 TEST_P(HistogramTest, IncHistogram) {
88  const int kHistogram = GetParam();
89  std::vector<std::thread> threads;
90  int cur_bucket = 0;
91  auto run = [kHistogram](const std::vector<int>& test_values,
92  int expected_bucket) {
93  gpr_log(GPR_DEBUG, "expected_bucket:%d nvalues=%" PRIdPTR, expected_bucket,
94  test_values.size());
95  for (auto j : test_values) {
96  std::unique_ptr<Snapshot> snapshot(new Snapshot);
97 
99  grpc_stats_inc_histogram[kHistogram](j);
100 
101  auto delta = snapshot->delta();
102 
103  EXPECT_EQ(
104  delta
105  .histograms[grpc_stats_histo_start[kHistogram] + expected_bucket],
106  1)
107  << "\nhistogram:" << kHistogram
108  << "\nexpected_bucket:" << expected_bucket << "\nj:" << j;
109  }
110  };
111  std::vector<int> test_values;
112  // largest bucket boundary for current histogram type.
113  int max_bucket_boundary =
115  [grpc_stats_histo_buckets[kHistogram] -
116  1];
117  for (int j = -1000; j < max_bucket_boundary + 1000;) {
118  int expected_bucket = FindExpectedBucket(kHistogram, j);
119  if (cur_bucket != expected_bucket) {
120  threads.emplace_back(
121  [test_values, run, cur_bucket]() { run(test_values, cur_bucket); });
122  cur_bucket = expected_bucket;
123  test_values.clear();
124  }
125  test_values.push_back(j);
126  if (j < max_bucket_boundary &&
127  FindExpectedBucket(kHistogram, j + 1000) == expected_bucket &&
128  FindExpectedBucket(kHistogram, j - 1000) == expected_bucket) {
129  // if we are far from bucket boundary, skip values to speed-up the tests
130  j += 500;
131  } else {
132  j++;
133  }
134  }
135  run(test_values, cur_bucket);
136  for (auto& t : threads) {
137  t.join();
138  }
139 }
140 
141 INSTANTIATE_TEST_SUITE_P(HistogramTestCases, HistogramTest,
142  ::testing::Range<int>(0, GRPC_STATS_HISTOGRAM_COUNT));
143 
144 } // namespace testing
145 } // namespace grpc
146 
147 int main(int argc, char** argv) {
148 /* Only run this test if GRPC_COLLECT_STATS is defined or if it is a debug
149  * build.
150  */
151 #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
152  grpc::testing::TestEnvironment env(&argc, argv);
153  ::testing::InitGoogleTest(&argc, argv);
154  grpc_init();
155  int ret = RUN_ALL_TESTS();
156  grpc_shutdown();
157  return ret;
158 #else
159  // Avoid unused parameter warning for conditional parameters.
160  (void)argc;
161  (void)argv;
162 #endif
163 }
testing
Definition: aws_request_signer_test.cc:25
grpc::testing::Snapshot::begin_
grpc_stats_data begin_
Definition: stats_test.cc:48
now
static double now(void)
Definition: test/core/fling/client.cc:130
log.h
generate.env
env
Definition: generate.py:37
GRPC_STATS_INC_COUNTER
#define GRPC_STATS_INC_COUNTER(ctr)
Definition: src/core/lib/debug/stats.h:46
grpc_stats_histo_buckets
const int grpc_stats_histo_buckets[13]
Definition: stats_data.cc:663
grpc
Definition: grpcpp/alarm.h:33
GRPC_STATS_HISTOGRAM_COUNT
@ GRPC_STATS_HISTOGRAM_COUNT
Definition: stats_data.h:142
stats.h
threads
static uv_thread_t * threads
Definition: threadpool.c:38
main
int main(int argc, char **argv)
Definition: stats_test.cc:147
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
GRPC_STATS_COUNTER_SYSCALL_POLL
@ GRPC_STATS_COUNTER_SYSCALL_POLL
Definition: stats_data.h:33
grpc::testing::Snapshot::Snapshot
Snapshot()
Definition: stats_test.cc:37
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
cpu.h
GRPC_STATS_INC_SYSCALL_POLL
#define GRPC_STATS_INC_SYSCALL_POLL()
Definition: stats_data.h:188
grpc::testing::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(HistogramTestCases, HistogramTest, ::testing::Range< int >(0, GRPC_STATS_HISTOGRAM_COUNT))
grpc::testing::FindExpectedBucket
static int FindExpectedBucket(int i, int j)
Definition: stats_test.cc:71
grpc_stats_histo_start
const int grpc_stats_histo_start[13]
Definition: stats_data.cc:665
grpc_stats_collect
void grpc_stats_collect(grpc_stats_data *output)
Definition: stats.cc:48
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc::testing::Snapshot::delta
grpc_stats_data delta()
Definition: stats_test.cc:39
grpc_core::ExecCtx
Definition: exec_ctx.h:97
test_values
static void test_values(void)
Definition: test/core/gpr/time_test.cc:63
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
client.run
def run()
Definition: examples/python/async_streaming/client.py:109
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc_stats_data
Definition: src/core/lib/debug/stats.h:33
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
grpc::testing::Snapshot
Definition: stats_test.cc:35
grpc_stats_inc_histogram
void(*const grpc_stats_inc_histogram[13])(int x)
Definition: stats_data.cc:673
grpc_stats_diff
void grpc_stats_diff(const grpc_stats_data *b, const grpc_stats_data *a, grpc_stats_data *c)
Definition: stats.cc:62
grpc::testing::TEST
TEST(StatsTest, IncCounters)
Definition: stats_test.cc:51
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
GRPC_STATS_COUNTER_COUNT
@ GRPC_STATS_COUNTER_COUNT
Definition: stats_data.h:124
grpc_stats_counters
grpc_stats_counters
Definition: stats_data.h:26
grpc::testing::TEST_P
TEST_P(HistogramTest, IncHistogram)
Definition: stats_test.cc:87
grpc::testing::HistogramTest
Definition: stats_test.cc:85
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_stats_histo_bucket_boundaries
const int *const grpc_stats_histo_bucket_boundaries[13]
Definition: stats_data.cc:667


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:22