stats_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include "src/core/lib/debug/stats.h"
  19. #include <mutex>
  20. #include <thread>
  21. #include <gtest/gtest.h>
  22. #include <grpc/grpc.h>
  23. #include <grpc/support/cpu.h>
  24. #include <grpc/support/log.h>
  25. #include "test/core/util/test_config.h"
  26. namespace grpc {
  27. namespace testing {
  28. class Snapshot {
  29. public:
  30. Snapshot() { grpc_stats_collect(&begin_); }
  31. grpc_stats_data delta() {
  32. grpc_stats_data now;
  33. grpc_stats_collect(&now);
  34. grpc_stats_data delta;
  35. grpc_stats_diff(&now, &begin_, &delta);
  36. return delta;
  37. }
  38. private:
  39. grpc_stats_data begin_;
  40. };
  41. TEST(StatsTest, IncCounters) {
  42. for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
  43. std::unique_ptr<Snapshot> snapshot(new Snapshot);
  44. grpc_core::ExecCtx exec_ctx;
  45. GRPC_STATS_INC_COUNTER((grpc_stats_counters)i);
  46. EXPECT_EQ(snapshot->delta().counters[i], 1);
  47. }
  48. }
  49. TEST(StatsTest, IncSpecificCounter) {
  50. std::unique_ptr<Snapshot> snapshot(new Snapshot);
  51. grpc_core::ExecCtx exec_ctx;
  52. GRPC_STATS_INC_SYSCALL_POLL();
  53. EXPECT_EQ(snapshot->delta().counters[GRPC_STATS_COUNTER_SYSCALL_POLL], 1);
  54. }
  55. static int FindExpectedBucket(int i, int j) {
  56. if (j < 0) {
  57. return 0;
  58. }
  59. if (j >= grpc_stats_histo_bucket_boundaries[i][grpc_stats_histo_buckets[i]]) {
  60. return grpc_stats_histo_buckets[i] - 1;
  61. }
  62. return std::upper_bound(grpc_stats_histo_bucket_boundaries[i],
  63. grpc_stats_histo_bucket_boundaries[i] +
  64. grpc_stats_histo_buckets[i],
  65. j) -
  66. grpc_stats_histo_bucket_boundaries[i] - 1;
  67. }
  68. class HistogramTest : public ::testing::TestWithParam<int> {};
  69. TEST_P(HistogramTest, IncHistogram) {
  70. const int kHistogram = GetParam();
  71. std::vector<std::thread> threads;
  72. int cur_bucket = 0;
  73. auto run = [kHistogram](const std::vector<int>& test_values,
  74. int expected_bucket) {
  75. gpr_log(GPR_DEBUG, "expected_bucket:%d nvalues=%" PRIdPTR, expected_bucket,
  76. test_values.size());
  77. for (auto j : test_values) {
  78. std::unique_ptr<Snapshot> snapshot(new Snapshot);
  79. grpc_core::ExecCtx exec_ctx;
  80. grpc_stats_inc_histogram[kHistogram](j);
  81. auto delta = snapshot->delta();
  82. EXPECT_EQ(
  83. delta
  84. .histograms[grpc_stats_histo_start[kHistogram] + expected_bucket],
  85. 1)
  86. << "\nhistogram:" << kHistogram
  87. << "\nexpected_bucket:" << expected_bucket << "\nj:" << j;
  88. }
  89. };
  90. std::vector<int> test_values;
  91. // largest bucket boundary for current histogram type.
  92. int max_bucket_boundary =
  93. grpc_stats_histo_bucket_boundaries[kHistogram]
  94. [grpc_stats_histo_buckets[kHistogram] -
  95. 1];
  96. for (int j = -1000; j < max_bucket_boundary + 1000;) {
  97. int expected_bucket = FindExpectedBucket(kHistogram, j);
  98. if (cur_bucket != expected_bucket) {
  99. threads.emplace_back(
  100. [test_values, run, cur_bucket]() { run(test_values, cur_bucket); });
  101. cur_bucket = expected_bucket;
  102. test_values.clear();
  103. }
  104. test_values.push_back(j);
  105. if (j < max_bucket_boundary &&
  106. FindExpectedBucket(kHistogram, j + 1000) == expected_bucket &&
  107. FindExpectedBucket(kHistogram, j - 1000) == expected_bucket) {
  108. // if we are far from bucket boundary, skip values to speed-up the tests
  109. j += 500;
  110. } else {
  111. j++;
  112. }
  113. }
  114. run(test_values, cur_bucket);
  115. for (auto& t : threads) {
  116. t.join();
  117. }
  118. }
  119. INSTANTIATE_TEST_SUITE_P(HistogramTestCases, HistogramTest,
  120. ::testing::Range<int>(0, GRPC_STATS_HISTOGRAM_COUNT));
  121. } // namespace testing
  122. } // namespace grpc
  123. int main(int argc, char** argv) {
  124. /* Only run this test if GRPC_COLLECT_STATS is defined or if it is a debug
  125. * build.
  126. */
  127. #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
  128. grpc::testing::TestEnvironment env(argc, argv);
  129. ::testing::InitGoogleTest(&argc, argv);
  130. grpc_init();
  131. int ret = RUN_ALL_TESTS();
  132. grpc_shutdown();
  133. return ret;
  134. #else
  135. // Avoid unused parameter warning for conditional parameters.
  136. (void)argc;
  137. (void)argv;
  138. #endif
  139. }