• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 #include <grpc/support/string_util.h>
25 
26 #include <gtest/gtest.h>
27 #include <limits.h>
28 
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/gpr/useful.h"
31 #include "src/core/lib/iomgr/timer_manager.h"
32 #include "test/core/util/test_config.h"
33 
34 extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type);
35 
36 namespace grpc_core {
37 namespace testing {
38 namespace {
39 int g_clock = 0;
40 
fake_gpr_now(gpr_clock_type clock_type)41 gpr_timespec fake_gpr_now(gpr_clock_type clock_type) {
42   gpr_timespec ts;
43   ts.tv_sec = g_clock;
44   ts.tv_nsec = 0;
45   ts.clock_type = clock_type;
46   return ts;
47 }
48 
inc_time(void)49 void inc_time(void) { g_clock += 30; }
50 }  // namespace
51 
TEST(BdpEstimatorTest,NoOp)52 TEST(BdpEstimatorTest, NoOp) { BdpEstimator est("test"); }
53 
TEST(BdpEstimatorTest,EstimateBdpNoSamples)54 TEST(BdpEstimatorTest, EstimateBdpNoSamples) {
55   BdpEstimator est("test");
56   est.EstimateBdp();
57 }
58 
59 namespace {
AddSamples(BdpEstimator * estimator,int64_t * samples,size_t n)60 void AddSamples(BdpEstimator* estimator, int64_t* samples, size_t n) {
61   estimator->AddIncomingBytes(1234567);
62   inc_time();
63   grpc_core::ExecCtx exec_ctx;
64   estimator->SchedulePing();
65   estimator->StartPing();
66   for (size_t i = 0; i < n; i++) {
67     estimator->AddIncomingBytes(samples[i]);
68   }
69   gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
70                                gpr_time_from_millis(1, GPR_TIMESPAN)));
71   grpc_core::ExecCtx::Get()->InvalidateNow();
72   estimator->CompletePing();
73 }
74 
AddSample(BdpEstimator * estimator,int64_t sample)75 void AddSample(BdpEstimator* estimator, int64_t sample) {
76   AddSamples(estimator, &sample, 1);
77 }
78 }  // namespace
79 
TEST(BdpEstimatorTest,GetEstimate1Sample)80 TEST(BdpEstimatorTest, GetEstimate1Sample) {
81   BdpEstimator est("test");
82   AddSample(&est, 100);
83   est.EstimateBdp();
84 }
85 
TEST(BdpEstimatorTest,GetEstimate2Samples)86 TEST(BdpEstimatorTest, GetEstimate2Samples) {
87   BdpEstimator est("test");
88   AddSample(&est, 100);
89   AddSample(&est, 100);
90   est.EstimateBdp();
91 }
92 
TEST(BdpEstimatorTest,GetEstimate3Samples)93 TEST(BdpEstimatorTest, GetEstimate3Samples) {
94   BdpEstimator est("test");
95   AddSample(&est, 100);
96   AddSample(&est, 100);
97   AddSample(&est, 100);
98   est.EstimateBdp();
99 }
100 
101 namespace {
NextPow2(int64_t v)102 int64_t NextPow2(int64_t v) {
103   v--;
104   v |= v >> 1;
105   v |= v >> 2;
106   v |= v >> 4;
107   v |= v >> 8;
108   v |= v >> 16;
109   v |= v >> 32;
110   v++;
111   return v;
112 }
113 }  // namespace
114 
115 class BdpEstimatorRandomTest : public ::testing::TestWithParam<size_t> {};
116 
TEST_P(BdpEstimatorRandomTest,GetEstimateRandomValues)117 TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues) {
118   BdpEstimator est("test");
119   const int kMaxSample = 65535;
120   int min = kMaxSample;
121   int max = 0;
122   for (size_t i = 0; i < GetParam(); i++) {
123     int sample = rand() % (kMaxSample + 1);
124     if (sample < min) min = sample;
125     if (sample > max) max = sample;
126     AddSample(&est, sample);
127     if (i >= 3) {
128       EXPECT_LE(est.EstimateBdp(), GPR_MAX(65536, 2 * NextPow2(max)))
129           << " min:" << min << " max:" << max << " sample:" << sample;
130     }
131   }
132 }
133 
134 INSTANTIATE_TEST_CASE_P(TooManyNames, BdpEstimatorRandomTest,
135                         ::testing::Values(3, 4, 6, 9, 13, 19, 28, 42, 63, 94,
136                                           141, 211, 316, 474, 711));
137 
138 }  // namespace testing
139 }  // namespace grpc_core
140 
main(int argc,char ** argv)141 int main(int argc, char** argv) {
142   grpc_test_init(argc, argv);
143   gpr_now_impl = grpc_core::testing::fake_gpr_now;
144   grpc_init();
145   grpc_timer_manager_set_threading(false);
146   ::testing::InitGoogleTest(&argc, argv);
147   int ret = RUN_ALL_TESTS();
148   grpc_shutdown();
149   return ret;
150 }
151