blob: 5483ee3083ff7c3db8000af6ac045e83bceb57f8 [file] [log] [blame]
hbos6d183ac2016-08-29 07:20:33 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/stats/rtcstatsreport.h"
hbos6d183ac2016-08-29 07:20:33 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "api/stats/rtcstats.h"
14#include "rtc_base/checks.h"
15#include "rtc_base/gunit.h"
hbos6d183ac2016-08-29 07:20:33 -070016
17namespace webrtc {
18
19class RTCTestStats1 : public RTCStats {
20 public:
hbosfc5e0502016-10-06 02:06:10 -070021 WEBRTC_RTCSTATS_DECL();
22
hbos0e6758d2016-08-31 07:57:36 -070023 RTCTestStats1(const std::string& id, int64_t timestamp_us)
24 : RTCStats(id, timestamp_us),
hbos6d183ac2016-08-29 07:20:33 -070025 integer("integer") {}
26
hbos6d183ac2016-08-29 07:20:33 -070027 RTCStatsMember<int32_t> integer;
28};
29
hbosfc5e0502016-10-06 02:06:10 -070030WEBRTC_RTCSTATS_IMPL(RTCTestStats1, RTCStats, "test-stats-1",
31 &integer);
hbos6d183ac2016-08-29 07:20:33 -070032
33class RTCTestStats2 : public RTCStats {
34 public:
hbosfc5e0502016-10-06 02:06:10 -070035 WEBRTC_RTCSTATS_DECL();
36
hbos0e6758d2016-08-31 07:57:36 -070037 RTCTestStats2(const std::string& id, int64_t timestamp_us)
38 : RTCStats(id, timestamp_us),
hbos6d183ac2016-08-29 07:20:33 -070039 number("number") {}
40
hbos6d183ac2016-08-29 07:20:33 -070041 RTCStatsMember<double> number;
42};
43
hbosfc5e0502016-10-06 02:06:10 -070044WEBRTC_RTCSTATS_IMPL(RTCTestStats2, RTCStats, "test-stats-2",
45 &number);
hbos6d183ac2016-08-29 07:20:33 -070046
47class RTCTestStats3 : public RTCStats {
48 public:
hbosfc5e0502016-10-06 02:06:10 -070049 WEBRTC_RTCSTATS_DECL();
50
hbos0e6758d2016-08-31 07:57:36 -070051 RTCTestStats3(const std::string& id, int64_t timestamp_us)
52 : RTCStats(id, timestamp_us),
hbos6d183ac2016-08-29 07:20:33 -070053 string("string") {}
54
hbos6d183ac2016-08-29 07:20:33 -070055 RTCStatsMember<std::string> string;
56};
57
hbosfc5e0502016-10-06 02:06:10 -070058WEBRTC_RTCSTATS_IMPL(RTCTestStats3, RTCStats, "test-stats-3",
59 &string);
hbos6d183ac2016-08-29 07:20:33 -070060
61TEST(RTCStatsReport, AddAndGetStats) {
hbos6ded1902016-11-01 01:50:46 -070062 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(1337);
63 EXPECT_EQ(report->timestamp_us(), 1337u);
hbos6d183ac2016-08-29 07:20:33 -070064 EXPECT_EQ(report->size(), static_cast<size_t>(0));
hbos0e6758d2016-08-31 07:57:36 -070065 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)));
hbos6d183ac2016-08-29 07:20:33 -070071 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));
hbos0e6758d2016-08-31 07:57:36 -070079 int64_t mask = 0;
hbos6d183ac2016-08-29 07:20:33 -070080 for (const RTCTestStats1* stats : a)
hbos0e6758d2016-08-31 07:57:36 -070081 mask |= stats->timestamp_us();
82 EXPECT_EQ(mask, static_cast<int64_t>(1 | 2 | 16));
hbos6d183ac2016-08-29 07:20:33 -070083
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)
hbos0e6758d2016-08-31 07:57:36 -070088 mask |= stats->timestamp_us();
89 EXPECT_EQ(mask, static_cast<int64_t>(4 | 8 | 32));
hbos6d183ac2016-08-29 07:20:33 -070090
91 EXPECT_EQ(report->GetStatsOfType<RTCTestStats3>().size(),
92 static_cast<size_t>(0));
93}
94
95TEST(RTCStatsReport, StatsOrder) {
hbos6ded1902016-11-01 01:50:46 -070096 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(1337);
97 EXPECT_EQ(report->timestamp_us(), 1337u);
hbos0e6758d2016-08-31 07:57:36 -070098 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;
hbos6d183ac2016-08-29 07:20:33 -0700106 for (const RTCStats& stats : *report) {
hbos0e6758d2016-08-31 07:57:36 -0700107 EXPECT_EQ(stats.timestamp_us(), i);
hbos6d183ac2016-08-29 07:20:33 -0700108 ++i;
109 }
hbos0e6758d2016-08-31 07:57:36 -0700110 EXPECT_EQ(i, static_cast<int64_t>(7));
hbos6d183ac2016-08-29 07:20:33 -0700111}
112
113TEST(RTCStatsReport, TakeMembersFrom) {
hbos6ded1902016-11-01 01:50:46 -0700114 rtc::scoped_refptr<RTCStatsReport> a = RTCStatsReport::Create(1337);
115 EXPECT_EQ(a->timestamp_us(), 1337u);
hbos0e6758d2016-08-31 07:57:36 -0700116 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)));
hbos6ded1902016-11-01 01:50:46 -0700119 rtc::scoped_refptr<RTCStatsReport> b = RTCStatsReport::Create(1338);
120 EXPECT_EQ(b->timestamp_us(), 1338u);
hbos0e6758d2016-08-31 07:57:36 -0700121 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)));
hbos6d183ac2016-08-29 07:20:33 -0700124
125 a->TakeMembersFrom(b);
126 EXPECT_EQ(b->size(), static_cast<size_t>(0));
hbos0e6758d2016-08-31 07:57:36 -0700127 int64_t i = 0;
hbos6d183ac2016-08-29 07:20:33 -0700128 for (const RTCStats& stats : *a) {
hbos0e6758d2016-08-31 07:57:36 -0700129 EXPECT_EQ(stats.timestamp_us(), i);
hbos6d183ac2016-08-29 07:20:33 -0700130 ++i;
131 }
hbos0e6758d2016-08-31 07:57:36 -0700132 EXPECT_EQ(i, static_cast<int64_t>(6));
hbos6d183ac2016-08-29 07:20:33 -0700133}
134
135} // namespace webrtc