bdp_estimator_test.cc
Go to the documentation of this file.
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 
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>
29 
35 
36 extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type);
37 
38 namespace grpc_core {
39 namespace testing {
40 namespace {
41 int g_clock = 123;
42 Mutex mu_;
43 
44 gpr_timespec fake_gpr_now(gpr_clock_type clock_type) {
45  MutexLock lock(&mu_);
46  gpr_timespec ts;
47  ts.tv_sec = g_clock;
48  ts.tv_nsec = 0;
49  ts.clock_type = clock_type;
50  return ts;
51 }
52 
53 void inc_time(void) {
54  MutexLock lock(&mu_);
55  g_clock += 30;
56 }
57 } // namespace
58 
59 TEST(BdpEstimatorTest, NoOp) { BdpEstimator est("test"); }
60 
61 TEST(BdpEstimatorTest, EstimateBdpNoSamples) {
62  BdpEstimator est("test");
63  est.EstimateBdp();
64 }
65 
66 namespace {
67 void AddSamples(BdpEstimator* estimator, int64_t* samples, size_t n) {
68  estimator->AddIncomingBytes(1234567);
69  inc_time();
71  estimator->SchedulePing();
72  estimator->StartPing();
73  for (size_t i = 0; i < n; i++) {
74  estimator->AddIncomingBytes(samples[i]);
75  }
79  estimator->CompletePing();
80 }
81 
82 void AddSample(BdpEstimator* estimator, int64_t sample) {
83  AddSamples(estimator, &sample, 1);
84 }
85 } // namespace
86 
87 TEST(BdpEstimatorTest, GetEstimate1Sample) {
88  BdpEstimator est("test");
89  AddSample(&est, 100);
90  est.EstimateBdp();
91 }
92 
93 TEST(BdpEstimatorTest, GetEstimate2Samples) {
94  BdpEstimator est("test");
95  AddSample(&est, 100);
96  AddSample(&est, 100);
97  est.EstimateBdp();
98 }
99 
100 TEST(BdpEstimatorTest, GetEstimate3Samples) {
101  BdpEstimator est("test");
102  AddSample(&est, 100);
103  AddSample(&est, 100);
104  AddSample(&est, 100);
105  est.EstimateBdp();
106 }
107 
108 namespace {
109 int64_t NextPow2(int64_t v) {
110  v--;
111  v |= v >> 1;
112  v |= v >> 2;
113  v |= v >> 4;
114  v |= v >> 8;
115  v |= v >> 16;
116  v |= v >> 32;
117  v++;
118  return v;
119 }
120 } // namespace
121 
123 
124 TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues) {
125  BdpEstimator est("test");
126  const int kMaxSample = 65535;
127  int min = kMaxSample;
128  int max = 0;
129  for (size_t i = 0; i < GetParam(); i++) {
130  int sample = rand() % (kMaxSample + 1);
131  if (sample < min) min = sample;
132  if (sample > max) max = sample;
133  AddSample(&est, sample);
134  if (i >= 3) {
135  EXPECT_LE(est.EstimateBdp(), std::max(int64_t(65536), 2 * NextPow2(max)))
136  << " min:" << min << " max:" << max << " sample:" << sample;
137  }
138  }
139 }
140 
141 INSTANTIATE_TEST_SUITE_P(TooManyNames, BdpEstimatorRandomTest,
142  ::testing::Values(3, 4, 6, 9, 13, 19, 28, 42, 63, 94,
143  141, 211, 316, 474, 711));
144 
145 } // namespace testing
146 } // namespace grpc_core
147 
148 int main(int argc, char** argv) {
149  grpc::testing::TestEnvironment env(&argc, argv);
150  gpr_now_impl = grpc_core::testing::fake_gpr_now;
151  grpc_init();
153  ::testing::InitGoogleTest(&argc, argv);
154  int ret = RUN_ALL_TESTS();
155  grpc_shutdown();
156  return ret;
157 }
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition: gpr_types.h:52
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
gpr_timespec::tv_sec
int64_t tv_sec
Definition: gpr_types.h:51
testing
Definition: aws_request_signer_test.cc:25
log.h
MutexLock
#define MutexLock(x)
Definition: bloaty/third_party/re2/util/mutex.h:125
generate.env
env
Definition: generate.py:37
grpc_timer_manager_set_threading
void grpc_timer_manager_set_threading(bool enabled)
Definition: iomgr/timer_manager.cc:346
NoOp
Definition: bm_call_create.cc:477
timer_manager.h
grpc_core
Definition: call_metric_recorder.h:31
string.h
useful.h
grpc_core::BdpEstimator::EstimateBdp
int64_t EstimateBdp() const
Definition: bdp_estimator.h:42
gpr_now_impl
gpr_timespec(* gpr_now_impl)(gpr_clock_type clock_type)
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
grpc_core::testing::BdpEstimatorRandomTest
Definition: bdp_estimator_test.cc:122
mu_
Mutex mu_
Definition: oob_backend_metric.cc:115
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
string_util.h
grpc_core::BdpEstimator::StartPing
void StartPing()
Definition: bdp_estimator.h:63
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::BdpEstimator::SchedulePing
void SchedulePing()
Definition: bdp_estimator.h:50
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
min
#define min(a, b)
Definition: qsort.h:83
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc_core::ExecCtx
Definition: exec_ctx.h:97
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
bdp_estimator.h
main
int main(int argc, char **argv)
Definition: bdp_estimator_test.cc:148
gpr_timespec::clock_type
gpr_clock_type clock_type
Definition: gpr_types.h:55
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
testing::Values
internal::ValueArray< T... > Values(T... v)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:335
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
gpr_timespec
struct gpr_timespec gpr_timespec
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_core::BdpEstimator::CompletePing
Timestamp CompletePing()
Definition: bdp_estimator.cc:44
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
g_clock
static timeval g_clock
Definition: test_state.cc:28
grpc_core::testing::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(TooManyNames, BdpEstimatorRandomTest, ::testing::Values(3, 4, 6, 9, 13, 19, 28, 42, 63, 94, 141, 211, 316, 474, 711))
exec_ctx.h
gpr_time_from_millis
GPRAPI gpr_timespec gpr_time_from_millis(int64_t ms, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:119
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
grpc_core::BdpEstimator::AddIncomingBytes
void AddIncomingBytes(int64_t num_bytes)
Definition: bdp_estimator.h:45
grpc_core::testing::TEST_P
TEST_P(BdpEstimatorRandomTest, GetEstimateRandomValues)
Definition: bdp_estimator_test.cc:124
gpr_timespec
Definition: gpr_types.h:50
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_core::BdpEstimator
Definition: bdp_estimator.h:37
grpc_core::ExecCtx::InvalidateNow
void InvalidateNow()
Definition: exec_ctx.h:188
gpr_clock_type
gpr_clock_type
Definition: gpr_types.h:34


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45