TokenBucketTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/test/TokenBucketTest.h>
  17. #include <folly/portability/GTest.h>
  18. using namespace folly;
  19. TEST(TokenBucket, ReverseTime) {
  20. const double rate = 1000;
  21. TokenBucket tokenBucket(rate, rate * 0.01 + 1e-6, 0);
  22. size_t count = 0;
  23. while (tokenBucket.consume(1, 0.1)) {
  24. count += 1;
  25. }
  26. EXPECT_EQ(10, count);
  27. // Going backwards in time has no affect on the toke count (this protects
  28. // against different threads providing out of order timestamps).
  29. double tokensBefore = tokenBucket.available();
  30. EXPECT_FALSE(tokenBucket.consume(1, 0.09999999));
  31. EXPECT_EQ(tokensBefore, tokenBucket.available());
  32. }
  33. TEST_P(TokenBucketTest, sanity) {
  34. std::pair<double, double> params = GetParam();
  35. double rate = params.first;
  36. double consumeSize = params.second;
  37. const double tenMillisecondBurst = rate * 0.010;
  38. // Select a burst size of 10 milliseconds at the max rate or the consume size
  39. // if 10 ms at rate is too small.
  40. const double burstSize = std::max(consumeSize, tenMillisecondBurst);
  41. TokenBucket tokenBucket(rate, burstSize, 0);
  42. double tokenCounter = 0;
  43. double currentTime = 0;
  44. // Simulate time advancing 10 seconds
  45. for (; currentTime <= 10.0; currentTime += 0.001) {
  46. EXPECT_FALSE(tokenBucket.consume(burstSize + 1, currentTime));
  47. while (tokenBucket.consume(consumeSize, currentTime)) {
  48. tokenCounter += consumeSize;
  49. }
  50. // Tokens consumed should exceed some lower bound based on rate.
  51. // Note: The token bucket implementation is not precise, so the lower bound
  52. // is somewhat fudged. The upper bound is accurate however.
  53. EXPECT_LE(rate * currentTime * 0.9 - 1, tokenCounter);
  54. // Tokens consumed should not exceed some upper bound based on rate.
  55. EXPECT_GE(rate * currentTime + 1e-6, tokenCounter);
  56. }
  57. }
  58. static std::vector<std::pair<double, double>> rateToConsumeSize = {
  59. {100, 1},
  60. {1000, 1},
  61. {10000, 1},
  62. {10000, 5},
  63. };
  64. INSTANTIATE_TEST_CASE_P(
  65. TokenBucket,
  66. TokenBucketTest,
  67. ::testing::ValuesIn(rateToConsumeSize));
  68. void doTokenBucketTest(double maxQps, double consumeSize) {
  69. const double tenMillisecondBurst = maxQps * 0.010;
  70. // Select a burst size of 10 milliseconds at the max rate or the consume size
  71. // if 10 ms at maxQps is too small.
  72. const double burstSize = std::max(consumeSize, tenMillisecondBurst);
  73. TokenBucket tokenBucket(maxQps, burstSize, 0);
  74. double tokenCounter = 0;
  75. double currentTime = 0;
  76. // Simulate time advancing 10 seconds
  77. for (; currentTime <= 10.0; currentTime += 0.001) {
  78. EXPECT_FALSE(tokenBucket.consume(burstSize + 1, currentTime));
  79. while (tokenBucket.consume(consumeSize, currentTime)) {
  80. tokenCounter += consumeSize;
  81. }
  82. // Tokens consumed should exceed some lower bound based on maxQps.
  83. // Note: The token bucket implementation is not precise, so the lower bound
  84. // is somewhat fudged. The upper bound is accurate however.
  85. EXPECT_LE(maxQps * currentTime * 0.9 - 1, tokenCounter);
  86. // Tokens consumed should not exceed some upper bound based on maxQps.
  87. EXPECT_GE(maxQps * currentTime + 1e-6, tokenCounter);
  88. }
  89. }
  90. TEST(TokenBucket, sanity) {
  91. doTokenBucketTest(100, 1);
  92. doTokenBucketTest(1000, 1);
  93. doTokenBucketTest(10000, 1);
  94. // Consume more than one at a time.
  95. doTokenBucketTest(10000, 5);
  96. }
  97. TEST(TokenBucket, ReverseTime2) {
  98. const double rate = 1000;
  99. TokenBucket tokenBucket(rate, rate * 0.01 + 1e-6);
  100. size_t count = 0;
  101. while (tokenBucket.consume(1, 0.1)) {
  102. count += 1;
  103. }
  104. EXPECT_EQ(10, count);
  105. // Going backwards in time has no affect on the toke count (this protects
  106. // against different threads providing out of order timestamps).
  107. double tokensBefore = tokenBucket.available();
  108. EXPECT_FALSE(tokenBucket.consume(1, 0.09999999));
  109. EXPECT_EQ(tokensBefore, tokenBucket.available());
  110. }
  111. TEST(TokenBucket, drainOnFail) {
  112. DynamicTokenBucket tokenBucket;
  113. // Almost empty the bucket
  114. EXPECT_TRUE(tokenBucket.consume(9, 10, 10, 1));
  115. // Request more tokens than available
  116. EXPECT_FALSE(tokenBucket.consume(5, 10, 10, 1));
  117. EXPECT_DOUBLE_EQ(1.0, tokenBucket.available(10, 10, 1));
  118. // Again request more tokens than available, but ask to drain
  119. EXPECT_DOUBLE_EQ(1.0, tokenBucket.consumeOrDrain(5, 10, 10, 1));
  120. EXPECT_DOUBLE_EQ(0.0, tokenBucket.consumeOrDrain(1, 10, 10, 1));
  121. }