stats_test.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. extern "C" {
  19. #include "src/core/lib/debug/stats.h"
  20. }
  21. #include <thread>
  22. #include <grpc/grpc.h>
  23. #include <grpc/support/cpu.h>
  24. #include <grpc/support/log.h>
  25. #include <gtest/gtest.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. Snapshot snapshot;
  44. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  45. GRPC_STATS_INC_COUNTER(&exec_ctx, (grpc_stats_counters)i);
  46. grpc_exec_ctx_finish(&exec_ctx);
  47. EXPECT_EQ(snapshot.delta().counters[i], 1);
  48. }
  49. }
  50. TEST(StatsTest, IncSpecificCounter) {
  51. Snapshot snapshot;
  52. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  53. GRPC_STATS_INC_SYSCALL_POLL(&exec_ctx);
  54. grpc_exec_ctx_finish(&exec_ctx);
  55. EXPECT_EQ(snapshot.delta().counters[GRPC_STATS_COUNTER_SYSCALL_POLL], 1);
  56. }
  57. static int FindExpectedBucket(int i, int j) {
  58. if (j < 0) {
  59. return 0;
  60. }
  61. if (j >= grpc_stats_histo_bucket_boundaries[i][grpc_stats_histo_buckets[i]]) {
  62. return grpc_stats_histo_buckets[i] - 1;
  63. }
  64. return std::upper_bound(grpc_stats_histo_bucket_boundaries[i],
  65. grpc_stats_histo_bucket_boundaries[i] +
  66. grpc_stats_histo_buckets[i],
  67. j) -
  68. grpc_stats_histo_bucket_boundaries[i] - 1;
  69. }
  70. class HistogramTest : public ::testing::TestWithParam<int> {};
  71. TEST_P(HistogramTest, IncHistogram) {
  72. const int kHistogram = GetParam();
  73. const int kThreads = std::max(1, (int)gpr_cpu_num_cores());
  74. std::vector<std::thread> threads;
  75. for (int thread = 0; thread < kThreads; thread++) {
  76. threads.emplace_back([kHistogram, kThreads, thread]() {
  77. std::vector<int> test_values;
  78. for (int j = -1000 + thread;
  79. j < grpc_stats_histo_bucket_boundaries
  80. [kHistogram][grpc_stats_histo_buckets[kHistogram] - 1] +
  81. 1000;
  82. j += kThreads) {
  83. test_values.push_back(j);
  84. }
  85. std::random_shuffle(test_values.begin(), test_values.end());
  86. for (auto j : test_values) {
  87. Snapshot snapshot;
  88. int expected_bucket = FindExpectedBucket(kHistogram, j);
  89. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  90. grpc_stats_inc_histogram[kHistogram](&exec_ctx, j);
  91. grpc_exec_ctx_finish(&exec_ctx);
  92. auto delta = snapshot.delta();
  93. EXPECT_GE(delta.histograms[grpc_stats_histo_start[kHistogram] +
  94. expected_bucket],
  95. 1)
  96. << "\nhistogram:" << kHistogram
  97. << "\nexpected_bucket:" << expected_bucket << "\nthread:" << thread
  98. << "\nj:" << j;
  99. }
  100. });
  101. }
  102. for (auto& t : threads) {
  103. t.join();
  104. }
  105. }
  106. INSTANTIATE_TEST_CASE_P(HistogramTestCases, HistogramTest,
  107. ::testing::Range<int>(0, GRPC_STATS_HISTOGRAM_COUNT));
  108. } // namespace testing
  109. } // namespace grpc
  110. int main(int argc, char** argv) {
  111. ::testing::InitGoogleTest(&argc, argv);
  112. grpc_init();
  113. int ret = RUN_ALL_TESTS();
  114. grpc_shutdown();
  115. return ret;
  116. }