histogram_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. *
  3. * Copyright 2015 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 "test/core/util/histogram.h"
  19. #include <grpc/support/log.h>
  20. #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x);
  21. static void test_no_op(void) {
  22. grpc_histogram_destroy(grpc_histogram_create(0.01, 60e9));
  23. }
  24. static void expect_percentile(grpc_histogram* h, double percentile,
  25. double min_expect, double max_expect) {
  26. double got = grpc_histogram_percentile(h, percentile);
  27. gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got,
  28. max_expect);
  29. GPR_ASSERT(min_expect <= got);
  30. GPR_ASSERT(got <= max_expect);
  31. }
  32. static void test_simple(void) {
  33. grpc_histogram* h;
  34. LOG_TEST("test_simple");
  35. h = grpc_histogram_create(0.01, 60e9);
  36. grpc_histogram_add(h, 10000);
  37. grpc_histogram_add(h, 10000);
  38. grpc_histogram_add(h, 11000);
  39. grpc_histogram_add(h, 11000);
  40. expect_percentile(h, 50, 10001, 10999);
  41. GPR_ASSERT(grpc_histogram_mean(h) == 10500);
  42. grpc_histogram_destroy(h);
  43. }
  44. static void test_percentile(void) {
  45. grpc_histogram* h;
  46. double last;
  47. double i;
  48. double cur;
  49. LOG_TEST("test_percentile");
  50. h = grpc_histogram_create(0.05, 1e9);
  51. grpc_histogram_add(h, 2.5);
  52. grpc_histogram_add(h, 2.5);
  53. grpc_histogram_add(h, 8);
  54. grpc_histogram_add(h, 4);
  55. GPR_ASSERT(grpc_histogram_count(h) == 4);
  56. GPR_ASSERT(grpc_histogram_minimum(h) == 2.5);
  57. GPR_ASSERT(grpc_histogram_maximum(h) == 8);
  58. GPR_ASSERT(grpc_histogram_sum(h) == 17);
  59. GPR_ASSERT(grpc_histogram_sum_of_squares(h) == 92.5);
  60. GPR_ASSERT(grpc_histogram_mean(h) == 4.25);
  61. GPR_ASSERT(grpc_histogram_variance(h) == 5.0625);
  62. GPR_ASSERT(grpc_histogram_stddev(h) == 2.25);
  63. expect_percentile(h, -10, 2.5, 2.5);
  64. expect_percentile(h, 0, 2.5, 2.5);
  65. expect_percentile(h, 12.5, 2.5, 2.5);
  66. expect_percentile(h, 25, 2.5, 2.5);
  67. expect_percentile(h, 37.5, 2.5, 2.8);
  68. expect_percentile(h, 50, 3.0, 3.5);
  69. expect_percentile(h, 62.5, 3.5, 4.5);
  70. expect_percentile(h, 75, 5, 7.9);
  71. expect_percentile(h, 100, 8, 8);
  72. expect_percentile(h, 110, 8, 8);
  73. /* test monotonicity */
  74. last = 0.0;
  75. for (i = 0; i < 100.0; i += 0.01) {
  76. cur = grpc_histogram_percentile(h, i);
  77. GPR_ASSERT(cur >= last);
  78. last = cur;
  79. }
  80. grpc_histogram_destroy(h);
  81. }
  82. static void test_merge(void) {
  83. grpc_histogram *h1, *h2;
  84. double last;
  85. double i;
  86. double cur;
  87. LOG_TEST("test_merge");
  88. h1 = grpc_histogram_create(0.05, 1e9);
  89. grpc_histogram_add(h1, 2.5);
  90. grpc_histogram_add(h1, 2.5);
  91. grpc_histogram_add(h1, 8);
  92. grpc_histogram_add(h1, 4);
  93. h2 = grpc_histogram_create(0.01, 1e9);
  94. GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0);
  95. grpc_histogram_destroy(h2);
  96. h2 = grpc_histogram_create(0.05, 1e10);
  97. GPR_ASSERT(grpc_histogram_merge(h1, h2) == 0);
  98. grpc_histogram_destroy(h2);
  99. h2 = grpc_histogram_create(0.05, 1e9);
  100. GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1);
  101. GPR_ASSERT(grpc_histogram_count(h1) == 4);
  102. GPR_ASSERT(grpc_histogram_minimum(h1) == 2.5);
  103. GPR_ASSERT(grpc_histogram_maximum(h1) == 8);
  104. GPR_ASSERT(grpc_histogram_sum(h1) == 17);
  105. GPR_ASSERT(grpc_histogram_sum_of_squares(h1) == 92.5);
  106. GPR_ASSERT(grpc_histogram_mean(h1) == 4.25);
  107. GPR_ASSERT(grpc_histogram_variance(h1) == 5.0625);
  108. GPR_ASSERT(grpc_histogram_stddev(h1) == 2.25);
  109. grpc_histogram_destroy(h2);
  110. h2 = grpc_histogram_create(0.05, 1e9);
  111. grpc_histogram_add(h2, 7.0);
  112. grpc_histogram_add(h2, 17.0);
  113. grpc_histogram_add(h2, 1.0);
  114. GPR_ASSERT(grpc_histogram_merge(h1, h2) == 1);
  115. GPR_ASSERT(grpc_histogram_count(h1) == 7);
  116. GPR_ASSERT(grpc_histogram_minimum(h1) == 1.0);
  117. GPR_ASSERT(grpc_histogram_maximum(h1) == 17.0);
  118. GPR_ASSERT(grpc_histogram_sum(h1) == 42.0);
  119. GPR_ASSERT(grpc_histogram_sum_of_squares(h1) == 431.5);
  120. GPR_ASSERT(grpc_histogram_mean(h1) == 6.0);
  121. /* test monotonicity */
  122. last = 0.0;
  123. for (i = 0; i < 100.0; i += 0.01) {
  124. cur = grpc_histogram_percentile(h1, i);
  125. GPR_ASSERT(cur >= last);
  126. last = cur;
  127. }
  128. grpc_histogram_destroy(h1);
  129. grpc_histogram_destroy(h2);
  130. }
  131. int main(void) {
  132. test_no_op();
  133. test_simple();
  134. test_percentile();
  135. test_merge();
  136. return 0;
  137. }