blob: 446990c574129238a24798453f872d8f234005e3 [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
hbos74e1a4f2016-09-15 23:33:01 -070011#include "webrtc/api/rtcstatscollector.h"
hbosd565b732016-08-30 14:04:35 -070012
13#include <memory>
14#include <string>
15#include <vector>
16
17#include "webrtc/api/jsepsessiondescription.h"
hbos74e1a4f2016-09-15 23:33:01 -070018#include "webrtc/api/stats/rtcstats_objects.h"
19#include "webrtc/api/stats/rtcstatsreport.h"
hbosd565b732016-08-30 14:04:35 -070020#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
hbosfdafab82016-09-14 06:02:13 -070061 rtc::ScopedFakeClock& fake_clock() { return fake_clock_; }
hbosd565b732016-08-30 14:04:35 -070062 MockWebRtcSession& session() { return session_; }
63 MockPeerConnection& pc() { return pc_; }
64 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() {
65 return data_channels_;
66 }
67
68 // SetSessionDescriptionObserver overrides.
69 void OnSuccess() override {}
70 void OnFailure(const std::string& error) override {
71 RTC_NOTREACHED() << error;
72 }
73
74 private:
hbosfdafab82016-09-14 06:02:13 -070075 rtc::ScopedFakeClock fake_clock_;
hbosd565b732016-08-30 14:04:35 -070076 rtc::Thread* const worker_thread_;
77 rtc::Thread* const network_thread_;
78 std::unique_ptr<cricket::ChannelManager> channel_manager_;
79 std::unique_ptr<webrtc::MediaControllerInterface> media_controller_;
80 MockWebRtcSession session_;
81 MockPeerConnection pc_;
82
83 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_;
84};
85
hbosc82f2e12016-09-05 01:36:50 -070086class RTCTestStats : public RTCStats {
hbosd565b732016-08-30 14:04:35 -070087 public:
hbosc82f2e12016-09-05 01:36:50 -070088 RTCTestStats(const std::string& id, int64_t timestamp_us)
89 : RTCStats(id, timestamp_us),
90 dummy_stat("dummyStat") {}
91
92 WEBRTC_RTCSTATS_IMPL(RTCStats, RTCTestStats,
93 &dummy_stat);
94
95 RTCStatsMember<int32_t> dummy_stat;
96};
97
98const char RTCTestStats::kType[] = "test-stats";
99
100// Overrides the stats collection to verify thread usage and that the resulting
101// partial reports are merged.
102class FakeRTCStatsCollector : public RTCStatsCollector,
103 public RTCStatsCollectorCallback {
104 public:
105 static rtc::scoped_refptr<FakeRTCStatsCollector> Create(
106 PeerConnection* pc,
107 int64_t cache_lifetime_us) {
108 return rtc::scoped_refptr<FakeRTCStatsCollector>(
109 new rtc::RefCountedObject<FakeRTCStatsCollector>(
110 pc, cache_lifetime_us));
111 }
112
113 // RTCStatsCollectorCallback implementation.
114 void OnStatsDelivered(
115 const rtc::scoped_refptr<const RTCStatsReport>& report) override {
116 EXPECT_TRUE(signaling_thread_->IsCurrent());
117 rtc::CritScope cs(&lock_);
118 delivered_report_ = report;
119 }
120
121 void VerifyThreadUsageAndResultsMerging() {
122 GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback>(this));
123 EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs);
124 }
125
126 bool HasVerifiedResults() {
127 EXPECT_TRUE(signaling_thread_->IsCurrent());
128 rtc::CritScope cs(&lock_);
129 if (!delivered_report_)
130 return false;
131 EXPECT_EQ(produced_on_signaling_thread_, 1);
132 EXPECT_EQ(produced_on_worker_thread_, 1);
133 EXPECT_EQ(produced_on_network_thread_, 1);
134
135 EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats"));
136 EXPECT_TRUE(delivered_report_->Get("WorkerThreadStats"));
137 EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats"));
138
139 produced_on_signaling_thread_ = 0;
140 produced_on_worker_thread_ = 0;
141 produced_on_network_thread_ = 0;
142 delivered_report_ = nullptr;
143 return true;
hbosd565b732016-08-30 14:04:35 -0700144 }
145
146 protected:
hbosc82f2e12016-09-05 01:36:50 -0700147 FakeRTCStatsCollector(
148 PeerConnection* pc,
149 int64_t cache_lifetime)
150 : RTCStatsCollector(pc, cache_lifetime),
151 signaling_thread_(pc->session()->signaling_thread()),
152 worker_thread_(pc->session()->worker_thread()),
153 network_thread_(pc->session()->network_thread()) {
154 }
155
156 void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override {
157 EXPECT_TRUE(signaling_thread_->IsCurrent());
158 {
159 rtc::CritScope cs(&lock_);
160 EXPECT_FALSE(delivered_report_);
161 ++produced_on_signaling_thread_;
162 }
163
164 rtc::scoped_refptr<RTCStatsReport> signaling_report =
165 RTCStatsReport::Create();
166 signaling_report->AddStats(std::unique_ptr<const RTCStats>(
167 new RTCTestStats("SignalingThreadStats", timestamp_us)));
168 AddPartialResults(signaling_report);
169 }
170 void ProducePartialResultsOnWorkerThread(int64_t timestamp_us) override {
171 EXPECT_TRUE(worker_thread_->IsCurrent());
172 {
173 rtc::CritScope cs(&lock_);
174 EXPECT_FALSE(delivered_report_);
175 ++produced_on_worker_thread_;
176 }
177
178 rtc::scoped_refptr<RTCStatsReport> worker_report = RTCStatsReport::Create();
179 worker_report->AddStats(std::unique_ptr<const RTCStats>(
180 new RTCTestStats("WorkerThreadStats", timestamp_us)));
181 AddPartialResults(worker_report);
182 }
183 void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override {
184 EXPECT_TRUE(network_thread_->IsCurrent());
185 {
186 rtc::CritScope cs(&lock_);
187 EXPECT_FALSE(delivered_report_);
188 ++produced_on_network_thread_;
189 }
190
191 rtc::scoped_refptr<RTCStatsReport> network_report =
192 RTCStatsReport::Create();
193 network_report->AddStats(std::unique_ptr<const RTCStats>(
194 new RTCTestStats("NetworkThreadStats", timestamp_us)));
195 AddPartialResults(network_report);
196 }
197
198 private:
199 rtc::Thread* const signaling_thread_;
200 rtc::Thread* const worker_thread_;
201 rtc::Thread* const network_thread_;
202
203 rtc::CriticalSection lock_;
204 rtc::scoped_refptr<const RTCStatsReport> delivered_report_;
205 int produced_on_signaling_thread_ = 0;
206 int produced_on_worker_thread_ = 0;
207 int produced_on_network_thread_ = 0;
hbosd565b732016-08-30 14:04:35 -0700208};
209
hbosc82f2e12016-09-05 01:36:50 -0700210class StatsCallback : public RTCStatsCollectorCallback {
211 public:
212 static rtc::scoped_refptr<StatsCallback> Create(
213 rtc::scoped_refptr<const RTCStatsReport>* report_ptr = nullptr) {
214 return rtc::scoped_refptr<StatsCallback>(
215 new rtc::RefCountedObject<StatsCallback>(report_ptr));
216 }
217
218 void OnStatsDelivered(
219 const rtc::scoped_refptr<const RTCStatsReport>& report) override {
220 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
221 report_ = report;
222 if (report_ptr_)
223 *report_ptr_ = report_;
224 }
225
226 rtc::scoped_refptr<const RTCStatsReport> report() const {
227 EXPECT_TRUE(thread_checker_.CalledOnValidThread());
228 return report_;
229 }
230
231 protected:
232 explicit StatsCallback(rtc::scoped_refptr<const RTCStatsReport>* report_ptr)
233 : report_ptr_(report_ptr) {}
234
235 private:
236 rtc::ThreadChecker thread_checker_;
237 rtc::scoped_refptr<const RTCStatsReport> report_;
238 rtc::scoped_refptr<const RTCStatsReport>* report_ptr_;
239};
240
241class RTCStatsCollectorTest : public testing::Test {
242 public:
243 RTCStatsCollectorTest()
244 : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()),
245 collector_(RTCStatsCollector::Create(
246 &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) {
247 }
248
249 rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() {
250 rtc::scoped_refptr<StatsCallback> callback = StatsCallback::Create();
251 collector_->GetStatsReport(callback);
252 EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs);
253 return callback->report();
254 }
255
256 protected:
257 rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_;
258 rtc::scoped_refptr<RTCStatsCollector> collector_;
259};
260
261TEST_F(RTCStatsCollectorTest, SingleCallback) {
262 rtc::scoped_refptr<const RTCStatsReport> result;
263 collector_->GetStatsReport(StatsCallback::Create(&result));
264 EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs);
265}
266
267TEST_F(RTCStatsCollectorTest, MultipleCallbacks) {
268 rtc::scoped_refptr<const RTCStatsReport> a;
269 rtc::scoped_refptr<const RTCStatsReport> b;
270 rtc::scoped_refptr<const RTCStatsReport> c;
271 collector_->GetStatsReport(StatsCallback::Create(&a));
272 collector_->GetStatsReport(StatsCallback::Create(&b));
273 collector_->GetStatsReport(StatsCallback::Create(&c));
274 EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
275 EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
276 EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
277 EXPECT_EQ(a.get(), b.get());
278 EXPECT_EQ(b.get(), c.get());
279}
280
281TEST_F(RTCStatsCollectorTest, CachedStatsReports) {
hbosd565b732016-08-30 14:04:35 -0700282 // Caching should ensure |a| and |b| are the same report.
hbosc82f2e12016-09-05 01:36:50 -0700283 rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport();
284 rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700285 EXPECT_EQ(a.get(), b.get());
286 // Invalidate cache by clearing it.
hbosc82f2e12016-09-05 01:36:50 -0700287 collector_->ClearCachedStatsReport();
288 rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700289 EXPECT_NE(b.get(), c.get());
290 // Invalidate cache by advancing time.
hbosfdafab82016-09-14 06:02:13 -0700291 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
hbosc82f2e12016-09-05 01:36:50 -0700292 rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport();
hbosd565b732016-08-30 14:04:35 -0700293 EXPECT_TRUE(d);
294 EXPECT_NE(c.get(), d.get());
295}
296
hbosc82f2e12016-09-05 01:36:50 -0700297TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) {
hbosc82f2e12016-09-05 01:36:50 -0700298 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.
hbosfdafab82016-09-14 06:02:13 -0700304 test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51));
hbosc82f2e12016-09-05 01:36:50 -0700305 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