[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 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);
// RTCStatsMemberInterface implementation.
// TODO(https://crbug.com/webrtc/15164): Delete RTCStatsMemberInterface in

View File

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

View File

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

View File

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