bvar_recorder_unittest.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // Date 2014/10/13 19:47:59
  18. #include <pthread.h> // pthread_*
  19. #include <cstddef>
  20. #include <memory>
  21. #include <iostream>
  22. #include "butil/time.h"
  23. #include "butil/macros.h"
  24. #include "bvar/recorder.h"
  25. #include "bvar/latency_recorder.h"
  26. #include <gtest/gtest.h>
  27. namespace {
  28. TEST(RecorderTest, test_complement) {
  29. LOG(INFO) << "sizeof(LatencyRecorder)=" << sizeof(bvar::LatencyRecorder)
  30. << " " << sizeof(bvar::detail::Percentile)
  31. << " " << sizeof(bvar::Maxer<int64_t>)
  32. << " " << sizeof(bvar::IntRecorder)
  33. << " " << sizeof(bvar::Window<bvar::IntRecorder>)
  34. << " " << sizeof(bvar::Window<bvar::detail::Percentile>);
  35. for (int a = -10000000; a < 10000000; ++a) {
  36. const uint64_t complement = bvar::IntRecorder::_get_complement(a);
  37. const int64_t b = bvar::IntRecorder::_extend_sign_bit(complement);
  38. ASSERT_EQ(a, b);
  39. }
  40. }
  41. TEST(RecorderTest, test_compress) {
  42. const uint64_t num = 125345;
  43. const uint64_t sum = 26032906;
  44. const uint64_t compressed = bvar::IntRecorder::_compress(num, sum);
  45. ASSERT_EQ(num, bvar::IntRecorder::_get_num(compressed));
  46. ASSERT_EQ(sum, bvar::IntRecorder::_get_sum(compressed));
  47. }
  48. TEST(RecorderTest, test_compress_negtive_number) {
  49. for (int a = -10000000; a < 10000000; ++a) {
  50. const uint64_t sum = bvar::IntRecorder::_get_complement(a);
  51. const uint64_t num = 123456;
  52. const uint64_t compressed = bvar::IntRecorder::_compress(num, sum);
  53. ASSERT_EQ(num, bvar::IntRecorder::_get_num(compressed));
  54. ASSERT_EQ(a, bvar::IntRecorder::_extend_sign_bit(bvar::IntRecorder::_get_sum(compressed)));
  55. }
  56. }
  57. TEST(RecorderTest, sanity) {
  58. {
  59. bvar::IntRecorder recorder;
  60. ASSERT_TRUE(recorder.valid());
  61. ASSERT_EQ(0, recorder.expose("var1"));
  62. for (size_t i = 0; i < 100; ++i) {
  63. recorder << 2;
  64. }
  65. ASSERT_EQ(2l, (int64_t)recorder.average());
  66. ASSERT_EQ("2", bvar::Variable::describe_exposed("var1"));
  67. std::vector<std::string> vars;
  68. bvar::Variable::list_exposed(&vars);
  69. ASSERT_EQ(1UL, vars.size());
  70. ASSERT_EQ("var1", vars[0]);
  71. ASSERT_EQ(1UL, bvar::Variable::count_exposed());
  72. }
  73. ASSERT_EQ(0UL, bvar::Variable::count_exposed());
  74. }
  75. TEST(RecorderTest, window) {
  76. bvar::IntRecorder c1;
  77. ASSERT_TRUE(c1.valid());
  78. bvar::Window<bvar::IntRecorder> w1(&c1, 1);
  79. bvar::Window<bvar::IntRecorder> w2(&c1, 2);
  80. bvar::Window<bvar::IntRecorder> w3(&c1, 3);
  81. const int N = 10000;
  82. int64_t last_time = butil::gettimeofday_us();
  83. for (int i = 1; i <= N; ++i) {
  84. c1 << i;
  85. int64_t now = butil::gettimeofday_us();
  86. if (now - last_time >= 1000000L) {
  87. last_time = now;
  88. LOG(INFO) << "c1=" << c1 << " w1=" << w1 << " w2=" << w2 << " w3=" << w3;
  89. } else {
  90. usleep(950);
  91. }
  92. }
  93. }
  94. TEST(RecorderTest, negative) {
  95. bvar::IntRecorder recorder;
  96. ASSERT_TRUE(recorder.valid());
  97. for (size_t i = 0; i < 3; ++i) {
  98. recorder << -2;
  99. }
  100. ASSERT_EQ(-2, recorder.average());
  101. }
  102. TEST(RecorderTest, positive_overflow) {
  103. bvar::IntRecorder recorder1;
  104. ASSERT_TRUE(recorder1.valid());
  105. for (int i = 0; i < 5; ++i) {
  106. recorder1 << std::numeric_limits<int64_t>::max();
  107. }
  108. ASSERT_EQ(std::numeric_limits<int>::max(), recorder1.average());
  109. bvar::IntRecorder recorder2;
  110. ASSERT_TRUE(recorder2.valid());
  111. recorder2.set_debug_name("recorder2");
  112. for (int i = 0; i < 5; ++i) {
  113. recorder2 << std::numeric_limits<int64_t>::max();
  114. }
  115. ASSERT_EQ(std::numeric_limits<int>::max(), recorder2.average());
  116. bvar::IntRecorder recorder3;
  117. ASSERT_TRUE(recorder3.valid());
  118. recorder3.expose("recorder3");
  119. for (int i = 0; i < 5; ++i) {
  120. recorder3 << std::numeric_limits<int64_t>::max();
  121. }
  122. ASSERT_EQ(std::numeric_limits<int>::max(), recorder3.average());
  123. bvar::LatencyRecorder latency1;
  124. latency1.expose("latency1");
  125. latency1 << std::numeric_limits<int64_t>::max();
  126. bvar::LatencyRecorder latency2;
  127. latency2 << std::numeric_limits<int64_t>::max();
  128. }
  129. TEST(RecorderTest, negtive_overflow) {
  130. bvar::IntRecorder recorder1;
  131. ASSERT_TRUE(recorder1.valid());
  132. for (int i = 0; i < 5; ++i) {
  133. recorder1 << std::numeric_limits<int64_t>::min();
  134. }
  135. ASSERT_EQ(std::numeric_limits<int>::min(), recorder1.average());
  136. bvar::IntRecorder recorder2;
  137. ASSERT_TRUE(recorder2.valid());
  138. recorder2.set_debug_name("recorder2");
  139. for (int i = 0; i < 5; ++i) {
  140. recorder2 << std::numeric_limits<int64_t>::min();
  141. }
  142. ASSERT_EQ(std::numeric_limits<int>::min(), recorder2.average());
  143. bvar::IntRecorder recorder3;
  144. ASSERT_TRUE(recorder3.valid());
  145. recorder3.expose("recorder3");
  146. for (int i = 0; i < 5; ++i) {
  147. recorder3 << std::numeric_limits<int64_t>::min();
  148. }
  149. ASSERT_EQ(std::numeric_limits<int>::min(), recorder3.average());
  150. bvar::LatencyRecorder latency1;
  151. latency1.expose("latency1");
  152. latency1 << std::numeric_limits<int64_t>::min();
  153. bvar::LatencyRecorder latency2;
  154. latency2 << std::numeric_limits<int64_t>::min();
  155. }
  156. const size_t OPS_PER_THREAD = 20000000;
  157. static void *thread_counter(void *arg) {
  158. bvar::IntRecorder *recorder = (bvar::IntRecorder *)arg;
  159. butil::Timer timer;
  160. timer.start();
  161. for (int i = 0; i < (int)OPS_PER_THREAD; ++i) {
  162. *recorder << i;
  163. }
  164. timer.stop();
  165. return (void *)(timer.n_elapsed());
  166. }
  167. TEST(RecorderTest, perf) {
  168. bvar::IntRecorder recorder;
  169. ASSERT_TRUE(recorder.valid());
  170. pthread_t threads[8];
  171. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  172. pthread_create(&threads[i], NULL, &thread_counter, (void *)&recorder);
  173. }
  174. long totol_time = 0;
  175. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  176. void *ret;
  177. pthread_join(threads[i], &ret);
  178. totol_time += (long)ret;
  179. }
  180. ASSERT_EQ(((int64_t)OPS_PER_THREAD - 1) / 2, recorder.average());
  181. LOG(INFO) << "Recorder takes " << totol_time / (OPS_PER_THREAD * ARRAY_SIZE(threads))
  182. << "ns per sample with " << ARRAY_SIZE(threads)
  183. << " threads";
  184. }
  185. } // namespace