21 using namespace folly;
24 const double rate = 1000;
25 TokenBucket tokenBucket(rate, rate * 0.01 + 1e-6, 0);
27 while (tokenBucket.
consume(1, 0.1)) {
33 double tokensBefore = tokenBucket.
available();
39 std::pair<double, double>
params = GetParam();
40 double rate = params.first;
41 double consumeSize = params.second;
43 const double tenMillisecondBurst = rate * 0.010;
46 const double burstSize =
std::max(consumeSize, tenMillisecondBurst);
48 double tokenCounter = 0;
49 double currentTime = 0;
51 for (; currentTime <= 10.0; currentTime += 0.001) {
53 while (tokenBucket.
consume(consumeSize, currentTime)) {
54 tokenCounter += consumeSize;
59 EXPECT_LE(rate * currentTime * 0.9 - 1, tokenCounter);
61 EXPECT_GE(rate * currentTime + 1e-6, tokenCounter);
75 ::testing::ValuesIn(rateToConsumeSize));
78 const double tenMillisecondBurst = maxQps * 0.010;
81 const double burstSize =
std::max(consumeSize, tenMillisecondBurst);
83 double tokenCounter = 0;
84 double currentTime = 0;
86 for (; currentTime <= 10.0; currentTime += 0.001) {
88 while (tokenBucket.
consume(consumeSize, currentTime)) {
89 tokenCounter += consumeSize;
94 EXPECT_LE(maxQps * currentTime * 0.9 - 1, tokenCounter);
96 EXPECT_GE(maxQps * currentTime + 1e-6, tokenCounter);
109 const double rate = 1000;
112 while (tokenBucket.
consume(1, 0.1)) {
118 double tokensBefore = tokenBucket.
available();
#define EXPECT_LE(val1, val2)
#define EXPECT_EQ(val1, val2)
—— Concurrent Priority Queue Implementation ——
#define EXPECT_GE(val1, val2)
void doTokenBucketTest(double maxQps, double consumeSize)
double available(double nowInSeconds=defaultClockNow()) const
bool consume(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
constexpr Params params[]
double available(double rate, double burstSize, double nowInSeconds=defaultClockNow()) const noexcept
INSTANTIATE_TEST_CASE_P(TokenBucket, TokenBucketTest,::testing::ValuesIn(rateToConsumeSize))
double consumeOrDrain(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
bool consume(double toConsume, double nowInSeconds=defaultClockNow())
#define EXPECT_TRUE(condition)
#define EXPECT_DOUBLE_EQ(val1, val2)
#define EXPECT_FALSE(condition)
static std::vector< std::pair< double, double > > rateToConsumeSize
TEST(SequencedExecutor, CPUThreadPoolExecutor)
TEST_P(TokenBucketTest, sanity)