blob: 005eef07f8f7a45c0d936e3e71b9b962eaae1323 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
wub04e96e32019-05-10 13:41:18 -07006
7#include <limits>
8
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "net/third_party/quiche/src/quic/core/quic_time.h"
10#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
11
12namespace quic {
13namespace test {
14
15class QuicBandwidthTest : public QuicTest {};
16
17TEST_F(QuicBandwidthTest, FromTo) {
18 EXPECT_EQ(QuicBandwidth::FromKBitsPerSecond(1),
19 QuicBandwidth::FromBitsPerSecond(1000));
20 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(1),
21 QuicBandwidth::FromBytesPerSecond(1000));
22 EXPECT_EQ(QuicBandwidth::FromBitsPerSecond(8000),
23 QuicBandwidth::FromBytesPerSecond(1000));
24 EXPECT_EQ(QuicBandwidth::FromKBitsPerSecond(8),
25 QuicBandwidth::FromKBytesPerSecond(1));
26
27 EXPECT_EQ(0, QuicBandwidth::Zero().ToBitsPerSecond());
28 EXPECT_EQ(0, QuicBandwidth::Zero().ToKBitsPerSecond());
29 EXPECT_EQ(0, QuicBandwidth::Zero().ToBytesPerSecond());
30 EXPECT_EQ(0, QuicBandwidth::Zero().ToKBytesPerSecond());
31
32 EXPECT_EQ(1, QuicBandwidth::FromBitsPerSecond(1000).ToKBitsPerSecond());
33 EXPECT_EQ(1000, QuicBandwidth::FromKBitsPerSecond(1).ToBitsPerSecond());
34 EXPECT_EQ(1, QuicBandwidth::FromBytesPerSecond(1000).ToKBytesPerSecond());
35 EXPECT_EQ(1000, QuicBandwidth::FromKBytesPerSecond(1).ToBytesPerSecond());
36}
37
38TEST_F(QuicBandwidthTest, Add) {
39 QuicBandwidth bandwidht_1 = QuicBandwidth::FromKBitsPerSecond(1);
40 QuicBandwidth bandwidht_2 = QuicBandwidth::FromKBytesPerSecond(1);
41
42 EXPECT_EQ(9000, (bandwidht_1 + bandwidht_2).ToBitsPerSecond());
43 EXPECT_EQ(9000, (bandwidht_2 + bandwidht_1).ToBitsPerSecond());
44}
45
46TEST_F(QuicBandwidthTest, Subtract) {
47 QuicBandwidth bandwidht_1 = QuicBandwidth::FromKBitsPerSecond(1);
48 QuicBandwidth bandwidht_2 = QuicBandwidth::FromKBytesPerSecond(1);
49
50 EXPECT_EQ(7000, (bandwidht_2 - bandwidht_1).ToBitsPerSecond());
51}
52
53TEST_F(QuicBandwidthTest, TimeDelta) {
54 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(1000),
55 QuicBandwidth::FromBytesAndTimeDelta(
56 1000, QuicTime::Delta::FromMilliseconds(1)));
57
58 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(10),
59 QuicBandwidth::FromBytesAndTimeDelta(
60 1000, QuicTime::Delta::FromMilliseconds(100)));
61}
62
63TEST_F(QuicBandwidthTest, Scale) {
64 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(500),
65 QuicBandwidth::FromKBytesPerSecond(1000) * 0.5f);
66 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(750),
67 0.75f * QuicBandwidth::FromKBytesPerSecond(1000));
68 EXPECT_EQ(QuicBandwidth::FromKBytesPerSecond(1250),
69 QuicBandwidth::FromKBytesPerSecond(1000) * 1.25f);
70
71 // Ensure we are rounding correctly within a 1bps level of precision.
72 EXPECT_EQ(QuicBandwidth::FromBitsPerSecond(5),
73 QuicBandwidth::FromBitsPerSecond(9) * 0.5f);
74 EXPECT_EQ(QuicBandwidth::FromBitsPerSecond(2),
75 QuicBandwidth::FromBitsPerSecond(12) * 0.2f);
76}
77
78TEST_F(QuicBandwidthTest, BytesPerPeriod) {
79 EXPECT_EQ(2000u, QuicBandwidth::FromKBytesPerSecond(2000).ToBytesPerPeriod(
80 QuicTime::Delta::FromMilliseconds(1)));
81 EXPECT_EQ(2u, QuicBandwidth::FromKBytesPerSecond(2000).ToKBytesPerPeriod(
82 QuicTime::Delta::FromMilliseconds(1)));
83 EXPECT_EQ(200000u, QuicBandwidth::FromKBytesPerSecond(2000).ToBytesPerPeriod(
84 QuicTime::Delta::FromMilliseconds(100)));
85 EXPECT_EQ(200u, QuicBandwidth::FromKBytesPerSecond(2000).ToKBytesPerPeriod(
86 QuicTime::Delta::FromMilliseconds(100)));
wub92e211d2019-06-11 04:55:16 -070087
88 // 1599 * 1001 = 1600599 bits/ms = 200.074875 bytes/s.
89 EXPECT_EQ(200u, QuicBandwidth::FromBitsPerSecond(1599).ToBytesPerPeriod(
90 QuicTime::Delta::FromMilliseconds(1001)));
91
92 EXPECT_EQ(200u, QuicBandwidth::FromBitsPerSecond(1599).ToKBytesPerPeriod(
93 QuicTime::Delta::FromSeconds(1001)));
QUICHE teama6ef0a62019-03-07 20:34:33 -050094}
95
96TEST_F(QuicBandwidthTest, TransferTime) {
97 EXPECT_EQ(QuicTime::Delta::FromSeconds(1),
98 QuicBandwidth::FromKBytesPerSecond(1).TransferTime(1000));
99 EXPECT_EQ(QuicTime::Delta::Zero(), QuicBandwidth::Zero().TransferTime(1000));
100}
101
102TEST_F(QuicBandwidthTest, RelOps) {
103 const QuicBandwidth b1 = QuicBandwidth::FromKBitsPerSecond(1);
104 const QuicBandwidth b2 = QuicBandwidth::FromKBytesPerSecond(2);
105 EXPECT_EQ(b1, b1);
106 EXPECT_NE(b1, b2);
107 EXPECT_LT(b1, b2);
108 EXPECT_GT(b2, b1);
109 EXPECT_LE(b1, b1);
110 EXPECT_LE(b1, b2);
111 EXPECT_GE(b1, b1);
112 EXPECT_GE(b2, b1);
113}
114
zhongyi26818ca2019-05-13 17:50:37 -0700115TEST_F(QuicBandwidthTest, DebuggingValue) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500116 EXPECT_EQ("128 bits/s (16 bytes/s)",
zhongyi26818ca2019-05-13 17:50:37 -0700117 QuicBandwidth::FromBytesPerSecond(16).ToDebuggingValue());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500118 EXPECT_EQ("4096 bits/s (512 bytes/s)",
zhongyi26818ca2019-05-13 17:50:37 -0700119 QuicBandwidth::FromBytesPerSecond(512).ToDebuggingValue());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500120
121 QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(1000 * 50);
zhongyi26818ca2019-05-13 17:50:37 -0700122 EXPECT_EQ("400.00 kbits/s (50.00 kbytes/s)", bandwidth.ToDebuggingValue());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500123
124 bandwidth = bandwidth * 1000;
zhongyi26818ca2019-05-13 17:50:37 -0700125 EXPECT_EQ("400.00 Mbits/s (50.00 Mbytes/s)", bandwidth.ToDebuggingValue());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500126
127 bandwidth = bandwidth * 1000;
zhongyi26818ca2019-05-13 17:50:37 -0700128 EXPECT_EQ("400.00 Gbits/s (50.00 Gbytes/s)", bandwidth.ToDebuggingValue());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129}
130
wub04e96e32019-05-10 13:41:18 -0700131TEST_F(QuicBandwidthTest, SpecialValues) {
132 EXPECT_EQ(0, QuicBandwidth::Zero().ToBitsPerSecond());
133 EXPECT_EQ(std::numeric_limits<int64_t>::max(),
134 QuicBandwidth::Infinite().ToBitsPerSecond());
135
136 EXPECT_TRUE(QuicBandwidth::Zero().IsZero());
137 EXPECT_FALSE(QuicBandwidth::Zero().IsInfinite());
138
139 EXPECT_TRUE(QuicBandwidth::Infinite().IsInfinite());
140 EXPECT_FALSE(QuicBandwidth::Infinite().IsZero());
141}
142
QUICHE teama6ef0a62019-03-07 20:34:33 -0500143} // namespace test
144} // namespace quic