3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "src/core/lib/transport/bdp_estimator.h"
23 #include <gtest/gtest.h>
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
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"
35 extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type);
42 gpr_timespec fake_gpr_now(gpr_clock_type clock_type) {
46 ts.clock_type = clock_type;
50 void inc_time(void) { g_clock += 30; }
53 TEST(BdpEstimatorTest, NoOp) { BdpEstimator est("test"); }
55 TEST(BdpEstimatorTest, EstimateBdpNoSamples) {
56 BdpEstimator est("test");
61 void AddSamples(BdpEstimator* estimator, int64_t* samples, size_t n) {
62 estimator->AddIncomingBytes(1234567);
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]);
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();
76 void AddSample(BdpEstimator* estimator, int64_t sample) {
77 AddSamples(estimator, &sample, 1);
81 TEST(BdpEstimatorTest, GetEstimate1Sample) {
82 BdpEstimator est("test");
87 TEST(BdpEstimatorTest, GetEstimate2Samples) {
88 BdpEstimator est("test");
94 TEST(BdpEstimatorTest, GetEstimate3Samples) {
95 BdpEstimator est("test");
103 int64_t NextPow2(int64_t v) {
116 class BdpEstimatorRandomTest : public ::testing::TestWithParam<size_t> {};
118 TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues) {
119 BdpEstimator est("test");
120 const int kMaxSample = 65535;
121 int min = kMaxSample;
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);
129 EXPECT_LE(est.EstimateBdp(), GPR_MAX(65536, 2 * NextPow2(max)))
130 << " min:" << min << " max:" << max << " sample:" << sample;
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));
139 } // namespace testing
140 } // namespace grpc_core
142 int main(int argc, char** argv) {
143 grpc::testing::TestEnvironment env(argc, argv);
144 gpr_now_impl = grpc_core::testing::fake_gpr_now;
146 grpc_timer_manager_set_threading(false);
147 ::testing::InitGoogleTest(&argc, argv);
148 int ret = RUN_ALL_TESTS();