proxygen
TokenBucketTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (TokenBucket, ReverseTime)
 
 TEST_P (TokenBucketTest, sanity)
 
 INSTANTIATE_TEST_CASE_P (TokenBucket, TokenBucketTest,::testing::ValuesIn(rateToConsumeSize))
 
void doTokenBucketTest (double maxQps, double consumeSize)
 
 TEST (TokenBucket, sanity)
 
 TEST (TokenBucket, ReverseTime2)
 
 TEST (TokenBucket, drainOnFail)
 

Variables

static std::vector< std::pair< double, double > > rateToConsumeSize
 

Function Documentation

void doTokenBucketTest ( double  maxQps,
double  consumeSize 
)

Definition at line 77 of file TokenBucketTest.cpp.

References folly::BasicTokenBucket< Clock >::consume(), EXPECT_FALSE, EXPECT_GE, EXPECT_LE, and max.

Referenced by TEST().

77  {
78  const double tenMillisecondBurst = maxQps * 0.010;
79  // Select a burst size of 10 milliseconds at the max rate or the consume size
80  // if 10 ms at maxQps is too small.
81  const double burstSize = std::max(consumeSize, tenMillisecondBurst);
82  TokenBucket tokenBucket(maxQps, burstSize, 0);
83  double tokenCounter = 0;
84  double currentTime = 0;
85  // Simulate time advancing 10 seconds
86  for (; currentTime <= 10.0; currentTime += 0.001) {
87  EXPECT_FALSE(tokenBucket.consume(burstSize + 1, currentTime));
88  while (tokenBucket.consume(consumeSize, currentTime)) {
89  tokenCounter += consumeSize;
90  }
91  // Tokens consumed should exceed some lower bound based on maxQps.
92  // Note: The token bucket implementation is not precise, so the lower bound
93  // is somewhat fudged. The upper bound is accurate however.
94  EXPECT_LE(maxQps * currentTime * 0.9 - 1, tokenCounter);
95  // Tokens consumed should not exceed some upper bound based on maxQps.
96  EXPECT_GE(maxQps * currentTime + 1e-6, tokenCounter);
97  }
98 }
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
INSTANTIATE_TEST_CASE_P ( TokenBucket  ,
TokenBucketTest  ,
::testing::ValuesIn(rateToConsumeSize  
)
TEST ( TokenBucket  ,
ReverseTime   
)

Definition at line 23 of file TokenBucketTest.cpp.

References folly::BasicTokenBucket< Clock >::available(), folly::BasicTokenBucket< Clock >::consume(), count, EXPECT_EQ, and EXPECT_FALSE.

23  {
24  const double rate = 1000;
25  TokenBucket tokenBucket(rate, rate * 0.01 + 1e-6, 0);
26  size_t count = 0;
27  while (tokenBucket.consume(1, 0.1)) {
28  count += 1;
29  }
30  EXPECT_EQ(10, count);
31  // Going backwards in time has no affect on the toke count (this protects
32  // against different threads providing out of order timestamps).
33  double tokensBefore = tokenBucket.available();
34  EXPECT_FALSE(tokenBucket.consume(1, 0.09999999));
35  EXPECT_EQ(tokensBefore, tokenBucket.available());
36 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int * count
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( TokenBucket  ,
sanity   
)

Definition at line 100 of file TokenBucketTest.cpp.

References doTokenBucketTest().

100  {
101  doTokenBucketTest(100, 1);
102  doTokenBucketTest(1000, 1);
103  doTokenBucketTest(10000, 1);
104  // Consume more than one at a time.
105  doTokenBucketTest(10000, 5);
106 }
void doTokenBucketTest(double maxQps, double consumeSize)
TEST ( TokenBucket  ,
ReverseTime2   
)

Definition at line 108 of file TokenBucketTest.cpp.

References folly::BasicTokenBucket< Clock >::available(), folly::BasicTokenBucket< Clock >::consume(), count, EXPECT_EQ, and EXPECT_FALSE.

108  {
109  const double rate = 1000;
110  TokenBucket tokenBucket(rate, rate * 0.01 + 1e-6);
111  size_t count = 0;
112  while (tokenBucket.consume(1, 0.1)) {
113  count += 1;
114  }
115  EXPECT_EQ(10, count);
116  // Going backwards in time has no affect on the toke count (this protects
117  // against different threads providing out of order timestamps).
118  double tokensBefore = tokenBucket.available();
119  EXPECT_FALSE(tokenBucket.consume(1, 0.09999999));
120  EXPECT_EQ(tokensBefore, tokenBucket.available());
121 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int * count
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( TokenBucket  ,
drainOnFail   
)

Definition at line 123 of file TokenBucketTest.cpp.

References folly::BasicDynamicTokenBucket< Clock >::available(), folly::BasicDynamicTokenBucket< Clock >::consume(), folly::BasicDynamicTokenBucket< Clock >::consumeOrDrain(), EXPECT_DOUBLE_EQ, EXPECT_FALSE, and EXPECT_TRUE.

123  {
124  DynamicTokenBucket tokenBucket;
125 
126  // Almost empty the bucket
127  EXPECT_TRUE(tokenBucket.consume(9, 10, 10, 1));
128 
129  // Request more tokens than available
130  EXPECT_FALSE(tokenBucket.consume(5, 10, 10, 1));
131  EXPECT_DOUBLE_EQ(1.0, tokenBucket.available(10, 10, 1));
132 
133  // Again request more tokens than available, but ask to drain
134  EXPECT_DOUBLE_EQ(1.0, tokenBucket.consumeOrDrain(5, 10, 10, 1));
135  EXPECT_DOUBLE_EQ(0.0, tokenBucket.consumeOrDrain(1, 10, 10, 1));
136 }
bool consume(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
Definition: TokenBucket.h:121
double available(double rate, double burstSize, double nowInSeconds=defaultClockNow()) const noexcept
Definition: TokenBucket.h:182
double consumeOrDrain(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
Definition: TokenBucket.h:154
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_DOUBLE_EQ(val1, val2)
Definition: gtest.h:2031
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_P ( TokenBucketTest  ,
sanity   
)

Definition at line 38 of file TokenBucketTest.cpp.

References folly::BasicTokenBucket< Clock >::consume(), EXPECT_FALSE, EXPECT_GE, EXPECT_LE, max, and params.

38  {
39  std::pair<double, double> params = GetParam();
40  double rate = params.first;
41  double consumeSize = params.second;
42 
43  const double tenMillisecondBurst = rate * 0.010;
44  // Select a burst size of 10 milliseconds at the max rate or the consume size
45  // if 10 ms at rate is too small.
46  const double burstSize = std::max(consumeSize, tenMillisecondBurst);
47  TokenBucket tokenBucket(rate, burstSize, 0);
48  double tokenCounter = 0;
49  double currentTime = 0;
50  // Simulate time advancing 10 seconds
51  for (; currentTime <= 10.0; currentTime += 0.001) {
52  EXPECT_FALSE(tokenBucket.consume(burstSize + 1, currentTime));
53  while (tokenBucket.consume(consumeSize, currentTime)) {
54  tokenCounter += consumeSize;
55  }
56  // Tokens consumed should exceed some lower bound based on rate.
57  // Note: The token bucket implementation is not precise, so the lower bound
58  // is somewhat fudged. The upper bound is accurate however.
59  EXPECT_LE(rate * currentTime * 0.9 - 1, tokenCounter);
60  // Tokens consumed should not exceed some upper bound based on rate.
61  EXPECT_GE(rate * currentTime + 1e-6, tokenCounter);
62  }
63 }
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
LogLevel max
Definition: LogLevel.cpp:31
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
constexpr Params params[]
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862

Variable Documentation

std::vector<std::pair<double, double> > rateToConsumeSize
static
Initial value:
= {
{100, 1},
{1000, 1},
{10000, 1},
{10000, 5},
}

Definition at line 65 of file TokenBucketTest.cpp.