1 /* 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "webrtc/modules/remote_bitrate_estimator/test/metric_recorder.h" 12 13 #include <math.h> 14 #include <algorithm> 15 #include <vector> 16 17 #include "testing/gtest/include/gtest/gtest.h" 18 19 namespace webrtc { 20 namespace testing { 21 namespace bwe { 22 23 class MetricRecorderTest : public ::testing::Test { 24 public: 25 MetricRecorderTest() : metric_recorder_("Test", 0, nullptr, nullptr) {} 26 27 ~MetricRecorderTest() {} 28 29 protected: 30 MetricRecorder metric_recorder_; 31 }; 32 33 TEST_F(MetricRecorderTest, NoPackets) { 34 EXPECT_EQ(metric_recorder_.AverageBitrateKbps(0), 0); 35 EXPECT_EQ(metric_recorder_.DelayStdDev(), 0.0); 36 EXPECT_EQ(metric_recorder_.NthDelayPercentile(0), 0); 37 EXPECT_EQ(metric_recorder_.NthDelayPercentile(5), 0); 38 EXPECT_EQ(metric_recorder_.NthDelayPercentile(95), 0); 39 EXPECT_EQ(metric_recorder_.NthDelayPercentile(100), 0); 40 } 41 42 TEST_F(MetricRecorderTest, RegularPackets) { 43 const size_t kPayloadSizeBytes = 1200; 44 const int64_t kDelayMs = 20; 45 const int64_t kInterpacketGapMs = 5; 46 const int kNumPackets = 1000; 47 48 for (int i = 0; i < kNumPackets; ++i) { 49 int64_t arrival_time_ms = kInterpacketGapMs * i + kDelayMs; 50 metric_recorder_.UpdateTimeMs(arrival_time_ms); 51 metric_recorder_.PushDelayMs(kDelayMs, arrival_time_ms); 52 metric_recorder_.PushThroughputBytes(kPayloadSizeBytes, arrival_time_ms); 53 } 54 55 EXPECT_NEAR( 56 metric_recorder_.AverageBitrateKbps(0), 57 static_cast<uint32_t>(kPayloadSizeBytes * 8) / (kInterpacketGapMs), 10); 58 59 EXPECT_EQ(metric_recorder_.DelayStdDev(), 0.0); 60 61 EXPECT_EQ(metric_recorder_.NthDelayPercentile(0), kDelayMs); 62 EXPECT_EQ(metric_recorder_.NthDelayPercentile(5), kDelayMs); 63 EXPECT_EQ(metric_recorder_.NthDelayPercentile(95), kDelayMs); 64 EXPECT_EQ(metric_recorder_.NthDelayPercentile(100), kDelayMs); 65 } 66 67 TEST_F(MetricRecorderTest, VariableDelayPackets) { 68 const size_t kPayloadSizeBytes = 1200; 69 const int64_t kInterpacketGapMs = 2000; 70 const int kNumPackets = 1000; 71 72 std::vector<int64_t> delays_ms; 73 for (int i = 0; i < kNumPackets; ++i) { 74 delays_ms.push_back(static_cast<int64_t>(i + 1)); 75 } 76 // Order of packets should not matter here. 77 std::random_shuffle(delays_ms.begin(), delays_ms.end()); 78 79 int first_received_ms = delays_ms[0]; 80 int64_t last_received_ms = 0; 81 for (int i = 0; i < kNumPackets; ++i) { 82 int64_t arrival_time_ms = kInterpacketGapMs * i + delays_ms[i]; 83 last_received_ms = std::max(last_received_ms, arrival_time_ms); 84 metric_recorder_.UpdateTimeMs(arrival_time_ms); 85 metric_recorder_.PushDelayMs(delays_ms[i], arrival_time_ms); 86 metric_recorder_.PushThroughputBytes(kPayloadSizeBytes, arrival_time_ms); 87 } 88 89 size_t received_bits = kPayloadSizeBytes * 8 * kNumPackets; 90 EXPECT_NEAR(metric_recorder_.AverageBitrateKbps(0), 91 static_cast<uint32_t>(received_bits) / 92 ((last_received_ms - first_received_ms)), 93 10); 94 95 double expected_x = (kNumPackets + 1) / 2.0; 96 double expected_x2 = ((kNumPackets + 1) * (2 * kNumPackets + 1)) / 6.0; 97 double var = expected_x2 - pow(expected_x, 2.0); 98 EXPECT_NEAR(metric_recorder_.DelayStdDev(), sqrt(var), kNumPackets / 1000.0); 99 100 EXPECT_EQ(metric_recorder_.NthDelayPercentile(0), 1); 101 EXPECT_EQ(metric_recorder_.NthDelayPercentile(5), (5 * kNumPackets) / 100); 102 EXPECT_EQ(metric_recorder_.NthDelayPercentile(95), (95 * kNumPackets) / 100); 103 EXPECT_EQ(metric_recorder_.NthDelayPercentile(100), kNumPackets); 104 } 105 106 } // namespace bwe 107 } // namespace testing 108 } // namespace webrtc 109