You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "api/stats/rtc_stats.h"
  11. #include <cmath>
  12. #include <cstdint>
  13. #include <cstring>
  14. #include <iostream>
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/strings/json.h"
  17. #include "stats/test/rtc_test_stats.h"
  18. #include "test/gtest.h"
  19. namespace webrtc {
  20. namespace {
  21. // JSON stores numbers as floating point numbers with 53 significant bits, which
  22. // amounts to about 15.95 decimal digits. Thus, when comparing large numbers
  23. // processed by JSON, that's all the precision we should expect.
  24. const double JSON_EPSILON = 1e-15;
  25. // We do this since Google Test doesn't support relative error.
  26. // This is computed as follows:
  27. // If |a - b| / |a| < EPS, then |a - b| < |a| * EPS, so |a| * EPS is the
  28. // maximum expected error.
  29. double GetExpectedError(const double expected_value) {
  30. return JSON_EPSILON * fabs(expected_value);
  31. }
  32. } // namespace
  33. class RTCChildStats : public RTCStats {
  34. public:
  35. WEBRTC_RTCSTATS_DECL();
  36. RTCChildStats(const std::string& id, int64_t timestamp_us)
  37. : RTCStats(id, timestamp_us), child_int("childInt") {}
  38. RTCStatsMember<int32_t> child_int;
  39. };
  40. WEBRTC_RTCSTATS_IMPL(RTCChildStats, RTCStats, "child-stats", &child_int)
  41. class RTCGrandChildStats : public RTCChildStats {
  42. public:
  43. WEBRTC_RTCSTATS_DECL();
  44. RTCGrandChildStats(const std::string& id, int64_t timestamp_us)
  45. : RTCChildStats(id, timestamp_us), grandchild_int("grandchildInt") {}
  46. RTCStatsMember<int32_t> grandchild_int;
  47. };
  48. WEBRTC_RTCSTATS_IMPL(RTCGrandChildStats,
  49. RTCChildStats,
  50. "grandchild-stats",
  51. &grandchild_int)
  52. TEST(RTCStatsTest, RTCStatsAndMembers) {
  53. RTCTestStats stats("testId", 42);
  54. EXPECT_EQ(stats.id(), "testId");
  55. EXPECT_EQ(stats.timestamp_us(), static_cast<int64_t>(42));
  56. std::vector<const RTCStatsMemberInterface*> members = stats.Members();
  57. EXPECT_EQ(members.size(), static_cast<size_t>(14));
  58. for (const RTCStatsMemberInterface* member : members) {
  59. EXPECT_FALSE(member->is_defined());
  60. }
  61. stats.m_bool = true;
  62. stats.m_int32 = 123;
  63. stats.m_uint32 = 123;
  64. stats.m_int64 = 123;
  65. stats.m_uint64 = 123;
  66. stats.m_double = 123.0;
  67. stats.m_string = std::string("123");
  68. std::vector<bool> sequence_bool;
  69. sequence_bool.push_back(true);
  70. std::vector<int32_t> sequence_int32;
  71. sequence_int32.push_back(static_cast<int32_t>(1));
  72. std::vector<uint32_t> sequence_uint32;
  73. sequence_uint32.push_back(static_cast<uint32_t>(2));
  74. std::vector<int64_t> sequence_int64;
  75. sequence_int64.push_back(static_cast<int64_t>(3));
  76. std::vector<uint64_t> sequence_uint64;
  77. sequence_uint64.push_back(static_cast<uint64_t>(4));
  78. std::vector<double> sequence_double;
  79. sequence_double.push_back(5.0);
  80. std::vector<std::string> sequence_string;
  81. sequence_string.push_back(std::string("six"));
  82. stats.m_sequence_bool = sequence_bool;
  83. stats.m_sequence_int32 = sequence_int32;
  84. stats.m_sequence_uint32 = sequence_uint32;
  85. EXPECT_FALSE(stats.m_sequence_int64.is_defined());
  86. stats.m_sequence_int64 = sequence_int64;
  87. stats.m_sequence_uint64 = sequence_uint64;
  88. stats.m_sequence_double = sequence_double;
  89. stats.m_sequence_string = sequence_string;
  90. for (const RTCStatsMemberInterface* member : members) {
  91. EXPECT_TRUE(member->is_defined());
  92. }
  93. EXPECT_EQ(*stats.m_bool, true);
  94. EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123));
  95. EXPECT_EQ(*stats.m_uint32, static_cast<uint32_t>(123));
  96. EXPECT_EQ(*stats.m_int64, static_cast<int64_t>(123));
  97. EXPECT_EQ(*stats.m_uint64, static_cast<uint64_t>(123));
  98. EXPECT_EQ(*stats.m_double, 123.0);
  99. EXPECT_EQ(*stats.m_string, std::string("123"));
  100. EXPECT_EQ(*stats.m_sequence_bool, sequence_bool);
  101. EXPECT_EQ(*stats.m_sequence_int32, sequence_int32);
  102. EXPECT_EQ(*stats.m_sequence_uint32, sequence_uint32);
  103. EXPECT_EQ(*stats.m_sequence_int64, sequence_int64);
  104. EXPECT_EQ(*stats.m_sequence_uint64, sequence_uint64);
  105. EXPECT_EQ(*stats.m_sequence_double, sequence_double);
  106. EXPECT_EQ(*stats.m_sequence_string, sequence_string);
  107. int32_t numbers[] = {4, 8, 15, 16, 23, 42};
  108. std::vector<int32_t> numbers_sequence(&numbers[0], &numbers[6]);
  109. stats.m_sequence_int32->clear();
  110. stats.m_sequence_int32->insert(stats.m_sequence_int32->end(),
  111. numbers_sequence.begin(),
  112. numbers_sequence.end());
  113. EXPECT_EQ(*stats.m_sequence_int32, numbers_sequence);
  114. }
  115. TEST(RTCStatsTest, EqualityOperator) {
  116. RTCTestStats empty_stats("testId", 123);
  117. EXPECT_EQ(empty_stats, empty_stats);
  118. RTCTestStats stats_with_all_values = empty_stats;
  119. stats_with_all_values.m_bool = true;
  120. stats_with_all_values.m_int32 = 123;
  121. stats_with_all_values.m_uint32 = 123;
  122. stats_with_all_values.m_int64 = 123;
  123. stats_with_all_values.m_uint64 = 123;
  124. stats_with_all_values.m_double = 123.0;
  125. stats_with_all_values.m_string = "123";
  126. stats_with_all_values.m_sequence_bool = std::vector<bool>();
  127. stats_with_all_values.m_sequence_int32 = std::vector<int32_t>();
  128. stats_with_all_values.m_sequence_uint32 = std::vector<uint32_t>();
  129. stats_with_all_values.m_sequence_int64 = std::vector<int64_t>();
  130. stats_with_all_values.m_sequence_uint64 = std::vector<uint64_t>();
  131. stats_with_all_values.m_sequence_double = std::vector<double>();
  132. stats_with_all_values.m_sequence_string = std::vector<std::string>();
  133. EXPECT_NE(stats_with_all_values, empty_stats);
  134. EXPECT_EQ(stats_with_all_values, stats_with_all_values);
  135. EXPECT_NE(stats_with_all_values.m_int32, stats_with_all_values.m_uint32);
  136. RTCTestStats one_member_different[] = {
  137. stats_with_all_values, stats_with_all_values, stats_with_all_values,
  138. stats_with_all_values, stats_with_all_values, stats_with_all_values,
  139. stats_with_all_values, stats_with_all_values, stats_with_all_values,
  140. stats_with_all_values, stats_with_all_values, stats_with_all_values,
  141. stats_with_all_values, stats_with_all_values,
  142. };
  143. for (size_t i = 0; i < 14; ++i) {
  144. EXPECT_EQ(stats_with_all_values, one_member_different[i]);
  145. }
  146. one_member_different[0].m_bool = false;
  147. one_member_different[1].m_int32 = 321;
  148. one_member_different[2].m_uint32 = 321;
  149. one_member_different[3].m_int64 = 321;
  150. one_member_different[4].m_uint64 = 321;
  151. one_member_different[5].m_double = 321.0;
  152. one_member_different[6].m_string = "321";
  153. one_member_different[7].m_sequence_bool->push_back(false);
  154. one_member_different[8].m_sequence_int32->push_back(321);
  155. one_member_different[9].m_sequence_uint32->push_back(321);
  156. one_member_different[10].m_sequence_int64->push_back(321);
  157. one_member_different[11].m_sequence_uint64->push_back(321);
  158. one_member_different[12].m_sequence_double->push_back(321.0);
  159. one_member_different[13].m_sequence_string->push_back("321");
  160. for (size_t i = 0; i < 14; ++i) {
  161. EXPECT_NE(stats_with_all_values, one_member_different[i]);
  162. }
  163. RTCTestStats empty_stats_different_id("testId2", 123);
  164. EXPECT_NE(empty_stats, empty_stats_different_id);
  165. RTCTestStats empty_stats_different_timestamp("testId", 321);
  166. EXPECT_EQ(empty_stats, empty_stats_different_timestamp);
  167. RTCChildStats child("childId", 42);
  168. RTCGrandChildStats grandchild("grandchildId", 42);
  169. EXPECT_NE(child, grandchild);
  170. RTCChildStats stats_with_defined_member("leId", 0);
  171. stats_with_defined_member.child_int = 0;
  172. RTCChildStats stats_with_undefined_member("leId", 0);
  173. EXPECT_NE(stats_with_defined_member, stats_with_undefined_member);
  174. EXPECT_NE(stats_with_undefined_member, stats_with_defined_member);
  175. }
  176. TEST(RTCStatsTest, RTCStatsGrandChild) {
  177. RTCGrandChildStats stats("grandchild", 0.0);
  178. stats.child_int = 1;
  179. stats.grandchild_int = 2;
  180. int32_t sum = 0;
  181. for (const RTCStatsMemberInterface* member : stats.Members()) {
  182. sum += *member->cast_to<const RTCStatsMember<int32_t>>();
  183. }
  184. EXPECT_EQ(sum, static_cast<int32_t>(3));
  185. std::unique_ptr<RTCStats> copy_ptr = stats.copy();
  186. const RTCGrandChildStats& copy = copy_ptr->cast_to<RTCGrandChildStats>();
  187. EXPECT_EQ(*copy.child_int, *stats.child_int);
  188. EXPECT_EQ(*copy.grandchild_int, *stats.grandchild_int);
  189. }
  190. TEST(RTCStatsTest, RTCStatsPrintsValidJson) {
  191. std::string id = "statsId";
  192. int timestamp = 42;
  193. bool m_bool = true;
  194. int m_int32 = 123;
  195. int64_t m_int64 = 1234567890123456499L;
  196. double m_double = 123.4567890123456499;
  197. std::string m_string = "123";
  198. std::vector<bool> sequence_bool;
  199. std::vector<int32_t> sequence_int32;
  200. sequence_int32.push_back(static_cast<int32_t>(1));
  201. std::vector<int64_t> sequence_int64;
  202. sequence_int64.push_back(static_cast<int64_t>(-1234567890123456499L));
  203. sequence_int64.push_back(static_cast<int64_t>(1));
  204. sequence_int64.push_back(static_cast<int64_t>(1234567890123456499L));
  205. std::vector<double> sequence_double;
  206. sequence_double.push_back(123.4567890123456499);
  207. sequence_double.push_back(1234567890123.456499);
  208. std::vector<std::string> sequence_string;
  209. sequence_string.push_back(std::string("four"));
  210. RTCTestStats stats(id, timestamp);
  211. stats.m_bool = m_bool;
  212. stats.m_int32 = m_int32;
  213. stats.m_int64 = m_int64;
  214. stats.m_double = m_double;
  215. stats.m_string = m_string;
  216. stats.m_sequence_bool = sequence_bool;
  217. stats.m_sequence_int32 = sequence_int32;
  218. stats.m_sequence_int64 = sequence_int64;
  219. stats.m_sequence_double = sequence_double;
  220. stats.m_sequence_string = sequence_string;
  221. Json::Value json_output;
  222. EXPECT_TRUE(Json::Reader().parse(stats.ToJson(), json_output));
  223. EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "id", &id));
  224. EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "timestamp", &timestamp));
  225. EXPECT_TRUE(rtc::GetBoolFromJsonObject(json_output, "mBool", &m_bool));
  226. EXPECT_TRUE(rtc::GetIntFromJsonObject(json_output, "mInt32", &m_int32));
  227. EXPECT_TRUE(rtc::GetDoubleFromJsonObject(json_output, "mDouble", &m_double));
  228. EXPECT_TRUE(rtc::GetStringFromJsonObject(json_output, "mString", &m_string));
  229. Json::Value json_array;
  230. EXPECT_TRUE(
  231. rtc::GetValueFromJsonObject(json_output, "mSequenceBool", &json_array));
  232. EXPECT_TRUE(rtc::JsonArrayToBoolVector(json_array, &sequence_bool));
  233. EXPECT_TRUE(
  234. rtc::GetValueFromJsonObject(json_output, "mSequenceInt32", &json_array));
  235. EXPECT_TRUE(rtc::JsonArrayToIntVector(json_array, &sequence_int32));
  236. EXPECT_TRUE(
  237. rtc::GetValueFromJsonObject(json_output, "mSequenceDouble", &json_array));
  238. EXPECT_TRUE(rtc::JsonArrayToDoubleVector(json_array, &sequence_double));
  239. EXPECT_TRUE(
  240. rtc::GetValueFromJsonObject(json_output, "mSequenceString", &json_array));
  241. EXPECT_TRUE(rtc::JsonArrayToStringVector(json_array, &sequence_string));
  242. EXPECT_EQ(id, stats.id());
  243. EXPECT_EQ(timestamp, stats.timestamp_us());
  244. EXPECT_EQ(m_bool, *stats.m_bool);
  245. EXPECT_EQ(m_int32, *stats.m_int32);
  246. EXPECT_EQ(m_string, *stats.m_string);
  247. EXPECT_EQ(sequence_bool, *stats.m_sequence_bool);
  248. EXPECT_EQ(sequence_int32, *stats.m_sequence_int32);
  249. EXPECT_EQ(sequence_string, *stats.m_sequence_string);
  250. EXPECT_NEAR(m_double, *stats.m_double, GetExpectedError(*stats.m_double));
  251. EXPECT_EQ(sequence_double.size(), stats.m_sequence_double->size());
  252. for (size_t i = 0; i < stats.m_sequence_double->size(); ++i) {
  253. EXPECT_NEAR(sequence_double[i], stats.m_sequence_double->at(i),
  254. GetExpectedError(stats.m_sequence_double->at(i)));
  255. }
  256. // We read mInt64 as double since JSON stores all numbers as doubles, so there
  257. // is not enough precision to represent large numbers.
  258. double m_int64_as_double;
  259. std::vector<double> sequence_int64_as_double;
  260. EXPECT_TRUE(
  261. rtc::GetDoubleFromJsonObject(json_output, "mInt64", &m_int64_as_double));
  262. EXPECT_TRUE(
  263. rtc::GetValueFromJsonObject(json_output, "mSequenceInt64", &json_array));
  264. EXPECT_TRUE(
  265. rtc::JsonArrayToDoubleVector(json_array, &sequence_int64_as_double));
  266. double stats_m_int64_as_double = static_cast<double>(*stats.m_int64);
  267. EXPECT_NEAR(m_int64_as_double, stats_m_int64_as_double,
  268. GetExpectedError(stats_m_int64_as_double));
  269. EXPECT_EQ(sequence_int64_as_double.size(), stats.m_sequence_int64->size());
  270. for (size_t i = 0; i < stats.m_sequence_int64->size(); ++i) {
  271. const double stats_value_as_double =
  272. static_cast<double>((*stats.m_sequence_int64)[i]);
  273. EXPECT_NEAR(sequence_int64_as_double[i], stats_value_as_double,
  274. GetExpectedError(stats_value_as_double));
  275. }
  276. // Neither stats.m_uint32 nor stats.m_uint64 are defined, so "mUint64" and
  277. // "mUint32" should not be part of the generated JSON object.
  278. int m_uint32;
  279. int m_uint64;
  280. EXPECT_FALSE(stats.m_uint32.is_defined());
  281. EXPECT_FALSE(stats.m_uint64.is_defined());
  282. EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint32", &m_uint32));
  283. EXPECT_FALSE(rtc::GetIntFromJsonObject(json_output, "mUint64", &m_uint64));
  284. std::cout << stats.ToJson() << std::endl;
  285. }
  286. TEST(RTCStatsTest, IsStandardized) {
  287. RTCStatsMember<int32_t> standardized("standardized");
  288. RTCNonStandardStatsMember<int32_t> unstandardized("unstandardized");
  289. EXPECT_TRUE(standardized.is_standardized());
  290. EXPECT_FALSE(unstandardized.is_standardized());
  291. }
  292. TEST(RTCStatsTest, NonStandardGroupId) {
  293. auto group_id = NonStandardGroupId::kGroupIdForTesting;
  294. RTCNonStandardStatsMember<int32_t> with_group_id("stat", {group_id});
  295. std::vector<NonStandardGroupId> expected_ids({group_id});
  296. EXPECT_EQ(expected_ids, with_group_id.group_ids());
  297. RTCNonStandardStatsMember<int32_t> without_group_id("stat");
  298. EXPECT_TRUE(without_group_id.group_ids().empty());
  299. }
  300. // Death tests.
  301. // Disabled on Android because death tests misbehave on Android, see
  302. // base/test/gtest_util.h.
  303. #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
  304. TEST(RTCStatsDeathTest, ValueOfUndefinedMember) {
  305. RTCTestStats stats("testId", 0.0);
  306. EXPECT_FALSE(stats.m_int32.is_defined());
  307. EXPECT_DEATH(*stats.m_int32, "");
  308. }
  309. TEST(RTCStatsDeathTest, InvalidCasting) {
  310. RTCGrandChildStats stats("grandchild", 0.0);
  311. EXPECT_DEATH(stats.cast_to<RTCChildStats>(), "");
  312. }
  313. #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
  314. } // namespace webrtc