periodic_sampler_test.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2019 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/profiling/internal/periodic_sampler.h"
  15. #include <thread> // NOLINT(build/c++11)
  16. #include "gmock/gmock.h"
  17. #include "gtest/gtest.h"
  18. #include "absl/base/attributes.h"
  19. #include "absl/base/macros.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace profiling_internal {
  23. namespace {
  24. using testing::Eq;
  25. using testing::Return;
  26. using testing::StrictMock;
  27. class MockPeriodicSampler : public PeriodicSamplerBase {
  28. public:
  29. virtual ~MockPeriodicSampler() = default;
  30. MOCK_METHOD(int, period, (), (const, noexcept));
  31. MOCK_METHOD(int64_t, GetExponentialBiased, (int), (noexcept));
  32. };
  33. TEST(PeriodicSamplerBaseTest, Sample) {
  34. StrictMock<MockPeriodicSampler> sampler;
  35. EXPECT_CALL(sampler, period()).Times(3).WillRepeatedly(Return(16));
  36. EXPECT_CALL(sampler, GetExponentialBiased(16))
  37. .WillOnce(Return(2))
  38. .WillOnce(Return(3))
  39. .WillOnce(Return(4));
  40. EXPECT_FALSE(sampler.Sample());
  41. EXPECT_TRUE(sampler.Sample());
  42. EXPECT_FALSE(sampler.Sample());
  43. EXPECT_FALSE(sampler.Sample());
  44. EXPECT_TRUE(sampler.Sample());
  45. EXPECT_FALSE(sampler.Sample());
  46. EXPECT_FALSE(sampler.Sample());
  47. EXPECT_FALSE(sampler.Sample());
  48. }
  49. TEST(PeriodicSamplerBaseTest, ImmediatelySample) {
  50. StrictMock<MockPeriodicSampler> sampler;
  51. EXPECT_CALL(sampler, period()).Times(2).WillRepeatedly(Return(16));
  52. EXPECT_CALL(sampler, GetExponentialBiased(16))
  53. .WillOnce(Return(1))
  54. .WillOnce(Return(2))
  55. .WillOnce(Return(3));
  56. EXPECT_TRUE(sampler.Sample());
  57. EXPECT_FALSE(sampler.Sample());
  58. EXPECT_TRUE(sampler.Sample());
  59. EXPECT_FALSE(sampler.Sample());
  60. EXPECT_FALSE(sampler.Sample());
  61. }
  62. TEST(PeriodicSamplerBaseTest, Disabled) {
  63. StrictMock<MockPeriodicSampler> sampler;
  64. EXPECT_CALL(sampler, period()).Times(3).WillRepeatedly(Return(0));
  65. EXPECT_FALSE(sampler.Sample());
  66. EXPECT_FALSE(sampler.Sample());
  67. EXPECT_FALSE(sampler.Sample());
  68. }
  69. TEST(PeriodicSamplerBaseTest, AlwaysOn) {
  70. StrictMock<MockPeriodicSampler> sampler;
  71. EXPECT_CALL(sampler, period()).Times(3).WillRepeatedly(Return(1));
  72. EXPECT_TRUE(sampler.Sample());
  73. EXPECT_TRUE(sampler.Sample());
  74. EXPECT_TRUE(sampler.Sample());
  75. }
  76. TEST(PeriodicSamplerBaseTest, Disable) {
  77. StrictMock<MockPeriodicSampler> sampler;
  78. EXPECT_CALL(sampler, period()).WillOnce(Return(16));
  79. EXPECT_CALL(sampler, GetExponentialBiased(16)).WillOnce(Return(3));
  80. EXPECT_FALSE(sampler.Sample());
  81. EXPECT_FALSE(sampler.Sample());
  82. EXPECT_CALL(sampler, period()).Times(2).WillRepeatedly(Return(0));
  83. EXPECT_FALSE(sampler.Sample());
  84. EXPECT_FALSE(sampler.Sample());
  85. }
  86. TEST(PeriodicSamplerBaseTest, Enable) {
  87. StrictMock<MockPeriodicSampler> sampler;
  88. EXPECT_CALL(sampler, period()).WillOnce(Return(0));
  89. EXPECT_FALSE(sampler.Sample());
  90. EXPECT_CALL(sampler, period()).Times(2).WillRepeatedly(Return(16));
  91. EXPECT_CALL(sampler, GetExponentialBiased(16))
  92. .Times(2)
  93. .WillRepeatedly(Return(3));
  94. EXPECT_FALSE(sampler.Sample());
  95. EXPECT_FALSE(sampler.Sample());
  96. EXPECT_TRUE(sampler.Sample());
  97. EXPECT_FALSE(sampler.Sample());
  98. EXPECT_FALSE(sampler.Sample());
  99. }
  100. TEST(PeriodicSamplerTest, ConstructConstInit) {
  101. struct Tag {};
  102. ABSL_CONST_INIT static PeriodicSampler<Tag> sampler;
  103. (void)sampler;
  104. }
  105. TEST(PeriodicSamplerTest, DefaultPeriod0) {
  106. struct Tag {};
  107. PeriodicSampler<Tag> sampler;
  108. EXPECT_THAT(sampler.period(), Eq(0));
  109. }
  110. TEST(PeriodicSamplerTest, DefaultPeriod) {
  111. struct Tag {};
  112. PeriodicSampler<Tag, 100> sampler;
  113. EXPECT_THAT(sampler.period(), Eq(100));
  114. }
  115. TEST(PeriodicSamplerTest, SetGlobalPeriod) {
  116. struct Tag1 {};
  117. struct Tag2 {};
  118. PeriodicSampler<Tag1, 25> sampler1;
  119. PeriodicSampler<Tag2, 50> sampler2;
  120. EXPECT_THAT(sampler1.period(), Eq(25));
  121. EXPECT_THAT(sampler2.period(), Eq(50));
  122. std::thread thread([] {
  123. PeriodicSampler<Tag1, 25> sampler1;
  124. PeriodicSampler<Tag2, 50> sampler2;
  125. EXPECT_THAT(sampler1.period(), Eq(25));
  126. EXPECT_THAT(sampler2.period(), Eq(50));
  127. sampler1.SetGlobalPeriod(10);
  128. sampler2.SetGlobalPeriod(20);
  129. });
  130. thread.join();
  131. EXPECT_THAT(sampler1.period(), Eq(10));
  132. EXPECT_THAT(sampler2.period(), Eq(20));
  133. }
  134. } // namespace
  135. } // namespace profiling_internal
  136. ABSL_NAMESPACE_END
  137. } // namespace absl