OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/rtcstatsreport.h" | 11 #include "webrtc/api/rtcstatsreport.h" |
12 | 12 |
13 #include "webrtc/api/rtcstats.h" | 13 #include "webrtc/api/rtcstats.h" |
14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
15 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
16 | 16 |
17 namespace webrtc { | 17 namespace webrtc { |
18 | 18 |
19 class RTCTestStats1 : public RTCStats { | 19 class RTCTestStats1 : public RTCStats { |
20 public: | 20 public: |
21 RTCTestStats1(const std::string& id, double timestamp) | 21 RTCTestStats1(const std::string& id, int64_t timestamp_us) |
22 : RTCStats(id, timestamp), | 22 : RTCStats(id, timestamp_us), |
23 integer("integer") {} | 23 integer("integer") {} |
24 | 24 |
25 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats1, | 25 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats1, |
26 &integer); | 26 &integer); |
27 | 27 |
28 RTCStatsMember<int32_t> integer; | 28 RTCStatsMember<int32_t> integer; |
29 }; | 29 }; |
30 | 30 |
31 const char RTCTestStats1::kType[] = "test-stats-1"; | 31 const char RTCTestStats1::kType[] = "test-stats-1"; |
32 | 32 |
33 class RTCTestStats2 : public RTCStats { | 33 class RTCTestStats2 : public RTCStats { |
34 public: | 34 public: |
35 RTCTestStats2(const std::string& id, double timestamp) | 35 RTCTestStats2(const std::string& id, int64_t timestamp_us) |
36 : RTCStats(id, timestamp), | 36 : RTCStats(id, timestamp_us), |
37 number("number") {} | 37 number("number") {} |
38 | 38 |
39 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats2, | 39 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats2, |
40 &number); | 40 &number); |
41 | 41 |
42 RTCStatsMember<double> number; | 42 RTCStatsMember<double> number; |
43 }; | 43 }; |
44 | 44 |
45 const char RTCTestStats2::kType[] = "test-stats-2"; | 45 const char RTCTestStats2::kType[] = "test-stats-2"; |
46 | 46 |
47 class RTCTestStats3 : public RTCStats { | 47 class RTCTestStats3 : public RTCStats { |
48 public: | 48 public: |
49 RTCTestStats3(const std::string& id, double timestamp) | 49 RTCTestStats3(const std::string& id, int64_t timestamp_us) |
50 : RTCStats(id, timestamp), | 50 : RTCStats(id, timestamp_us), |
51 string("string") {} | 51 string("string") {} |
52 | 52 |
53 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats3, | 53 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats3, |
54 &string); | 54 &string); |
55 | 55 |
56 RTCStatsMember<std::string> string; | 56 RTCStatsMember<std::string> string; |
57 }; | 57 }; |
58 | 58 |
59 const char RTCTestStats3::kType[] = "test-stats-3"; | 59 const char RTCTestStats3::kType[] = "test-stats-3"; |
60 | 60 |
61 TEST(RTCStatsReport, AddAndGetStats) { | 61 TEST(RTCStatsReport, AddAndGetStats) { |
62 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); | 62 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
63 EXPECT_EQ(report->size(), static_cast<size_t>(0)); | 63 EXPECT_EQ(report->size(), static_cast<size_t>(0)); |
64 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a0", 1.0))); | 64 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a0", 1))); |
65 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a1", 2.0))); | 65 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a1", 2))); |
66 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b0", 4.0))); | 66 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b0", 4))); |
67 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b1", 8.0))); | 67 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b1", 8))); |
68 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a2", 16.0))); | 68 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("a2", 16))); |
69 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b2", 32.0))); | 69 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("b2", 32))); |
70 EXPECT_EQ(report->size(), static_cast<size_t>(6)); | 70 EXPECT_EQ(report->size(), static_cast<size_t>(6)); |
71 | 71 |
72 EXPECT_EQ(report->Get("missing"), nullptr); | 72 EXPECT_EQ(report->Get("missing"), nullptr); |
73 EXPECT_EQ(report->Get("a0")->id(), "a0"); | 73 EXPECT_EQ(report->Get("a0")->id(), "a0"); |
74 EXPECT_EQ(report->Get("b2")->id(), "b2"); | 74 EXPECT_EQ(report->Get("b2")->id(), "b2"); |
75 | 75 |
76 std::vector<const RTCTestStats1*> a = report->GetStatsOfType<RTCTestStats1>(); | 76 std::vector<const RTCTestStats1*> a = report->GetStatsOfType<RTCTestStats1>(); |
77 EXPECT_EQ(a.size(), static_cast<size_t>(3)); | 77 EXPECT_EQ(a.size(), static_cast<size_t>(3)); |
78 uint32_t mask = 0; | 78 int64_t mask = 0; |
79 for (const RTCTestStats1* stats : a) | 79 for (const RTCTestStats1* stats : a) |
80 mask |= static_cast<uint32_t>(stats->timestamp()); | 80 mask |= stats->timestamp_us(); |
81 EXPECT_EQ(mask, static_cast<uint32_t>(1 | 2 | 16)); | 81 EXPECT_EQ(mask, static_cast<int64_t>(1 | 2 | 16)); |
82 | 82 |
83 std::vector<const RTCTestStats2*> b = report->GetStatsOfType<RTCTestStats2>(); | 83 std::vector<const RTCTestStats2*> b = report->GetStatsOfType<RTCTestStats2>(); |
84 EXPECT_EQ(b.size(), static_cast<size_t>(3)); | 84 EXPECT_EQ(b.size(), static_cast<size_t>(3)); |
85 mask = 0; | 85 mask = 0; |
86 for (const RTCTestStats2* stats : b) | 86 for (const RTCTestStats2* stats : b) |
87 mask |= static_cast<uint32_t>(stats->timestamp()); | 87 mask |= stats->timestamp_us(); |
88 EXPECT_EQ(mask, static_cast<uint32_t>(4 | 8 | 32)); | 88 EXPECT_EQ(mask, static_cast<int64_t>(4 | 8 | 32)); |
89 | 89 |
90 EXPECT_EQ(report->GetStatsOfType<RTCTestStats3>().size(), | 90 EXPECT_EQ(report->GetStatsOfType<RTCTestStats3>().size(), |
91 static_cast<size_t>(0)); | 91 static_cast<size_t>(0)); |
92 } | 92 } |
93 | 93 |
94 TEST(RTCStatsReport, StatsOrder) { | 94 TEST(RTCStatsReport, StatsOrder) { |
95 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); | 95 rtc::scoped_refptr<RTCStatsReport> report = RTCStatsReport::Create(); |
96 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2.0))); | 96 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2))); |
97 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3.0))); | 97 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3))); |
98 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("B", 1.0))); | 98 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("B", 1))); |
99 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("A", 0.0))); | 99 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("A", 0))); |
100 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("E", 4.0))); | 100 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("E", 4))); |
101 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("F", 5.0))); | 101 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("F", 5))); |
102 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("G", 6.0))); | 102 report->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats2("G", 6))); |
103 size_t i = 0; | 103 int64_t i = 0; |
104 for (const RTCStats& stats : *report) { | 104 for (const RTCStats& stats : *report) { |
105 EXPECT_EQ(static_cast<size_t>(stats.timestamp()), i); | 105 EXPECT_EQ(stats.timestamp_us(), i); |
106 ++i; | 106 ++i; |
107 } | 107 } |
108 EXPECT_EQ(i, static_cast<size_t>(7)); | 108 EXPECT_EQ(i, static_cast<int64_t>(7)); |
109 } | 109 } |
110 | 110 |
111 TEST(RTCStatsReport, TakeMembersFrom) { | 111 TEST(RTCStatsReport, TakeMembersFrom) { |
112 rtc::scoped_refptr<RTCStatsReport> a = RTCStatsReport::Create(); | 112 rtc::scoped_refptr<RTCStatsReport> a = RTCStatsReport::Create(); |
113 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("B", 1.0))); | 113 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("B", 1))); |
114 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2.0))); | 114 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("C", 2))); |
115 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("E", 4.0))); | 115 a->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("E", 4))); |
116 rtc::scoped_refptr<RTCStatsReport> b = RTCStatsReport::Create(); | 116 rtc::scoped_refptr<RTCStatsReport> b = RTCStatsReport::Create(); |
117 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("A", 0.0))); | 117 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("A", 0))); |
118 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3.0))); | 118 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("D", 3))); |
119 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("F", 5.0))); | 119 b->AddStats(std::unique_ptr<RTCStats>(new RTCTestStats1("F", 5))); |
120 | 120 |
121 a->TakeMembersFrom(b); | 121 a->TakeMembersFrom(b); |
122 EXPECT_EQ(b->size(), static_cast<size_t>(0)); | 122 EXPECT_EQ(b->size(), static_cast<size_t>(0)); |
123 size_t i = 0; | 123 int64_t i = 0; |
124 for (const RTCStats& stats : *a) { | 124 for (const RTCStats& stats : *a) { |
125 EXPECT_EQ(static_cast<size_t>(stats.timestamp()), i); | 125 EXPECT_EQ(stats.timestamp_us(), i); |
126 ++i; | 126 ++i; |
127 } | 127 } |
128 EXPECT_EQ(i, static_cast<size_t>(6)); | 128 EXPECT_EQ(i, static_cast<int64_t>(6)); |
129 } | 129 } |
130 | 130 |
131 } // namespace webrtc | 131 } // namespace webrtc |
OLD | NEW |