2014-05-22 02:16:17 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2014-08-21 22:40:44 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2014-05-22 02:16:17 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/macros.h"
|
|
|
|
#include "packager/mpd/base/bandwidth_estimator.h"
|
2014-05-22 02:16:17 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2014-05-22 02:16:17 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const int kNumBlocksForEstimate = 5;
|
|
|
|
const int kFirstOneBlockForEstimate = -1;
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint64_t kBitsInByte = 8;
|
2014-05-22 02:16:17 +00:00
|
|
|
const int kEstimateRoundError = 1;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Make sure that averaging of 5 blocks works, and also when there aren't all 5
|
|
|
|
// blocks.
|
|
|
|
TEST(BandwidthEstimatorTest, FiveBlocksFiveBlocksAdded) {
|
|
|
|
BandwidthEstimator be(kNumBlocksForEstimate);
|
|
|
|
const double kDuration = 1.0;
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint64_t kExpectedResults[] = {
|
|
|
|
// Harmonic mean of [1 * 8], [1 * 8, 2 * 8], ...
|
|
|
|
// 8 is the number of bits in a byte and 1, 2, ... is from the loop
|
|
|
|
// counter below.
|
|
|
|
// Note that these are rounded up.
|
|
|
|
8,
|
|
|
|
11,
|
|
|
|
14,
|
|
|
|
16,
|
|
|
|
18
|
2014-05-22 02:16:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
COMPILE_ASSERT(kNumBlocksForEstimate == arraysize(kExpectedResults),
|
|
|
|
incorrect_number_of_expectations);
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint64_t i = 1; i <= arraysize(kExpectedResults); ++i) {
|
2014-05-22 02:16:17 +00:00
|
|
|
be.AddBlock(i, kDuration);
|
|
|
|
EXPECT_EQ(kExpectedResults[i - 1], be.Estimate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// More practical situation where a lot of blocks get added but only the last 5
|
|
|
|
// are considered for the estimate.
|
|
|
|
TEST(BandwidthEstimatorTest, FiveBlocksNormal) {
|
|
|
|
BandwidthEstimator be(kNumBlocksForEstimate);
|
|
|
|
const double kDuration = 10.0;
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint64_t kNumBlocksToAdd = 200;
|
|
|
|
const uint64_t kExptectedEstimate = 800;
|
2014-05-22 02:16:17 +00:00
|
|
|
|
|
|
|
// Doesn't matter what gets passed to the estimator except for the last 5
|
|
|
|
// blocks which we add kExptectedEstimate / 8 bytes per second so that the
|
|
|
|
// estimate becomes kExptectedEstimate.
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint64_t i = 1; i <= kNumBlocksToAdd; ++i) {
|
2014-05-22 02:16:17 +00:00
|
|
|
if (i > kNumBlocksToAdd - kNumBlocksForEstimate) {
|
|
|
|
be.AddBlock(kExptectedEstimate * kDuration / kBitsInByte, kDuration);
|
|
|
|
} else {
|
|
|
|
be.AddBlock(i, kDuration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_NEAR(kExptectedEstimate, be.Estimate(), kEstimateRoundError);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Average all the blocks!
|
|
|
|
TEST(BandwidthEstimatorTest, AllBlocks) {
|
|
|
|
BandwidthEstimator be(BandwidthEstimator::kUseAllBlocks);
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint64_t kNumBlocksToAdd = 100;
|
2014-05-22 02:16:17 +00:00
|
|
|
const double kDuration = 1.0;
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint64_t i = 1; i <= kNumBlocksToAdd; ++i)
|
2014-05-22 02:16:17 +00:00
|
|
|
be.AddBlock(i, kDuration);
|
|
|
|
|
|
|
|
// The harmonic mean of 8, 16, ... , 800; rounded up.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint64_t kExptectedEstimate = 155;
|
2014-05-22 02:16:17 +00:00
|
|
|
EXPECT_EQ(kExptectedEstimate, be.Estimate());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use only the first one.
|
|
|
|
TEST(BandwidthEstimatorTest, FirstOneBlock) {
|
|
|
|
BandwidthEstimator be(kFirstOneBlockForEstimate);
|
|
|
|
const double kDuration = 11.0;
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint64_t kExptectedEstimate = 123456;
|
2014-05-22 02:16:17 +00:00
|
|
|
be.AddBlock(kExptectedEstimate * kDuration / kBitsInByte, kDuration);
|
|
|
|
|
|
|
|
// Anything. Should be ignored.
|
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
|
|
be.AddBlock(100000, 10);
|
|
|
|
EXPECT_EQ(kExptectedEstimate, be.Estimate());
|
|
|
|
}
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
} // edash_packager
|