Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / core / transport / bdp_estimator_test.cc
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include "src/core/lib/transport/bdp_estimator.h"
20
21 #include <limits.h>
22
23 #include <gtest/gtest.h>
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
29
30 #include "src/core/lib/gpr/string.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "src/core/lib/iomgr/timer_manager.h"
33 #include "test/core/util/test_config.h"
34
35 extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type);
36
37 namespace grpc_core {
38 namespace testing {
39 namespace {
40 int g_clock = 0;
41
42 gpr_timespec fake_gpr_now(gpr_clock_type clock_type) {
43   gpr_timespec ts;
44   ts.tv_sec = g_clock;
45   ts.tv_nsec = 0;
46   ts.clock_type = clock_type;
47   return ts;
48 }
49
50 void inc_time(void) { g_clock += 30; }
51 }  // namespace
52
53 TEST(BdpEstimatorTest, NoOp) { BdpEstimator est("test"); }
54
55 TEST(BdpEstimatorTest, EstimateBdpNoSamples) {
56   BdpEstimator est("test");
57   est.EstimateBdp();
58 }
59
60 namespace {
61 void AddSamples(BdpEstimator* estimator, int64_t* samples, size_t n) {
62   estimator->AddIncomingBytes(1234567);
63   inc_time();
64   grpc_core::ExecCtx exec_ctx;
65   estimator->SchedulePing();
66   estimator->StartPing();
67   for (size_t i = 0; i < n; i++) {
68     estimator->AddIncomingBytes(samples[i]);
69   }
70   gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
71                                gpr_time_from_millis(1, GPR_TIMESPAN)));
72   grpc_core::ExecCtx::Get()->InvalidateNow();
73   estimator->CompletePing();
74 }
75
76 void AddSample(BdpEstimator* estimator, int64_t sample) {
77   AddSamples(estimator, &sample, 1);
78 }
79 }  // namespace
80
81 TEST(BdpEstimatorTest, GetEstimate1Sample) {
82   BdpEstimator est("test");
83   AddSample(&est, 100);
84   est.EstimateBdp();
85 }
86
87 TEST(BdpEstimatorTest, GetEstimate2Samples) {
88   BdpEstimator est("test");
89   AddSample(&est, 100);
90   AddSample(&est, 100);
91   est.EstimateBdp();
92 }
93
94 TEST(BdpEstimatorTest, GetEstimate3Samples) {
95   BdpEstimator est("test");
96   AddSample(&est, 100);
97   AddSample(&est, 100);
98   AddSample(&est, 100);
99   est.EstimateBdp();
100 }
101
102 namespace {
103 int64_t NextPow2(int64_t v) {
104   v--;
105   v |= v >> 1;
106   v |= v >> 2;
107   v |= v >> 4;
108   v |= v >> 8;
109   v |= v >> 16;
110   v |= v >> 32;
111   v++;
112   return v;
113 }
114 }  // namespace
115
116 class BdpEstimatorRandomTest : public ::testing::TestWithParam<size_t> {};
117
118 TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues) {
119   BdpEstimator est("test");
120   const int kMaxSample = 65535;
121   int min = kMaxSample;
122   int max = 0;
123   for (size_t i = 0; i < GetParam(); i++) {
124     int sample = rand() % (kMaxSample + 1);
125     if (sample < min) min = sample;
126     if (sample > max) max = sample;
127     AddSample(&est, sample);
128     if (i >= 3) {
129       EXPECT_LE(est.EstimateBdp(), GPR_MAX(65536, 2 * NextPow2(max)))
130           << " min:" << min << " max:" << max << " sample:" << sample;
131     }
132   }
133 }
134
135 INSTANTIATE_TEST_SUITE_P(TooManyNames, BdpEstimatorRandomTest,
136                          ::testing::Values(3, 4, 6, 9, 13, 19, 28, 42, 63, 94,
137                                            141, 211, 316, 474, 711));
138
139 }  // namespace testing
140 }  // namespace grpc_core
141
142 int main(int argc, char** argv) {
143   grpc::testing::TestEnvironment env(argc, argv);
144   gpr_now_impl = grpc_core::testing::fake_gpr_now;
145   grpc_init();
146   grpc_timer_manager_set_threading(false);
147   ::testing::InitGoogleTest(&argc, argv);
148   int ret = RUN_ALL_TESTS();
149   grpc_shutdown();
150   return ret;
151 }