blob: f3d232ff074ecfba5f9202d285d26503b6180c1d [file] [log] [blame]
hbosd565b732016-08-30 14:04:35 -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
11#include "webrtc/stats/rtcstatscollector.h"
12
13#include <memory>
14#include <string>
15#include <vector>
16
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/rtcstats_objects.h"
19#include "webrtc/api/rtcstatsreport.h"
20#include "webrtc/api/test/mock_datachannel.h"
21#include "webrtc/api/test/mock_peerconnection.h"
22#include "webrtc/api/test/mock_webrtcsession.h"
23#include "webrtc/base/checks.h"
hbos0e6758d2016-08-31 07:57:36 -070024#include "webrtc/base/fakeclock.h"
hbosd565b732016-08-30 14:04:35 -070025#include "webrtc/base/gunit.h"
26#include "webrtc/base/logging.h"
hbosc82f2e12016-09-05 01:36:50 -070027#include "webrtc/base/thread_checker.h"
hbos0e6758d2016-08-31 07:57:36 -070028#include "webrtc/base/timedelta.h"
29#include "webrtc/base/timeutils.h"
hbosd565b732016-08-30 14:04:35 -070030#include "webrtc/media/base/fakemediaengine.h"
31
32using testing::Return;
33using testing::ReturnRef;
34
35namespace webrtc {
36
hbosc82f2e12016-09-05 01:36:50 -070037namespace {
38
39const int64_t kGetStatsReportTimeoutMs = 1000;
40
41class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver {
hbosd565b732016-08-30 14:04:35 -070042 public:
hbosc82f2e12016-09-05 01:36:50 -070043 RTCStatsCollectorTestHelper()
hbosd565b732016-08-30 14:04:35 -070044 : worker_thread_(rtc::Thread::Current()),
45 network_thread_(rtc::Thread::Current()),
46 channel_manager_(new cricket::ChannelManager(
47 new cricket::FakeMediaEngine(),
48 worker_thread_,
49 network_thread_)),
50 media_controller_(
51 MediaControllerInterface::Create(cricket::MediaConfig(),
52 worker_thread_,
53 channel_manager_.get())),
54 session_(media_controller_.get()),
55 pc_() {
56 EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_));
57 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly(
58 ReturnRef(data_channels_));
59 }
60
61 MockWebRtcSession& session() { return session_; }
62 MockPeerConnection& pc() { return pc_; }
63 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() {
64 return data_channels_;
65 }
66
67 // SetSessionDescriptionObserver overrides.
68 void OnSuccess() override {}
69 void OnFailure(const std::string& error) override {
70 RTC_NOTREACHED() << error;
71 }
72
73 private:
74 rtc::Thread* const worker_thread_;
75 rtc::Thread* const network_thread_;
76 std::unique_ptr<cricket::ChannelManager> channel_manager_;
77 std::unique_ptr<webrtc::MediaControllerInterface> media_controller_;
78 MockWebRtcSession session_;
79 MockPeerConnection pc_;
80
81 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_;
82};
83
hbosc82f2e12016-09-05 01:36:50 -070084class RTCTestStats : public RTCStats {
hbosd565b732016-08-30 14:04:35 -070085 public:
hbosc82f2e12016-09-05 01:36:50 -070086 RTCTestStats(const std::string& id, int64_t timestamp_us)
87 : RTCStats(id, timestamp_us),
88 dummy_stat("dummyStat") {}
89
90 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats,
91 &dummy_stat);
92
93 RTCStatsMember<int32_t> dummy_stat;
94};
95
96const char RTCTestStats::kType[] = "test-stats";
97
98// Overrides the stats collection to verify thread usage and that the resulting
99// partial reports are merged.
100class FakeRTCStatsCollector : public RTCStatsCollector,
101 public RTCStatsCollectorCallback {
102 public:
103 static rtc::scoped_refptr<FakeRTCStatsCollector> Create(
104 PeerConnection* pc,
105 int64_t cache_lifetime_us) {
106 return rtc::scoped_refptr<FakeRTCStatsCollector>(
107 new rtc::RefCountedObject<FakeRTCStatsCollector>(
108 pc, cache_lifetime_us));
109 }
110
111 // RTCStatsCollectorCallback implementation.
112 void OnStatsDelivered(
113 const rtc::scoped_refptr<const RTCStatsReport>& report) override {
114 EXPECT_TRUE(signaling_thread_->IsCurrent());
115 rtc::CritScope cs(&lock_);
116 delivered_report_ = report;
117 }
118
119 void VerifyThreadUsageAndResultsMerging() {
120 GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback>(this));
121 EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs);
122 }
123
124 bool HasVerifiedResults() {
125 EXPECT_TRUE(signaling_thread_->IsCurrent());
126 rtc::CritScope cs(&lock_);
127 if (!delivered_report_)
128 return false;
129 EXPECT_EQ(produced_on_signaling_thread_, 1);
130 EXPECT_EQ(produced_on_worker_thread_, 1);
131 EXPECT_EQ(produced_on_network_thread_, 1);
132
133 EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats"));
134 EXPECT_TRUE(delivered_report_->Get("WorkerThreadStats"));
135 EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats"));
136
137 produced_on_signaling_thread_ = 0;
138 produced_on_worker_thread_ = 0;
139 produced_on_network_thread_ = 0;
140 delivered_report_ = nullptr;
141 return true;
hbosd565b732016-08-30 14:04:35 -0700142 }
143
144 protected:
hbosc82f2e12016-09-05 01:36:50 -0700145 FakeRTCStatsCollector(
146 PeerConnection* pc,
147 int64_t cache_lifetime)
148 : RTCStatsCollector(pc, cache_lifetime),
149 signaling_thread_(pc->session()->signaling_thread()),
150 worker_thread_(pc->session()->worker_thread()),
151 network_thread_(pc->session()->network_thread()) {
152 }
153
154 void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override {
155 EXPECT_TRUE(signaling_thread_->IsCurrent());
156 {
157 rtc::CritScope cs(&lock_);
158 EXPECT_FALSE(delivered_report_);
159 ++produced_on_signaling_thread_;
160 }
161
162 rtc::scoped_refptr<RTCStatsReport> signaling_report =
163 RTCStatsReport::Create();
164 signaling_report->AddStats(std::unique_ptr<const RTCStats>(
165 new RTCTestStats("SignalingThreadStats", timestamp_us)));
166 AddPartialResults(signaling_report);
167 }
168 void ProducePartialResultsOnWorkerThread(int64_t timestamp_us) override {
169 EXPECT_TRUE(worker_thread_->IsCurrent());
170 {
171 rtc::CritScope cs(&lock_);
172 EXPECT_FALSE(delivered_report_);
173 ++produced_on_worker_thread_;
174 }
175
176 rtc::scoped_refptr<RTCStatsReport> worker_report = RTCStatsReport::Create();
177 worker_report->AddStats(std::unique_ptr<const RTCStats>(
178 new RTCTestStats("WorkerThreadStats", timestamp_us)));
179 AddPartialResults(worker_report);
180 }
181 void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override {
182 EXPECT_TRUE(network_thread_->IsCurrent());
183 {
184 rtc::CritScope cs(&lock_);
185 EXPECT_FALSE(delivered_report_);
186 ++produced_on_network_thread_;
187 }
188
189 rtc::scoped_refptr<RTCStatsReport> network_report =
190 RTCStatsReport::Create();
191 network_report->AddStats(std::unique_ptr<const RTCStats>(
192 new RTCTestStats("NetworkThreadStats", timestamp_us)));
193 AddPartialResults(network_report);
194 }
195
196 private:
197 rtc::Thread* const signaling_thread_;
198 rtc::Thread* const worker_thread_;
199 rtc::Thread* const network_thread_;
200
201 rtc::CriticalSection lock_;
202 rtc::scoped_refptr<const RTCStatsReport> delivered_report_;
203 int produced_on_signaling_thread_ = 0;
204 int produced_on_worker_thread_ = 0;
205 int produced_on_network_thread_ = 0;
hbosd565b732016-08-30 14:04:35 -0700206};
207
hbosc82f2e12016-09-05 01:36:50 -0700208class StatsCallback : public RTCStatsCollectorCallback {
209 public:
210 static rtc::scoped_refptr<StatsCallback> Create(
211 rtc::scoped_refptr<const RTCStatsReport>* report_ptr = nullptr) {
212 return rtc::scoped_refptr<StatsCallback>(
213 new rtc::RefCountedObject<StatsCallback>(report_ptr));
214 }
215
216 void OnStatsDelivered(
217 const rtc::scoped_refptr<const RTCStatsReport>& report) override {
218 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
219 report_ = report;
220 if (report_ptr_)
221 *report_ptr_ = report_;
222 }
223
224 rtc::scoped_refptr<const RTCStatsReport> report() const {
225 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
226 return report_;
227 }
228
229 protected:
230 explicit StatsCallback(rtc::scoped_refptr<const RTCStatsReport>* report_ptr)
231 : report_ptr_(report_ptr) {}
232
233 private:
234 rtc::ThreadChecker thread_checker_;
235 rtc::scoped_refptr<const RTCStatsReport> report_;
236 rtc::scoped_refptr<const RTCStatsReport>* report_ptr_;
237};
238
239class RTCStatsCollectorTest : public testing::Test {
240 public:
241 RTCStatsCollectorTest()
242 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()),
243 collector_(RTCStatsCollector::Create(
244 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) {
245 }
246
247 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() {
248 rtc::scoped_refptr<StatsCallback> callback = StatsCallback::Create();
249 collector_->GetStatsReport(callback);
250 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs);
251 return callback->report();
252 }
253
254 protected:
255 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_;
256 rtc::scoped_refptr<RTCStatsCollector> collector_;
257};
258
259TEST_F(RTCStatsCollectorTest, SingleCallback) {
260 rtc::scoped_refptr<const RTCStatsReport> result;
261 collector_->GetStatsReport(StatsCallback::Create(&result));
262 EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs);
263}
264
265TEST_F(RTCStatsCollectorTest, MultipleCallbacks) {
266 rtc::scoped_refptr<const RTCStatsReport> a;
267 rtc::scoped_refptr<const RTCStatsReport> b;
268 rtc::scoped_refptr<const RTCStatsReport> c;
269 collector_->GetStatsReport(StatsCallback::Create(&a));
270 collector_->GetStatsReport(StatsCallback::Create(&b));
271 collector_->GetStatsReport(StatsCallback::Create(&c));
272 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
273 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
274 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
275 EXPECT_EQ(a.get(), b.get());
276 EXPECT_EQ(b.get(), c.get());
277}
278
279TEST_F(RTCStatsCollectorTest, CachedStatsReports) {
hbos0e6758d2016-08-31 07:57:36 -0700280 rtc::ScopedFakeClock fake_clock;
hbosd565b732016-08-30 14:04:35 -0700281 // Caching should ensure |a| and |b| are the same report.
hbosc82f2e12016-09-05 01:36:50 -0700282 rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport();
283 rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700284 EXPECT_EQ(a.get(), b.get());
285 // Invalidate cache by clearing it.
hbosc82f2e12016-09-05 01:36:50 -0700286 collector_->ClearCachedStatsReport();
287 rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700288 EXPECT_NE(b.get(), c.get());
289 // Invalidate cache by advancing time.
hbos0e6758d2016-08-31 07:57:36 -0700290 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
hbosc82f2e12016-09-05 01:36:50 -0700291 rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700292 EXPECT_TRUE(d);
293 EXPECT_NE(c.get(), d.get());
294}
295
hbosc82f2e12016-09-05 01:36:50 -0700296TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) {
297 rtc::ScopedFakeClock fake_clock;
298 rtc::scoped_refptr<const RTCStatsReport> a;
299 rtc::scoped_refptr<const RTCStatsReport> b;
300 rtc::scoped_refptr<const RTCStatsReport> c;
301 collector_->GetStatsReport(StatsCallback::Create(&a));
302 collector_->GetStatsReport(StatsCallback::Create(&b));
303 // Cache is invalidated after 50 ms.
304 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
305 collector_->GetStatsReport(StatsCallback::Create(&c));
306 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
307 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
308 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
309 EXPECT_EQ(a.get(), b.get());
310 // The act of doing |AdvanceTime| processes all messages. If this was not the
311 // case we might not require |c| to be fresher than |b|.
312 EXPECT_NE(c.get(), b.get());
313}
314
hbosd565b732016-08-30 14:04:35 -0700315TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) {
nissecdf37a92016-09-13 23:41:47 -0700316 int64_t before = rtc::TimeUTCMicros();
hbosc82f2e12016-09-05 01:36:50 -0700317 rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport();
nissecdf37a92016-09-13 23:41:47 -0700318 int64_t after = rtc::TimeUTCMicros();
hbosd565b732016-08-30 14:04:35 -0700319 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(),
320 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats.";
321 const RTCStats* stats = report->Get("RTCPeerConnection");
322 EXPECT_TRUE(stats);
hbos0e6758d2016-08-31 07:57:36 -0700323 EXPECT_LE(before, stats->timestamp_us());
324 EXPECT_LE(stats->timestamp_us(), after);
hbosd565b732016-08-30 14:04:35 -0700325 {
326 // Expected stats with no data channels
327 const RTCPeerConnectionStats& pcstats =
328 stats->cast_to<RTCPeerConnectionStats>();
329 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(0));
330 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(0));
331 }
332
333 test_->data_channels().push_back(
334 new MockDataChannel(DataChannelInterface::kConnecting));
335 test_->data_channels().push_back(
336 new MockDataChannel(DataChannelInterface::kOpen));
337 test_->data_channels().push_back(
338 new MockDataChannel(DataChannelInterface::kClosing));
339 test_->data_channels().push_back(
340 new MockDataChannel(DataChannelInterface::kClosed));
341
hbosc82f2e12016-09-05 01:36:50 -0700342 collector_->ClearCachedStatsReport();
343 report = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700344 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(),
345 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats.";
346 stats = report->Get("RTCPeerConnection");
347 EXPECT_TRUE(stats);
348 {
349 // Expected stats with the above four data channels
350 // TODO(hbos): When the |RTCPeerConnectionStats| is the number of data
351 // channels that have been opened and closed, not the numbers currently
352 // open/closed, we would expect opened >= closed and (opened - closed) to be
353 // the number currently open. crbug.com/636818.
354 const RTCPeerConnectionStats& pcstats =
355 stats->cast_to<RTCPeerConnectionStats>();
356 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(1));
357 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(3));
358 }
359}
360
hbosc82f2e12016-09-05 01:36:50 -0700361class RTCStatsCollectorTestWithFakeCollector : public testing::Test {
362 public:
363 RTCStatsCollectorTestWithFakeCollector()
364 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()),
365 collector_(FakeRTCStatsCollector::Create(
366 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) {
367 }
368
369 protected:
370 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_;
371 rtc::scoped_refptr<FakeRTCStatsCollector> collector_;
372};
373
374TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) {
375 collector_->VerifyThreadUsageAndResultsMerging();
376}
377
378} // namespace
379
hbosd565b732016-08-30 14:04:35 -0700380} // namespace webrtc