blob: 7148c9fc2f434812058b61ae1ac6ebb4344986c9 [file] [log] [blame]
hbos615d3012016-08-24 01:33:13 -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/rtcstats.h"
hbos615d3012016-08-24 01:33:13 -070012
ehmaldonado35a872c2017-07-28 07:29:12 -070013#include <cmath>
hbosfdafab82016-09-14 06:02:13 -070014#include <cstring>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/gunit.h"
18#include "rtc_base/json.h"
19#include "stats/test/rtcteststats.h"
hbos615d3012016-08-24 01:33:13 -070020
21namespace webrtc {
22
ehmaldonado35a872c2017-07-28 07:29:12 -070023namespace {
24
25// JSON stores numbers as floating point numbers with 53 significant bits, which
26// amounts to about 15.95 decimal digits. Thus, when comparing large numbers
27// processed by JSON, that's all the precision we should expect.
28const double JSON_EPSILON = 1e-15;
29
30// We do this since Google Test doesn't support relative error.
31// This is computed as follows:
32// If |a - b| / |a| < EPS, then |a - b| < |a| * EPS, so |a| * EPS is the
33// maximum expected error.
34double GetExpectedError(const double expected_value) {
35 return JSON_EPSILON * fabs(expected_value);
36}
37
38} // namespace
39
hbos615d3012016-08-24 01:33:13 -070040class RTCChildStats : public RTCStats {
41 public:
hbosfc5e0502016-10-06 02:06:10 -070042 WEBRTC_RTCSTATS_DECL();
43
hbos0e6758d2016-08-31 07:57:36 -070044 RTCChildStats(const std::string& id, int64_t timestamp_us)
45 : RTCStats(id, timestamp_us),
hbos615d3012016-08-24 01:33:13 -070046 child_int("childInt") {}
47
hbos615d3012016-08-24 01:33:13 -070048 RTCStatsMember<int32_t> child_int;
49};
50
hbosfc5e0502016-10-06 02:06:10 -070051WEBRTC_RTCSTATS_IMPL(RTCChildStats, RTCStats, "child-stats",
52 &child_int);
hbos615d3012016-08-24 01:33:13 -070053
54class RTCGrandChildStats : public RTCChildStats {
55 public:
hbosfc5e0502016-10-06 02:06:10 -070056 WEBRTC_RTCSTATS_DECL();
57
hbos0e6758d2016-08-31 07:57:36 -070058 RTCGrandChildStats(const std::string& id, int64_t timestamp_us)
59 : RTCChildStats(id, timestamp_us),
hbos615d3012016-08-24 01:33:13 -070060 grandchild_int("grandchildInt") {}
61
hbos615d3012016-08-24 01:33:13 -070062 RTCStatsMember<int32_t> grandchild_int;
63};
64
hbosfc5e0502016-10-06 02:06:10 -070065WEBRTC_RTCSTATS_IMPL(RTCGrandChildStats, RTCChildStats, "grandchild-stats",
66 &grandchild_int);
hbos615d3012016-08-24 01:33:13 -070067
68TEST(RTCStatsTest, RTCStatsAndMembers) {
hbos0e6758d2016-08-31 07:57:36 -070069 RTCTestStats stats("testId", 42);
hbos615d3012016-08-24 01:33:13 -070070 EXPECT_EQ(stats.id(), "testId");
hbos0e6758d2016-08-31 07:57:36 -070071 EXPECT_EQ(stats.timestamp_us(), static_cast<int64_t>(42));
hbos615d3012016-08-24 01:33:13 -070072 std::vector<const RTCStatsMemberInterface*> members = stats.Members();
hbosb20f3872016-10-04 14:37:11 -070073 EXPECT_EQ(members.size(), static_cast<size_t>(14));
hbos615d3012016-08-24 01:33:13 -070074 for (const RTCStatsMemberInterface* member : members) {
75 EXPECT_FALSE(member->is_defined());
76 }
hbosb20f3872016-10-04 14:37:11 -070077 stats.m_bool = true;
hbos615d3012016-08-24 01:33:13 -070078 stats.m_int32 = 123;
79 stats.m_uint32 = 123;
80 stats.m_int64 = 123;
81 stats.m_uint64 = 123;
82 stats.m_double = 123.0;
hbos615d3012016-08-24 01:33:13 -070083 stats.m_string = std::string("123");
hbosfdafab82016-09-14 06:02:13 -070084
hbosb20f3872016-10-04 14:37:11 -070085 std::vector<bool> sequence_bool;
86 sequence_bool.push_back(true);
hbosfdafab82016-09-14 06:02:13 -070087 std::vector<int32_t> sequence_int32;
88 sequence_int32.push_back(static_cast<int32_t>(1));
89 std::vector<uint32_t> sequence_uint32;
90 sequence_uint32.push_back(static_cast<uint32_t>(2));
91 std::vector<int64_t> sequence_int64;
92 sequence_int64.push_back(static_cast<int64_t>(3));
93 std::vector<uint64_t> sequence_uint64;
94 sequence_uint64.push_back(static_cast<uint64_t>(4));
95 std::vector<double> sequence_double;
96 sequence_double.push_back(5.0);
hbosfdafab82016-09-14 06:02:13 -070097 std::vector<std::string> sequence_string;
hbos8faf9e02016-09-15 06:52:43 -070098 sequence_string.push_back(std::string("six"));
hbosfdafab82016-09-14 06:02:13 -070099
hbosb20f3872016-10-04 14:37:11 -0700100 stats.m_sequence_bool = sequence_bool;
hbosfdafab82016-09-14 06:02:13 -0700101 stats.m_sequence_int32 = sequence_int32;
102 stats.m_sequence_uint32 = sequence_uint32;
hbos615d3012016-08-24 01:33:13 -0700103 EXPECT_FALSE(stats.m_sequence_int64.is_defined());
hbosfdafab82016-09-14 06:02:13 -0700104 stats.m_sequence_int64 = sequence_int64;
105 stats.m_sequence_uint64 = sequence_uint64;
106 stats.m_sequence_double = sequence_double;
hbosfdafab82016-09-14 06:02:13 -0700107 stats.m_sequence_string = sequence_string;
hbos615d3012016-08-24 01:33:13 -0700108 for (const RTCStatsMemberInterface* member : members) {
109 EXPECT_TRUE(member->is_defined());
110 }
hbosb20f3872016-10-04 14:37:11 -0700111 EXPECT_EQ(*stats.m_bool, true);
hbos615d3012016-08-24 01:33:13 -0700112 EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123));
113 EXPECT_EQ(*stats.m_uint32, static_cast<uint32_t>(123));
114 EXPECT_EQ(*stats.m_int64, static_cast<int64_t>(123));
115 EXPECT_EQ(*stats.m_uint64, static_cast<uint64_t>(123));
116 EXPECT_EQ(*stats.m_double, 123.0);
hbos615d3012016-08-24 01:33:13 -0700117 EXPECT_EQ(*stats.m_string, std::string("123"));
hbosb20f3872016-10-04 14:37:11 -0700118 EXPECT_EQ(*stats.m_sequence_bool, sequence_bool);
hbosfdafab82016-09-14 06:02:13 -0700119 EXPECT_EQ(*stats.m_sequence_int32, sequence_int32);
120 EXPECT_EQ(*stats.m_sequence_uint32, sequence_uint32);
121 EXPECT_EQ(*stats.m_sequence_int64, sequence_int64);
122 EXPECT_EQ(*stats.m_sequence_uint64, sequence_uint64);
123 EXPECT_EQ(*stats.m_sequence_double, sequence_double);
hbosfdafab82016-09-14 06:02:13 -0700124 EXPECT_EQ(*stats.m_sequence_string, sequence_string);
125
hbos615d3012016-08-24 01:33:13 -0700126 int32_t numbers[] = { 4, 8, 15, 16, 23, 42 };
hbosfdafab82016-09-14 06:02:13 -0700127 std::vector<int32_t> numbers_sequence(&numbers[0], &numbers[6]);
128 stats.m_sequence_int32->clear();
hbos615d3012016-08-24 01:33:13 -0700129 stats.m_sequence_int32->insert(stats.m_sequence_int32->end(),
130 numbers_sequence.begin(),
131 numbers_sequence.end());
132 EXPECT_EQ(*stats.m_sequence_int32, numbers_sequence);
133}
134
hbos67c8bc42016-10-25 04:31:23 -0700135TEST(RTCStatsTest, EqualityOperator) {
136 RTCTestStats empty_stats("testId", 123);
137 EXPECT_EQ(empty_stats, empty_stats);
138
139 RTCTestStats stats_with_all_values = empty_stats;
140 stats_with_all_values.m_bool = true;
141 stats_with_all_values.m_int32 = 123;
142 stats_with_all_values.m_uint32 = 123;
143 stats_with_all_values.m_int64 = 123;
144 stats_with_all_values.m_uint64 = 123;
145 stats_with_all_values.m_double = 123.0;
146 stats_with_all_values.m_string = "123";
147 stats_with_all_values.m_sequence_bool = std::vector<bool>();
148 stats_with_all_values.m_sequence_int32 = std::vector<int32_t>();
149 stats_with_all_values.m_sequence_uint32 = std::vector<uint32_t>();
150 stats_with_all_values.m_sequence_int64 = std::vector<int64_t>();
151 stats_with_all_values.m_sequence_uint64 = std::vector<uint64_t>();
152 stats_with_all_values.m_sequence_double = std::vector<double>();
153 stats_with_all_values.m_sequence_string = std::vector<std::string>();
154 EXPECT_NE(stats_with_all_values, empty_stats);
155 EXPECT_EQ(stats_with_all_values, stats_with_all_values);
156 EXPECT_NE(stats_with_all_values.m_int32, stats_with_all_values.m_uint32);
157
158 RTCTestStats one_member_different[] = {
159 stats_with_all_values, stats_with_all_values, stats_with_all_values,
160 stats_with_all_values, stats_with_all_values, stats_with_all_values,
161 stats_with_all_values, stats_with_all_values, stats_with_all_values,
162 stats_with_all_values, stats_with_all_values, stats_with_all_values,
163 stats_with_all_values, stats_with_all_values,
164 };
165 for (size_t i = 0; i < 14; ++i) {
166 EXPECT_EQ(stats_with_all_values, one_member_different[i]);
167 }
168 one_member_different[0].m_bool = false;
169 one_member_different[1].m_int32 = 321;
170 one_member_different[2].m_uint32 = 321;
171 one_member_different[3].m_int64 = 321;
172 one_member_different[4].m_uint64 = 321;
173 one_member_different[5].m_double = 321.0;
174 one_member_different[6].m_string = "321";
175 one_member_different[7].m_sequence_bool->push_back(false);
176 one_member_different[8].m_sequence_int32->push_back(321);
177 one_member_different[9].m_sequence_uint32->push_back(321);
178 one_member_different[10].m_sequence_int64->push_back(321);
179 one_member_different[11].m_sequence_uint64->push_back(321);
180 one_member_different[12].m_sequence_double->push_back(321.0);
181 one_member_different[13].m_sequence_string->push_back("321");
182 for (size_t i = 0; i < 14; ++i) {
183 EXPECT_NE(stats_with_all_values, one_member_different[i]);
184 }
185
186 RTCTestStats empty_stats_different_id("testId2", 123);
187 EXPECT_NE(empty_stats, empty_stats_different_id);
188 RTCTestStats empty_stats_different_timestamp("testId", 321);
hbos0583b282016-11-30 01:50:14 -0800189 EXPECT_EQ(empty_stats, empty_stats_different_timestamp);
hbos67c8bc42016-10-25 04:31:23 -0700190
191 RTCChildStats child("childId", 42);
192 RTCGrandChildStats grandchild("grandchildId", 42);
193 EXPECT_NE(child, grandchild);
hbos28747962016-11-21 09:17:41 -0800194
195 RTCChildStats stats_with_defined_member("leId", 0);
196 stats_with_defined_member.child_int = 0;
197 RTCChildStats stats_with_undefined_member("leId", 0);
198 EXPECT_NE(stats_with_defined_member, stats_with_undefined_member);
199 EXPECT_NE(stats_with_undefined_member, stats_with_defined_member);
hbos67c8bc42016-10-25 04:31:23 -0700200}
201
hbos615d3012016-08-24 01:33:13 -0700202TEST(RTCStatsTest, RTCStatsGrandChild) {
203 RTCGrandChildStats stats("grandchild", 0.0);
204 stats.child_int = 1;
205 stats.grandchild_int = 2;
206 int32_t sum = 0;
207 for (const RTCStatsMemberInterface* member : stats.Members()) {
208 sum += *member->cast_to<const RTCStatsMember<int32_t>>();
209 }
210 EXPECT_EQ(sum, static_cast<int32_t>(3));
211
212 std::unique_ptr<RTCStats> copy_ptr = stats.copy();
213 const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>();
214 EXPECT_EQ(*copy.child_int, *stats.child_int);
215 EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int);
216}
217
ehmaldonado35a872c2017-07-28 07:29:12 -0700218TEST(RTCStatsTest, RTCStatsPrintsValidJson) {
219 std::string id = "statsId";
220 int timestamp = 42;
221 bool m_bool = true;
222 int m_int32 = 123;
223 int64_t m_int64 = 1234567890123456499L;
224 double m_double = 123.4567890123456499;
225 std::string m_string = "123";
226
227 std::vector<bool> sequence_bool;
228 std::vector<int32_t> sequence_int32;
229 sequence_int32.push_back(static_cast<int32_t>(1));
230 std::vector<int64_t> sequence_int64;
231 sequence_int64.push_back(static_cast<int64_t>(-1234567890123456499L));
232 sequence_int64.push_back(static_cast<int64_t>(1));
233 sequence_int64.push_back(static_cast<int64_t>(1234567890123456499L));
234 std::vector<double> sequence_double;
235 sequence_double.push_back(123.4567890123456499);
236 sequence_double.push_back(1234567890123.456499);
237 std::vector<std::string> sequence_string;
238 sequence_string.push_back(std::string("four"));
239
240 RTCTestStats stats(id, timestamp);
241 stats.m_bool = m_bool;
242 stats.m_int32 = m_int32;
243 stats.m_int64 = m_int64;
244 stats.m_double = m_double;
245 stats.m_string = m_string;
246 stats.m_sequence_bool = sequence_bool;
247 stats.m_sequence_int32 = sequence_int32;
248 stats.m_sequence_int64 = sequence_int64;
249 stats.m_sequence_double = sequence_double;
250 stats.m_sequence_string = sequence_string;
251
252 Json::Value json_output;
253 EXPECT_TRUE(Json::Reader().parse(stats.ToJson(), json_output));
254
255 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "id", &id));
256 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "timestamp", &timestamp));
257 EXPECT_TRUE(rtc::GetBoolFromJsonObject(json_output, "mBool", &m_bool));
258 EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "mInt32", &m_int32));
259 EXPECT_TRUE(rtc::GetDoubleFromJsonObject(json_output, "mDouble", &m_double));
260 EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "mString", &m_string));
261
262 Json::Value json_array;
263
264 EXPECT_TRUE(
265 rtc::GetValueFromJsonObject(json_output, "mSequenceBool", &json_array));
266 EXPECT_TRUE(rtc::JsonArrayToBoolVector(json_array, &sequence_bool));
267
268 EXPECT_TRUE(
269 rtc::GetValueFromJsonObject(json_output, "mSequenceInt32", &json_array));
270 EXPECT_TRUE(rtc::JsonArrayToIntVector(json_array, &sequence_int32));
271
272 EXPECT_TRUE(
273 rtc::GetValueFromJsonObject(json_output, "mSequenceDouble", &json_array));
274 EXPECT_TRUE(rtc::JsonArrayToDoubleVector(json_array, &sequence_double));
275
276 EXPECT_TRUE(
277 rtc::GetValueFromJsonObject(json_output, "mSequenceString", &json_array));
278 EXPECT_TRUE(rtc::JsonArrayToStringVector(json_array, &sequence_string));
279
280 EXPECT_EQ(id, stats.id());
281 EXPECT_EQ(timestamp, stats.timestamp_us());
282 EXPECT_EQ(m_bool, *stats.m_bool);
283 EXPECT_EQ(m_int32, *stats.m_int32);
284 EXPECT_EQ(m_string, *stats.m_string);
285 EXPECT_EQ(sequence_bool, *stats.m_sequence_bool);
286 EXPECT_EQ(sequence_int32, *stats.m_sequence_int32);
287 EXPECT_EQ(sequence_string, *stats.m_sequence_string);
288
289 EXPECT_NEAR(m_double, *stats.m_double, GetExpectedError(*stats.m_double));
290
291 EXPECT_EQ(sequence_double.size(), stats.m_sequence_double->size());
292 for (size_t i = 0; i < stats.m_sequence_double->size(); ++i) {
293 EXPECT_NEAR(sequence_double[i], stats.m_sequence_double->at(i),
294 GetExpectedError(stats.m_sequence_double->at(i)));
295 }
296
297 // We read mInt64 as double since JSON stores all numbers as doubles, so there
298 // is not enough precision to represent large numbers.
299 double m_int64_as_double;
300 std::vector<double> sequence_int64_as_double;
301
302 EXPECT_TRUE(
303 rtc::GetDoubleFromJsonObject(json_output, "mInt64", &m_int64_as_double));
304
305 EXPECT_TRUE(
306 rtc::GetValueFromJsonObject(json_output, "mSequenceInt64", &json_array));
307 EXPECT_TRUE(
308 rtc::JsonArrayToDoubleVector(json_array, &sequence_int64_as_double));
309
310 double stats_m_int64_as_double = static_cast<double>(*stats.m_int64);
311 EXPECT_NEAR(m_int64_as_double, stats_m_int64_as_double,
312 GetExpectedError(stats_m_int64_as_double));
313
314 EXPECT_EQ(sequence_int64_as_double.size(), stats.m_sequence_int64->size());
315 for (size_t i = 0; i < stats.m_sequence_int64->size(); ++i) {
316 const double stats_value_as_double =
317 static_cast<double>((*stats.m_sequence_int64)[i]);
318 EXPECT_NEAR(sequence_int64_as_double[i], stats_value_as_double,
319 GetExpectedError(stats_value_as_double));
320 }
321
322 // Neither stats.m_uint32 nor stats.m_uint64 are defined, so "mUint64" and
323 // "mUint32" should not be part of the generated JSON object.
324 int m_uint32;
325 int m_uint64;
326 EXPECT_FALSE(stats.m_uint32.is_defined());
327 EXPECT_FALSE(stats.m_uint64.is_defined());
328 EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint32", &m_uint32));
329 EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint64", &m_uint64));
330
331 std::cout << stats.ToJson() << std::endl;
332}
333
hbos615d3012016-08-24 01:33:13 -0700334// Death tests.
335// Disabled on Android because death tests misbehave on Android, see
336// base/test/gtest_util.h.
337#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
338
339TEST(RTCStatsDeathTest, ValueOfUndefinedMember) {
340 RTCTestStats stats("testId", 0.0);
341 EXPECT_FALSE(stats.m_int32.is_defined());
342 EXPECT_DEATH(*stats.m_int32, "");
343}
344
345TEST(RTCStatsDeathTest, InvalidCasting) {
346 RTCGrandChildStats stats("grandchild", 0.0);
347 EXPECT_DEATH(stats.cast_to<RTCChildStats>(), "");
348}
349
350#endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
351
352} // namespace webrtc