1 /*
2 * Copyright (C) 2022 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/util/descriptors.h"
18
19 #include <cstddef>
20 #include <cstdint>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "perfetto/protozero/scattered_heap_buffer.h"
26 #include "src/protozero/test/example_proto/test_messages.pbzero.h"
27 #include "src/trace_processor/test_messages.descriptor.h"
28 #include "src/trace_processor/util/proto_profiler.h"
29 #include "test/gtest_and_gmock.h"
30
31 namespace perfetto::trace_processor::util {
32 namespace {
33
34 using ::testing::UnorderedElementsAreArray;
35
TEST(ProtoProfiler,TestMessage)36 TEST(ProtoProfiler, TestMessage) {
37 protozero::HeapBuffered<protozero::test::protos::pbzero::NestedA> message;
38 message->add_repeated_a()->set_value_b()->set_value_c(1);
39 message->add_repeated_a()->set_value_b()->set_value_c(2);
40 message->set_super_nested()->set_value_c(3);
41 const std::vector<uint8_t> bytes = message.SerializeAsArray();
42
43 DescriptorPool pool;
44 pool.AddFromFileDescriptorSet(kTestMessagesDescriptor.data(),
45 kTestMessagesDescriptor.size());
46 SizeProfileComputer computer(&pool, ".protozero.test.protos.NestedA");
47 computer.Reset(bytes.data(), bytes.size());
48
49 // Convert to vector for test matcher.
50 using Item = std::pair<std::vector<std::string>, size_t>;
51 std::vector<Item> got;
52 for (auto sample = computer.GetNext(); sample; sample = computer.GetNext()) {
53 std::vector<std::string> path;
54 for (const auto& field : computer.GetPath()) {
55 if (field.has_field_name())
56 path.push_back(field.field_name());
57 path.push_back(field.type_name());
58 }
59 got.emplace_back(path, *sample);
60 }
61 std::vector<Item> expected{
62 {{"NestedA"}, 6},
63 {{"NestedA", "#repeated_a", "NestedB"}, 2},
64 {{"NestedA", "#repeated_a", "NestedB"}, 2},
65 {{"NestedA", "#repeated_a", "NestedB", "#value_b", "NestedC"}, 1},
66 {{"NestedA", "#repeated_a", "NestedB", "#value_b", "NestedC"}, 1},
67 {{"NestedA", "#repeated_a", "NestedB", "#value_b", "NestedC", "#value_c",
68 "int32"},
69 1},
70 {{"NestedA", "#repeated_a", "NestedB", "#value_b", "NestedC", "#value_c",
71 "int32"},
72 1},
73 {{"NestedA", "#super_nested", "NestedC"}, 1},
74 {{"NestedA", "#super_nested", "NestedC", "#value_c", "int32"}, 1}};
75
76 EXPECT_THAT(got, UnorderedElementsAreArray(expected));
77 }
78
79 } // namespace
80 } // namespace perfetto::trace_processor::util
81