hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/stats/rtcstatsreport.h" |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "api/stats/rtcstats.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/gunit.h" |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | class RTCTestStats1 : public RTCStats { |
| 20 | public: |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 21 | WEBRTC_RTCSTATS_DECL(); |
| 22 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 23 | RTCTestStats1(const std::string& id, int64_t timestamp_us) |
| 24 | : RTCStats(id, timestamp_us), |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 25 | integer("integer") {} |
| 26 | |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 27 | RTCStatsMember<int32_t> integer; |
| 28 | }; |
| 29 | |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 30 | WEBRTC_RTCSTATS_IMPL(RTCTestStats1, RTCStats, "test-stats-1", |
| 31 | &integer); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 32 | |
| 33 | class RTCTestStats2 : public RTCStats { |
| 34 | public: |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 35 | WEBRTC_RTCSTATS_DECL(); |
| 36 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 37 | RTCTestStats2(const std::string& id, int64_t timestamp_us) |
| 38 | : RTCStats(id, timestamp_us), |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 39 | number("number") {} |
| 40 | |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 41 | RTCStatsMember<double> number; |
| 42 | }; |
| 43 | |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 44 | WEBRTC_RTCSTATS_IMPL(RTCTestStats2, RTCStats, "test-stats-2", |
| 45 | &number); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 46 | |
| 47 | class RTCTestStats3 : public RTCStats { |
| 48 | public: |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 49 | WEBRTC_RTCSTATS_DECL(); |
| 50 | |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 51 | RTCTestStats3(const std::string& id, int64_t timestamp_us) |
| 52 | : RTCStats(id, timestamp_us), |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 53 | string("string") {} |
| 54 | |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 55 | RTCStatsMember<std::string> string; |
| 56 | }; |
| 57 | |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 58 | WEBRTC_RTCSTATS_IMPL(RTCTestStats3, RTCStats, "test-stats-3", |
| 59 | &string); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 60 | |
| 61 | TEST(RTCStatsReport, AddAndGetStats) { |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 62 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(1337); |
| 63 | EXPECT_EQ(report->timestamp_us(), 1337u); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 64 | EXPECT_EQ(report->size(), static_cast<size_t>(0)); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 65 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a0", 1))); |
| 66 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a1", 2))); |
| 67 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b0", 4))); |
| 68 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b1", 8))); |
| 69 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a2", 16))); |
| 70 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b2", 32))); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 71 | EXPECT_EQ(report->size(), static_cast<size_t>(6)); |
| 72 | |
| 73 | EXPECT_EQ(report->Get("missing"), nullptr); |
| 74 | EXPECT_EQ(report->Get("a0")->id(), "a0"); |
| 75 | EXPECT_EQ(report->Get("b2")->id(), "b2"); |
| 76 | |
| 77 | std::vector<const RTCTestStats1*> a = report->GetStatsOfType<RTCTestStats1>(); |
| 78 | EXPECT_EQ(a.size(), static_cast<size_t>(3)); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 79 | int64_t mask = 0; |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 80 | for (const RTCTestStats1* stats : a) |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 81 | mask |= stats->timestamp_us(); |
| 82 | EXPECT_EQ(mask, static_cast<int64_t>(1 | 2 | 16)); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 83 | |
| 84 | std::vector<const RTCTestStats2*> b = report->GetStatsOfType<RTCTestStats2>(); |
| 85 | EXPECT_EQ(b.size(), static_cast<size_t>(3)); |
| 86 | mask = 0; |
| 87 | for (const RTCTestStats2* stats : b) |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 88 | mask |= stats->timestamp_us(); |
| 89 | EXPECT_EQ(mask, static_cast<int64_t>(4 | 8 | 32)); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 90 | |
| 91 | EXPECT_EQ(report->GetStatsOfType<RTCTestStats3>().size(), |
| 92 | static_cast<size_t>(0)); |
| 93 | } |
| 94 | |
| 95 | TEST(RTCStatsReport, StatsOrder) { |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 96 | rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(1337); |
| 97 | EXPECT_EQ(report->timestamp_us(), 1337u); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 98 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2))); |
| 99 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3))); |
| 100 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("B", 1))); |
| 101 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("A", 0))); |
| 102 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("E", 4))); |
| 103 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("F", 5))); |
| 104 | report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("G", 6))); |
| 105 | int64_t i = 0; |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 106 | for (const RTCStats& stats : *report) { |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 107 | EXPECT_EQ(stats.timestamp_us(), i); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 108 | ++i; |
| 109 | } |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 110 | EXPECT_EQ(i, static_cast<int64_t>(7)); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST(RTCStatsReport, TakeMembersFrom) { |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 114 | rtc::scoped_refptr<RTCStatsReport> a = RTCStatsReport::Create(1337); |
| 115 | EXPECT_EQ(a->timestamp_us(), 1337u); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 116 | a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("B", 1))); |
| 117 | a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2))); |
| 118 | a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("E", 4))); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 119 | rtc::scoped_refptr<RTCStatsReport> b = RTCStatsReport::Create(1338); |
| 120 | EXPECT_EQ(b->timestamp_us(), 1338u); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 121 | b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("A", 0))); |
| 122 | b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3))); |
| 123 | b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("F", 5))); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 124 | |
| 125 | a->TakeMembersFrom(b); |
| 126 | EXPECT_EQ(b->size(), static_cast<size_t>(0)); |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 127 | int64_t i = 0; |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 128 | for (const RTCStats& stats : *a) { |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 129 | EXPECT_EQ(stats.timestamp_us(), i); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 130 | ++i; |
| 131 | } |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 132 | EXPECT_EQ(i, static_cast<int64_t>(6)); |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace webrtc |