#include "src/trace_processor/metrics/metrics.h"
#include <cstdint>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "perfetto/protozero/field.h"
#include "perfetto/protozero/proto_decoder.h"
#include "perfetto/protozero/proto_utils.h"
#include "perfetto/trace_processor/basic_types.h"
#include "protos/perfetto/common/descriptor.pbzero.h"
#include "src/base/test/status_matchers.h"
#include "src/trace_processor/util/descriptors.h"
#include "test/gtest_and_gmock.h"
#include "protos/perfetto/trace_processor/metrics_impl.pbzero.h"
namespace perfetto::trace_processor::metrics {
namespace {
IsError;
IsError;
IsEmpty;
std::string RunTemplateReplace(
const std::string& str,
const std::unordered_map<std::string, std::string>& subs) { … }
TEST(…) {
auto res = RunTemplateReplace("no templates here", {});
ASSERT_EQ(…);
res = RunTemplateReplace("{{justtemplate}}", {{"justtemplate", "result"}});
ASSERT_EQ(…);
res = RunTemplateReplace("{{temp1}} {{temp2}}!",
{{"temp1", "hello"}, {"temp2", "world"}});
ASSERT_EQ(…);
std::string unused;
ASSERT_NE(…);
}
class ProtoBuilderTest : public ::testing::Test {
protected:
template <bool repeated>
protozero::TypedProtoDecoder<1, repeated> DecodeSingleFieldProto(
const std::vector<uint8_t>& result_ser) {
protos::pbzero::ProtoBuilderResult::Decoder result(result_ser.data(),
result_ser.size());
protos::pbzero::SingleBuilderResult::Decoder single(result.single());
protozero::ConstBytes proto_ser = single.protobuf();
return protozero::TypedProtoDecoder<1, repeated>(proto_ser.data,
proto_ser.size);
}
};
TEST_F(…) {
using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
DescriptorPool pool;
ProtoDescriptor descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestProto",
ProtoDescriptor::Type::kMessage, std::nullopt);
descriptor.AddField(FieldDescriptor("int_value", 1,
FieldDescriptorProto::TYPE_INT64, "",
std::vector<uint8_t>(), false, false));
ProtoBuilder builder(&pool, &descriptor);
ASSERT_OK(…);
auto result_ser = builder.SerializeToProtoBuilderResult();
auto proto = DecodeSingleFieldProto<false>(result_ser);
const protozero::Field& int_field = proto.Get(1);
ASSERT_EQ(…);
}
TEST_F(…) {
using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
DescriptorPool pool;
ProtoDescriptor descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestProto",
ProtoDescriptor::Type::kMessage, std::nullopt);
descriptor.AddField(FieldDescriptor("double_value", 1,
FieldDescriptorProto::TYPE_DOUBLE, "",
std::vector<uint8_t>(), false, false));
ProtoBuilder builder(&pool, &descriptor);
ASSERT_OK(…);
auto result_ser = builder.SerializeToProtoBuilderResult();
auto proto = DecodeSingleFieldProto<false>(result_ser);
const protozero::Field& db_field = proto.Get(1);
ASSERT_DOUBLE_EQ(…);
}
TEST_F(…) {
using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
DescriptorPool pool;
ProtoDescriptor descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestProto",
ProtoDescriptor::Type::kMessage, std::nullopt);
descriptor.AddField(FieldDescriptor("string_value", 1,
FieldDescriptorProto::TYPE_STRING, "",
std::vector<uint8_t>(), false, false));
ProtoBuilder builder(&pool, &descriptor);
ASSERT_OK(…);
auto result_ser = builder.SerializeToProtoBuilderResult();
auto proto = DecodeSingleFieldProto<false>(result_ser);
const protozero::Field& str_field = proto.Get(1);
ASSERT_EQ(…);
}
TEST_F(…) {
using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
DescriptorPool pool;
ProtoDescriptor nested("file.proto", ".perfetto.protos",
".perfetto.protos.TestProto.NestedProto",
ProtoDescriptor::Type::kMessage, std::nullopt);
nested.AddField(FieldDescriptor("nested_int_value", 1,
FieldDescriptorProto::TYPE_INT64, "",
std::vector<uint8_t>(), false, false));
ProtoDescriptor descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestProto",
ProtoDescriptor::Type::kMessage, std::nullopt);
auto field =
FieldDescriptor("nested_value", 1, FieldDescriptorProto::TYPE_MESSAGE,
".perfetto.protos.TestProto.NestedProto",
std::vector<uint8_t>(), false, false);
field.set_resolved_type_name(".perfetto.protos.TestProto.NestedProto");
descriptor.AddField(field);
ProtoBuilder nest_builder(&pool, &nested);
ASSERT_OK(…);
auto nest_ser = nest_builder.SerializeToProtoBuilderResult();
ProtoBuilder builder(&pool, &descriptor);
ASSERT_OK(…);
auto result_ser = builder.SerializeToProtoBuilderResult();
auto proto = DecodeSingleFieldProto<false>(result_ser);
const protozero::Field& nest_field = proto.Get(1);
ASSERT_EQ(…);
protozero::ConstBytes nest_bytes = nest_field.as_bytes();
protozero::TypedProtoDecoder<1, false> nest(nest_bytes.data, nest_bytes.size);
const protozero::Field& nest_int_field = nest.Get(1);
ASSERT_EQ(…);
ASSERT_EQ(…);
}
TEST_F(…) {
using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
DescriptorPool pool;
ProtoDescriptor descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestProto",
ProtoDescriptor::Type::kMessage, std::nullopt);
descriptor.AddField(FieldDescriptor("rep_int_value", 1,
FieldDescriptorProto::TYPE_INT64, "",
std::vector<uint8_t>(), true, false));
ASSERT_THAT(…);
ProtoBuilder builder(&pool, &descriptor);
ASSERT_OK(…);
auto proto =
DecodeSingleFieldProto<true>(builder.SerializeToProtoBuilderResult());
auto it = proto.GetRepeated<int64_t>(1);
ASSERT_FALSE(…);
}
TEST_F(…) {
using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
DescriptorPool pool;
ProtoDescriptor descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestProto",
ProtoDescriptor::Type::kMessage, std::nullopt);
descriptor.AddField(FieldDescriptor("rep_int_value", 1,
FieldDescriptorProto::TYPE_INT64, "",
std::vector<uint8_t>(), true, false));
RepeatedFieldBuilder rep_builder;
rep_builder.AddSqlValue(SqlValue::Long(1234));
rep_builder.AddSqlValue(SqlValue::Long(5678));
std::vector<uint8_t> rep_ser = rep_builder.SerializeToProtoBuilderResult();
ProtoBuilder builder(&pool, &descriptor);
ASSERT_OK(…);
auto proto =
DecodeSingleFieldProto<true>(builder.SerializeToProtoBuilderResult());
auto it = proto.GetRepeated<int64_t>(1);
ASSERT_EQ(…);
ASSERT_EQ(…);
ASSERT_FALSE(…);
}
TEST_F(…) {
using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
DescriptorPool pool;
ProtoDescriptor enum_descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestEnum",
ProtoDescriptor::Type::kEnum, std::nullopt);
enum_descriptor.AddEnumValue(1, "FIRST");
enum_descriptor.AddEnumValue(2, "SECOND");
enum_descriptor.AddEnumValue(3, "THIRD");
pool.AddProtoDescriptorForTesting(enum_descriptor);
ProtoDescriptor descriptor("file.proto", ".perfetto.protos",
".perfetto.protos.TestMessage",
ProtoDescriptor::Type::kMessage, std::nullopt);
FieldDescriptor enum_field("enum_value", 1, FieldDescriptorProto::TYPE_ENUM,
".perfetto.protos.TestEnum",
std::vector<uint8_t>(), false, false);
enum_field.set_resolved_type_name(".perfetto.protos.TestEnum");
descriptor.AddField(enum_field);
pool.AddProtoDescriptorForTesting(descriptor);
ProtoBuilder value_builder(&pool, &descriptor);
ASSERT_THAT(…);
ASSERT_OK(…);
ASSERT_THAT(…);
auto value_proto = DecodeSingleFieldProto<false>(
value_builder.SerializeToProtoBuilderResult());
ASSERT_EQ(…);
ProtoBuilder str_builder(&pool, &descriptor);
ASSERT_THAT(…);
ASSERT_OK(…);
ASSERT_THAT(…);
auto str_proto = DecodeSingleFieldProto<false>(
str_builder.SerializeToProtoBuilderResult());
ASSERT_EQ(…);
}
}
}