1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/metrics/metrics.h"
18
19 #include <vector>
20
21 #include "protos/perfetto/common/descriptor.pbzero.h"
22 #include "test/gtest_and_gmock.h"
23
24 namespace perfetto {
25 namespace trace_processor {
26 namespace metrics {
27
28 namespace {
29
RunTemplateReplace(const std::string & str,std::unordered_map<std::string,std::string> subs)30 std::string RunTemplateReplace(
31 const std::string& str,
32 std::unordered_map<std::string, std::string> subs) {
33 std::string out;
34 EXPECT_EQ(TemplateReplace(str, subs, &out), 0);
35 return out;
36 }
37
TEST(MetricsTest,TemplateReplace)38 TEST(MetricsTest, TemplateReplace) {
39 auto res = RunTemplateReplace("no templates here", {});
40 ASSERT_EQ(res, "no templates here");
41
42 res = RunTemplateReplace("{{justtemplate}}", {{"justtemplate", "result"}});
43 ASSERT_EQ(res, "result");
44
45 res = RunTemplateReplace("{{temp1}} {{temp2}}!",
46 {{"temp1", "hello"}, {"temp2", "world"}});
47 ASSERT_EQ(res, "hello world!");
48
49 std::string unused;
50 ASSERT_NE(TemplateReplace("{{missing}}", {{}}, &unused), 0);
51 }
52
53 class ProtoBuilderTest : public ::testing::Test {
54 protected:
55 template <bool repeated>
DecodeSingleFieldProto(const std::vector<uint8_t> & result_ser)56 protozero::TypedProtoDecoder<1, repeated> DecodeSingleFieldProto(
57 const std::vector<uint8_t>& result_ser) {
58 protos::pbzero::ProtoBuilderResult::Decoder result(result_ser.data(),
59 result_ser.size());
60 protozero::ConstBytes single_ser = result.single();
61 protos::pbzero::SingleBuilderResult::Decoder single(single_ser.data,
62 single_ser.size);
63
64 protozero::ConstBytes proto_ser = single.protobuf();
65 return protozero::TypedProtoDecoder<1, repeated>(proto_ser.data,
66 proto_ser.size);
67 }
68 };
69
TEST_F(ProtoBuilderTest,AppendLong)70 TEST_F(ProtoBuilderTest, AppendLong) {
71 using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
72
73 // Create the descriptor version of the following message:
74 // message TestProto {
75 // optional int64 int_value = 1;
76 // }
77 ProtoDescriptor descriptor(".perfetto.protos", ".perfetto.protos.TestProto",
78 ProtoDescriptor::Type::kMessage, base::nullopt);
79 descriptor.AddField(FieldDescriptor(
80 "int_value", 1, FieldDescriptorProto::TYPE_INT64, "", false));
81
82 ProtoBuilder builder(&descriptor);
83 ASSERT_TRUE(builder.AppendLong("int_value", 12345).ok());
84
85 auto result_ser = builder.SerializeToProtoBuilderResult();
86 auto proto = DecodeSingleFieldProto<false>(result_ser);
87 const protozero::Field& int_field = proto.Get(1);
88 ASSERT_EQ(int_field.as_int64(), 12345);
89 }
90
TEST_F(ProtoBuilderTest,AppendDouble)91 TEST_F(ProtoBuilderTest, AppendDouble) {
92 using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
93
94 // Create the descriptor version of the following message:
95 // message TestProto {
96 // optional double double_value = 1;
97 // }
98 ProtoDescriptor descriptor(".perfetto.protos", ".perfetto.protos.TestProto",
99 ProtoDescriptor::Type::kMessage, base::nullopt);
100 descriptor.AddField(FieldDescriptor(
101 "double_value", 1, FieldDescriptorProto::TYPE_DOUBLE, "", false));
102
103 ProtoBuilder builder(&descriptor);
104 ASSERT_TRUE(builder.AppendDouble("double_value", 1.2345).ok());
105
106 auto result_ser = builder.SerializeToProtoBuilderResult();
107 auto proto = DecodeSingleFieldProto<false>(result_ser);
108 const protozero::Field& db_field = proto.Get(1);
109 ASSERT_DOUBLE_EQ(db_field.as_double(), 1.2345);
110 }
111
TEST_F(ProtoBuilderTest,AppendString)112 TEST_F(ProtoBuilderTest, AppendString) {
113 using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
114
115 // Create the descriptor version of the following message:
116 // message TestProto {
117 // optional string string_value = 1;
118 // }
119 ProtoDescriptor descriptor(".perfetto.protos", ".perfetto.protos.TestProto",
120 ProtoDescriptor::Type::kMessage, base::nullopt);
121 descriptor.AddField(FieldDescriptor(
122 "string_value", 1, FieldDescriptorProto::TYPE_STRING, "", false));
123
124 ProtoBuilder builder(&descriptor);
125 ASSERT_TRUE(builder.AppendString("string_value", "hello world!").ok());
126
127 auto result_ser = builder.SerializeToProtoBuilderResult();
128 auto proto = DecodeSingleFieldProto<false>(result_ser);
129 const protozero::Field& str_field = proto.Get(1);
130 ASSERT_EQ(str_field.as_std_string(), "hello world!");
131 }
132
TEST_F(ProtoBuilderTest,AppendNested)133 TEST_F(ProtoBuilderTest, AppendNested) {
134 using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
135
136 // Create the descriptor version of the following message:
137 // message TestProto {
138 // message NestedProto {
139 // optional int64 nested_int_value = 1;
140 // }
141 // optional NestedProto nested_value = 1;
142 // }
143 ProtoDescriptor nested(".perfetto.protos",
144 ".perfetto.protos.TestProto.NestedProto",
145 ProtoDescriptor::Type::kMessage, base::nullopt);
146 nested.AddField(FieldDescriptor("nested_int_value", 1,
147 FieldDescriptorProto::TYPE_INT64, "", false));
148
149 ProtoDescriptor descriptor(".perfetto.protos", ".perfetto.protos.TestProto",
150 ProtoDescriptor::Type::kMessage, base::nullopt);
151 auto field =
152 FieldDescriptor("nested_value", 1, FieldDescriptorProto::TYPE_MESSAGE,
153 ".perfetto.protos.TestProto.NestedProto", false);
154 field.set_resolved_type_name(".perfetto.protos.TestProto.NestedProto");
155 descriptor.AddField(field);
156
157 ProtoBuilder nest_builder(&nested);
158 ASSERT_TRUE(nest_builder.AppendLong("nested_int_value", 789).ok());
159
160 auto nest_ser = nest_builder.SerializeToProtoBuilderResult();
161
162 ProtoBuilder builder(&descriptor);
163 ASSERT_TRUE(
164 builder.AppendBytes("nested_value", nest_ser.data(), nest_ser.size())
165 .ok());
166
167 auto result_ser = builder.SerializeToProtoBuilderResult();
168 auto proto = DecodeSingleFieldProto<false>(result_ser);
169 const protozero::Field& nest_field = proto.Get(1);
170 ASSERT_EQ(nest_field.type(),
171 protozero::proto_utils::ProtoWireType::kLengthDelimited);
172
173 protozero::ConstBytes nest_bytes = nest_field.as_bytes();
174 protozero::TypedProtoDecoder<1, false> nest(nest_bytes.data, nest_bytes.size);
175
176 const protozero::Field& nest_int_field = nest.Get(1);
177 ASSERT_EQ(nest_int_field.type(),
178 protozero::proto_utils::ProtoWireType::kVarInt);
179 ASSERT_EQ(nest_int_field.as_int64(), 789);
180 }
181
TEST_F(ProtoBuilderTest,AppendRepeatedPrimitive)182 TEST_F(ProtoBuilderTest, AppendRepeatedPrimitive) {
183 using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto;
184
185 // Create the descriptor version of the following message:
186 // message TestProto {
187 // repeated int64 int_value = 1;
188 // }
189 ProtoDescriptor descriptor(".perfetto.protos", ".perfetto.protos.TestProto",
190 ProtoDescriptor::Type::kMessage, base::nullopt);
191 descriptor.AddField(FieldDescriptor(
192 "rep_int_value", 1, FieldDescriptorProto::TYPE_INT64, "", true));
193
194 RepeatedFieldBuilder rep_builder;
195 rep_builder.AddLong(1234);
196 rep_builder.AddLong(5678);
197
198 std::vector<uint8_t> rep_ser = rep_builder.SerializeToProtoBuilderResult();
199
200 ProtoBuilder builder(&descriptor);
201 ASSERT_TRUE(
202 builder.AppendBytes("rep_int_value", rep_ser.data(), rep_ser.size())
203 .ok());
204
205 auto result_ser = builder.SerializeToProtoBuilderResult();
206 auto proto = DecodeSingleFieldProto<true>(result_ser);
207 auto it = proto.GetRepeated<int64_t>(1);
208 ASSERT_EQ(*it, 1234);
209 ASSERT_EQ(*++it, 5678);
210 ASSERT_FALSE(++it);
211 }
212
213 } // namespace
214
215 } // namespace metrics
216 } // namespace trace_processor
217 } // namespace perfetto
218