/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/rate_limiter_test.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | |
24 | | #ifndef __STDC_FORMAT_MACROS |
25 | | #define __STDC_FORMAT_MACROS |
26 | | #endif |
27 | | |
28 | | #include <inttypes.h> |
29 | | #include <limits> |
30 | | #include <string> |
31 | | #include <gtest/gtest.h> |
32 | | #include "yb/rocksdb/env.h" |
33 | | #include "yb/rocksdb/util/rate_limiter.h" |
34 | | #include "yb/rocksdb/util/random.h" |
35 | | |
36 | | #include "yb/rocksdb/util/testutil.h" |
37 | | |
38 | | namespace rocksdb { |
39 | | |
40 | | class RateLimiterTest : public RocksDBTest {}; |
41 | | |
42 | 1 | TEST_F(RateLimiterTest, StartStop) { |
43 | 1 | std::unique_ptr<RateLimiter> limiter(new GenericRateLimiter(100, 100, 10)); |
44 | 1 | } |
45 | | |
46 | | #ifndef OS_MACOSX |
47 | | TEST_F(RateLimiterTest, Rate) { |
48 | | auto* env = Env::Default(); |
49 | | struct Arg { |
50 | | Arg(int32_t _target_rate, int _burst) |
51 | | : limiter(new GenericRateLimiter(_target_rate, 100 * 1000, 10)), |
52 | | request_size(_target_rate / 10), |
53 | | burst(_burst) {} |
54 | | std::unique_ptr<RateLimiter> limiter; |
55 | | int32_t request_size; |
56 | | int burst; |
57 | | }; |
58 | | |
59 | | auto writer = [](void* p) { |
60 | | auto* thread_env = Env::Default(); |
61 | | auto* arg = static_cast<Arg*>(p); |
62 | | // Test for 10 seconds |
63 | | auto until = thread_env->NowMicros() + 10 * 1000000; |
64 | | Random r((uint32_t)(thread_env->NowNanos() % |
65 | | std::numeric_limits<uint32_t>::max())); |
66 | | while (thread_env->NowMicros() < until) { |
67 | | for (int i = 0; i < static_cast<int>(r.Skewed(arg->burst) + 1); ++i) { |
68 | | arg->limiter->Request(r.Uniform(arg->request_size - 1) + 1, |
69 | | Env::IO_HIGH); |
70 | | } |
71 | | arg->limiter->Request(r.Uniform(arg->request_size - 1) + 1, Env::IO_LOW); |
72 | | } |
73 | | }; |
74 | | |
75 | | for (int i = 1; i <= 16; i *= 2) { |
76 | | int32_t target = i * 1024 * 10; |
77 | | Arg arg(target, i / 4 + 1); |
78 | | int64_t old_total_bytes_through = 0; |
79 | | for (int iter = 1; iter <= 2; ++iter) { |
80 | | // second iteration changes the target dynamically |
81 | | if (iter == 2) { |
82 | | target *= 2; |
83 | | arg.limiter->SetBytesPerSecond(target); |
84 | | } |
85 | | auto start = env->NowMicros(); |
86 | | for (int t = 0; t < i; ++t) { |
87 | | env->StartThread(writer, &arg); |
88 | | } |
89 | | env->WaitForJoin(); |
90 | | |
91 | | auto elapsed = env->NowMicros() - start; |
92 | | double rate = |
93 | | (arg.limiter->GetTotalBytesThrough() - old_total_bytes_through) * |
94 | | 1000000.0 / elapsed; |
95 | | old_total_bytes_through = arg.limiter->GetTotalBytesThrough(); |
96 | | fprintf(stderr, |
97 | | "request size [1 - %" PRIi32 "], limit %" PRIi32 |
98 | | " KB/sec, actual rate: %lf KB/sec, elapsed %.2lf seconds\n", |
99 | | arg.request_size - 1, target / 1024, rate / 1024, |
100 | | elapsed / 1000000.0); |
101 | | |
102 | | ASSERT_GE(rate / target, 0.8); |
103 | | ASSERT_LE(rate / target, 1.2); |
104 | | } |
105 | | } |
106 | | } |
107 | | #endif |
108 | | |
109 | | } // namespace rocksdb |
110 | | |
111 | 13.2k | int main(int argc, char** argv) { |
112 | 13.2k | ::testing::InitGoogleTest(&argc, argv); |
113 | 13.2k | return RUN_ALL_TESTS(); |
114 | 13.2k | } |