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