1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/pacing/bitrate_prober.h"
#include <algorithm>
#include "api/transport/field_trial_based_config.h"
#include "api/transport/network_types.h"
#include "api/units/data_rate.h"
#include "api/units/data_size.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "test/explicit_key_value_config.h"
#include "test/gtest.h"
namespace webrtc {
TEST(BitrateProberTest, VerifyStatesAndTimeBetweenProbes) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
EXPECT_FALSE(prober.is_probing());
Timestamp now = Timestamp::Zero();
const Timestamp start_time = now;
EXPECT_EQ(prober.NextProbeTime(now), Timestamp::PlusInfinity());
const DataRate kTestBitrate1 = DataRate::KilobitsPerSec(900);
const DataRate kTestBitrate2 = DataRate::KilobitsPerSec(1800);
const int kClusterSize = 5;
const DataSize kProbeSize = DataSize::Bytes(1000);
const TimeDelta kMinProbeDuration = TimeDelta::Millis(15);
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = kTestBitrate1,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = kTestBitrate2,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 1});
EXPECT_FALSE(prober.is_probing());
prober.OnIncomingPacket(kProbeSize);
EXPECT_TRUE(prober.is_probing());
EXPECT_EQ(0, prober.CurrentCluster(now)->probe_cluster_id);
// First packet should probe as soon as possible.
EXPECT_EQ(Timestamp::MinusInfinity(), prober.NextProbeTime(now));
for (int i = 0; i < kClusterSize; ++i) {
now = std::max(now, prober.NextProbeTime(now));
EXPECT_EQ(now, std::max(now, prober.NextProbeTime(now)));
EXPECT_EQ(0, prober.CurrentCluster(now)->probe_cluster_id);
prober.ProbeSent(now, kProbeSize);
}
EXPECT_GE(now - start_time, kMinProbeDuration);
// Verify that the actual bitrate is withing 10% of the target.
DataRate bitrate = kProbeSize * (kClusterSize - 1) / (now - start_time);
EXPECT_GT(bitrate, kTestBitrate1 * 0.9);
EXPECT_LT(bitrate, kTestBitrate1 * 1.1);
now = std::max(now, prober.NextProbeTime(now));
Timestamp probe2_started = now;
for (int i = 0; i < kClusterSize; ++i) {
now = std::max(now, prober.NextProbeTime(now));
EXPECT_EQ(now, std::max(now, prober.NextProbeTime(now)));
EXPECT_EQ(1, prober.CurrentCluster(now)->probe_cluster_id);
prober.ProbeSent(now, kProbeSize);
}
// Verify that the actual bitrate is withing 10% of the target.
TimeDelta duration = now - probe2_started;
EXPECT_GE(duration, kMinProbeDuration);
bitrate = (kProbeSize * (kClusterSize - 1)) / duration;
EXPECT_GT(bitrate, kTestBitrate2 * 0.9);
EXPECT_LT(bitrate, kTestBitrate2 * 1.1);
EXPECT_EQ(prober.NextProbeTime(now), Timestamp::PlusInfinity());
EXPECT_FALSE(prober.is_probing());
}
TEST(BitrateProberTest, DoesntProbeWithoutRecentPackets) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
const DataSize kProbeSize = DataSize::Bytes(1000);
Timestamp now = Timestamp::Zero();
EXPECT_EQ(prober.NextProbeTime(now), Timestamp::PlusInfinity());
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = DataRate::KilobitsPerSec(900),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
EXPECT_FALSE(prober.is_probing());
prober.OnIncomingPacket(kProbeSize);
EXPECT_TRUE(prober.is_probing());
EXPECT_EQ(now, std::max(now, prober.NextProbeTime(now)));
prober.ProbeSent(now, kProbeSize);
}
TEST(BitrateProberTest, DiscardsDelayedProbes) {
const TimeDelta kMaxProbeDelay = TimeDelta::Millis(3);
const test::ExplicitKeyValueConfig trials(
"WebRTC-Bwe-ProbingBehavior/"
"abort_delayed_probes:1,"
"max_probe_delay:3ms/");
BitrateProber prober(trials);
const DataSize kProbeSize = DataSize::Bytes(1000);
Timestamp now = Timestamp::Zero();
// Add two probe clusters.
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = DataRate::KilobitsPerSec(900),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(kProbeSize);
EXPECT_TRUE(prober.is_probing());
EXPECT_EQ(prober.CurrentCluster(now)->probe_cluster_id, 0);
// Advance to first probe time and indicate sent probe.
now = std::max(now, prober.NextProbeTime(now));
prober.ProbeSent(now, kProbeSize);
// Advance time 1ms past timeout for the next probe.
Timestamp next_probe_time = prober.NextProbeTime(now);
EXPECT_GT(next_probe_time, now);
now += next_probe_time - now + kMaxProbeDelay + TimeDelta::Millis(1);
// Still indicates the time we wanted to probe at.
EXPECT_EQ(prober.NextProbeTime(now), next_probe_time);
// First and only cluster removed due to timeout.
EXPECT_FALSE(prober.CurrentCluster(now).has_value());
}
TEST(BitrateProberTest, LimitsNumberOfPendingProbeClusters) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
const DataSize kProbeSize = DataSize::Bytes(1000);
Timestamp now = Timestamp::Zero();
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = DataRate::KilobitsPerSec(900),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(kProbeSize);
ASSERT_TRUE(prober.is_probing());
ASSERT_EQ(prober.CurrentCluster(now)->probe_cluster_id, 0);
for (int i = 1; i < 11; ++i) {
prober.CreateProbeCluster(
{.at_time = now,
.target_data_rate = DataRate::KilobitsPerSec(900),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = i});
prober.OnIncomingPacket(kProbeSize);
}
// Expect some clusters has been dropped.
EXPECT_TRUE(prober.is_probing());
EXPECT_GE(prober.CurrentCluster(now)->probe_cluster_id, 5);
Timestamp max_expected_probe_time = now + TimeDelta::Seconds(1);
while (prober.is_probing() && now < max_expected_probe_time) {
now = std::max(now, prober.NextProbeTime(now));
prober.ProbeSent(now, kProbeSize);
}
EXPECT_FALSE(prober.is_probing());
}
TEST(BitrateProberTest, DoesntInitializeProbingForSmallPackets) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
prober.SetEnabled(true);
ASSERT_FALSE(prober.is_probing());
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = DataRate::KilobitsPerSec(1000),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(DataSize::Bytes(100));
EXPECT_FALSE(prober.is_probing());
}
TEST(BitrateProberTest, DoesInitializeProbingForSmallPacketsIfConfigured) {
const test::ExplicitKeyValueConfig config(
"WebRTC-Bwe-ProbingBehavior/"
"min_packet_size:0bytes/");
BitrateProber prober(config);
prober.SetEnabled(true);
ASSERT_FALSE(prober.is_probing());
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = DataRate::KilobitsPerSec(1000),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(DataSize::Bytes(10));
EXPECT_TRUE(prober.is_probing());
}
TEST(BitrateProberTest, VerifyProbeSizeOnHighBitrate) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
const DataRate kHighBitrate = DataRate::KilobitsPerSec(10000); // 10 Mbps
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = kHighBitrate,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
// Probe size should ensure a minimum of 1 ms interval.
EXPECT_GT(prober.RecommendedMinProbeSize(),
kHighBitrate * TimeDelta::Millis(1));
}
TEST(BitrateProberTest, ProbeSizeCanBeSetInProbeClusterConfig) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
prober.SetEnabled(true);
const DataRate kHighBitrate = DataRate::KilobitsPerSec(10000); // 10 Mbps
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = kHighBitrate,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(20),
.target_probe_count = 5,
.id = 0});
EXPECT_EQ(prober.RecommendedMinProbeSize(),
kHighBitrate * TimeDelta::Millis(20));
prober.OnIncomingPacket(DataSize::Bytes(1000));
// Next time to send probe should be "min_probe_delta" if the recommended
// number of bytes has been sent.
prober.ProbeSent(Timestamp::Zero(), prober.RecommendedMinProbeSize());
EXPECT_EQ(prober.NextProbeTime(Timestamp::Zero()),
Timestamp::Zero() + TimeDelta::Millis(20));
}
TEST(BitrateProberTest, MinumumNumberOfProbingPackets) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
// Even when probing at a low bitrate we expect a minimum number
// of packets to be sent.
const DataRate kBitrate = DataRate::KilobitsPerSec(100);
const DataSize kPacketSize = DataSize::Bytes(1000);
Timestamp now = Timestamp::Zero();
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = kBitrate,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(kPacketSize);
for (int i = 0; i < 5; ++i) {
EXPECT_TRUE(prober.is_probing());
prober.ProbeSent(now, kPacketSize);
}
EXPECT_FALSE(prober.is_probing());
}
TEST(BitrateProberTest, ScaleBytesUsedForProbing) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
const DataRate kBitrate = DataRate::KilobitsPerSec(10000); // 10 Mbps.
const DataSize kPacketSize = DataSize::Bytes(1000);
const DataSize kExpectedDataSent = kBitrate * TimeDelta::Millis(15);
Timestamp now = Timestamp::Zero();
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = kBitrate,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(kPacketSize);
DataSize data_sent = DataSize::Zero();
while (data_sent < kExpectedDataSent) {
ASSERT_TRUE(prober.is_probing());
prober.ProbeSent(now, kPacketSize);
data_sent += kPacketSize;
}
EXPECT_FALSE(prober.is_probing());
}
TEST(BitrateProberTest, HighBitrateProbing) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
const DataRate kBitrate = DataRate::KilobitsPerSec(1000000); // 1 Gbps.
const DataSize kPacketSize = DataSize::Bytes(1000);
const DataSize kExpectedDataSent = kBitrate * TimeDelta::Millis(15);
Timestamp now = Timestamp::Zero();
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = kBitrate,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(kPacketSize);
DataSize data_sent = DataSize::Zero();
while (data_sent < kExpectedDataSent) {
ASSERT_TRUE(prober.is_probing());
prober.ProbeSent(now, kPacketSize);
data_sent += kPacketSize;
}
EXPECT_FALSE(prober.is_probing());
}
TEST(BitrateProberTest, ProbeClusterTimeout) {
const FieldTrialBasedConfig config;
BitrateProber prober(config);
const DataRate kBitrate = DataRate::KilobitsPerSec(300);
const DataSize kSmallPacketSize = DataSize::Bytes(20);
// Expecting two probe clusters of 5 packets each.
const DataSize kExpectedDataSent = kSmallPacketSize * 2 * 5;
const TimeDelta kTimeout = TimeDelta::Millis(5000);
Timestamp now = Timestamp::Zero();
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = kBitrate,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
prober.OnIncomingPacket(kSmallPacketSize);
EXPECT_FALSE(prober.is_probing());
now += kTimeout;
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = kBitrate / 10,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 1});
prober.OnIncomingPacket(kSmallPacketSize);
EXPECT_FALSE(prober.is_probing());
now += TimeDelta::Millis(1);
prober.CreateProbeCluster({.at_time = now,
.target_data_rate = kBitrate / 10,
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 2});
prober.OnIncomingPacket(kSmallPacketSize);
EXPECT_TRUE(prober.is_probing());
DataSize data_sent = DataSize::Zero();
while (data_sent < kExpectedDataSent) {
ASSERT_TRUE(prober.is_probing());
prober.ProbeSent(now, kSmallPacketSize);
data_sent += kSmallPacketSize;
}
EXPECT_FALSE(prober.is_probing());
}
TEST(BitrateProberTest, CanProbeImmediatelyIfConfigured) {
const test::ExplicitKeyValueConfig trials(
"WebRTC-Bwe-ProbingBehavior/min_packet_size:0/");
BitrateProber prober(trials);
prober.CreateProbeCluster({.at_time = Timestamp::Zero(),
.target_data_rate = DataRate::KilobitsPerSec(300),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 5,
.id = 0});
EXPECT_TRUE(prober.is_probing());
}
TEST(BitrateProberTest, CanProbeImmediatelyAgainAfterProbeIfConfigured) {
const test::ExplicitKeyValueConfig trials(
"WebRTC-Bwe-ProbingBehavior/min_packet_size:0/");
BitrateProber prober(trials);
ProbeClusterConfig cluster_config = {
.at_time = Timestamp::Zero(),
.target_data_rate = DataRate::KilobitsPerSec(300),
.target_duration = TimeDelta::Millis(15),
.min_probe_delta = TimeDelta::Millis(2),
.target_probe_count = 1,
.id = 0};
prober.CreateProbeCluster(cluster_config);
ASSERT_TRUE(prober.is_probing());
(cluster_config.target_data_rate * cluster_config.target_duration).bytes();
prober.ProbeSent(
Timestamp::Zero() + TimeDelta::Millis(1),
cluster_config.target_data_rate * cluster_config.target_duration);
ASSERT_FALSE(prober.is_probing());
cluster_config.id = 2;
cluster_config.at_time = Timestamp::Zero() + TimeDelta::Millis(100);
prober.CreateProbeCluster(cluster_config);
EXPECT_TRUE(prober.is_probing());
}
} // namespace webrtc
|