[Stats] Migrate RTCStats/Test to Attributes.

One in series of CLs to migrate from Members() to Attributes() to
unblock code deletion.

Adds and makes use of has_value(), holds_alternative<T>() and get<T>()
convenience methods.

Bug: webrtc:15164
Change-Id: I2ae9869a5d1de3f3875c70494c9781d69da7f936
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/334160
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Evan Shrubsole <eshr@google.com>
Cr-Commit-Position: refs/heads/main@{#41510}
This commit is contained in:
Henrik Boström 2024-01-12 09:24:06 +01:00 committed by WebRTC LUCI CQ
parent 634cb403e6
commit bfee961786
4 changed files with 43 additions and 25 deletions

View File

@ -53,6 +53,18 @@ class RTC_EXPORT Attribute : public RTCStatsMemberInterface {
const char* name() const; const char* name() const;
const StatVariant& as_variant() const; const StatVariant& as_variant() const;
bool has_value() const;
template <typename T>
bool holds_alternative() const {
return absl::holds_alternative<const RTCStatsMember<T>*>(attribute_);
}
template <typename T>
const T& get() const {
RTC_CHECK(holds_alternative<T>());
RTC_CHECK(has_value());
return absl::get<const RTCStatsMember<T>*>(attribute_)->value();
}
static Attribute FromMemberInterface(const RTCStatsMemberInterface* member); static Attribute FromMemberInterface(const RTCStatsMemberInterface* member);
// RTCStatsMemberInterface implementation. // RTCStatsMemberInterface implementation.
// TODO(https://crbug.com/webrtc/15164): Delete RTCStatsMemberInterface in // TODO(https://crbug.com/webrtc/15164): Delete RTCStatsMemberInterface in

View File

@ -99,6 +99,11 @@ const Attribute::StatVariant& Attribute::as_variant() const {
return attribute_; return attribute_;
} }
bool Attribute::has_value() const {
return absl::visit([](const auto* attr) { return attr->has_value(); },
attribute_);
}
RTCStatsMemberInterface::Type Attribute::type() const { RTCStatsMemberInterface::Type Attribute::type() const {
return absl::visit([](const auto* attr) { return attr->type(); }, attribute_); return absl::visit([](const auto* attr) { return attr->type(); }, attribute_);
} }

View File

@ -24,16 +24,13 @@ RTCStats::~RTCStats() {}
bool RTCStats::operator==(const RTCStats& other) const { bool RTCStats::operator==(const RTCStats& other) const {
if (type() != other.type() || id() != other.id()) if (type() != other.type() || id() != other.id())
return false; return false;
std::vector<const RTCStatsMemberInterface*> members = Members(); std::vector<Attribute> attributes = Attributes();
std::vector<const RTCStatsMemberInterface*> other_members = other.Members(); std::vector<Attribute> other_attributes = other.Attributes();
RTC_DCHECK_EQ(members.size(), other_members.size()); RTC_DCHECK_EQ(attributes.size(), other_attributes.size());
for (size_t i = 0; i < members.size(); ++i) { for (size_t i = 0; i < attributes.size(); ++i) {
const RTCStatsMemberInterface* member = members[i]; if (attributes[i] != other_attributes[i]) {
const RTCStatsMemberInterface* other_member = other_members[i];
RTC_DCHECK_EQ(member->type(), other_member->type());
RTC_DCHECK_EQ(member->name(), other_member->name());
if (*member != *other_member)
return false; return false;
}
} }
return true; return true;
} }
@ -51,13 +48,17 @@ std::string RTCStats::ToJson() const {
<< "\"," << "\","
"\"timestamp\":" "\"timestamp\":"
<< timestamp_.us(); << timestamp_.us();
for (const RTCStatsMemberInterface* member : Members()) { for (const Attribute& attribute : Attributes()) {
if (member->is_defined()) { if (attribute.has_value()) {
sb << ",\"" << member->name() << "\":"; sb << ",\"" << attribute.name() << "\":";
if (member->is_string()) if (attribute.holds_alternative<std::string>()) {
sb << "\"" << member->ValueToJson() << "\""; sb << "\"";
else }
sb << member->ValueToJson(); sb << absl::visit([](const auto* attr) { return attr->ValueToJson(); },
attribute.as_variant());
if (attribute.holds_alternative<std::string>()) {
sb << "\"";
}
} }
} }
sb << "}"; sb << "}";

View File

@ -66,14 +66,14 @@ WEBRTC_RTCSTATS_IMPL(RTCGrandChildStats,
"grandchild-stats", "grandchild-stats",
&grandchild_int) &grandchild_int)
TEST(RTCStatsTest, RTCStatsAndMembers) { TEST(RTCStatsTest, RTCStatsAndAttributes) {
RTCTestStats stats("testId", Timestamp::Micros(42)); RTCTestStats stats("testId", Timestamp::Micros(42));
EXPECT_EQ(stats.id(), "testId"); EXPECT_EQ(stats.id(), "testId");
EXPECT_EQ(stats.timestamp().us(), static_cast<int64_t>(42)); EXPECT_EQ(stats.timestamp().us(), static_cast<int64_t>(42));
std::vector<const RTCStatsMemberInterface*> members = stats.Members(); std::vector<Attribute> attributes = stats.Attributes();
EXPECT_EQ(members.size(), static_cast<size_t>(16)); EXPECT_EQ(attributes.size(), static_cast<size_t>(16));
for (const RTCStatsMemberInterface* member : members) { for (const auto& attribute : attributes) {
EXPECT_FALSE(member->is_defined()); EXPECT_FALSE(attribute.has_value());
} }
stats.m_bool = true; stats.m_bool = true;
stats.m_int32 = 123; stats.m_int32 = 123;
@ -111,8 +111,8 @@ TEST(RTCStatsTest, RTCStatsAndMembers) {
stats.m_sequence_string = sequence_string; stats.m_sequence_string = sequence_string;
stats.m_map_string_uint64 = map_string_uint64; stats.m_map_string_uint64 = map_string_uint64;
stats.m_map_string_double = map_string_double; stats.m_map_string_double = map_string_double;
for (const RTCStatsMemberInterface* member : members) { for (const auto& attribute : attributes) {
EXPECT_TRUE(member->is_defined()); EXPECT_TRUE(attribute.has_value());
} }
EXPECT_EQ(*stats.m_bool, true); EXPECT_EQ(*stats.m_bool, true);
EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123)); EXPECT_EQ(*stats.m_int32, static_cast<int32_t>(123));
@ -217,8 +217,8 @@ TEST(RTCStatsTest, RTCStatsGrandChild) {
stats.child_int = 1; stats.child_int = 1;
stats.grandchild_int = 2; stats.grandchild_int = 2;
int32_t sum = 0; int32_t sum = 0;
for (const RTCStatsMemberInterface* member : stats.Members()) { for (const auto& attribute : stats.Attributes()) {
sum += *member->cast_to<const RTCStatsMember<int32_t>>(); sum += attribute.get<int32_t>();
} }
EXPECT_EQ(sum, static_cast<int32_t>(3)); EXPECT_EQ(sum, static_cast<int32_t>(3));