xref: /aosp_15_r20/external/cronet/base/profiler/sample_metadata_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/profiler/sample_metadata.h"
6 
7 #include "base/metrics/metrics_hashes.h"
8 #include "base/threading/platform_thread.h"
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace base {
13 
TEST(SampleMetadataTest,ScopedSampleMetadata)14 TEST(SampleMetadataTest, ScopedSampleMetadata) {
15   MetadataRecorder::ItemArray items;
16   // TODO(https://crbug/1494111): Locate the other tests that are leaving items
17   // in MetadataRecorder and update them to clean up the state.
18   size_t initial_item_count =
19       MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
20                                          PlatformThread::CurrentId())
21           .GetItems(&items);
22 
23   {
24     ScopedSampleMetadata m("myname", 100, SampleMetadataScope::kProcess);
25 
26     ASSERT_EQ(initial_item_count + 1,
27               MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
28                                                  PlatformThread::CurrentId())
29                   .GetItems(&items));
30     EXPECT_EQ(HashMetricName("myname"), items[initial_item_count].name_hash);
31     EXPECT_FALSE(items[initial_item_count].key.has_value());
32     EXPECT_EQ(100, items[initial_item_count].value);
33   }
34 
35   ASSERT_EQ(initial_item_count,
36             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
37                                                PlatformThread::CurrentId())
38                 .GetItems(&items));
39 }
40 
TEST(SampleMetadataTest,ScopedSampleMetadataWithKey)41 TEST(SampleMetadataTest, ScopedSampleMetadataWithKey) {
42   MetadataRecorder::ItemArray items;
43   // TODO(https://crbug/1494111): Locate the other tests that are leaving items
44   // in MetadataRecorder and update them to clean up the state.
45   size_t initial_item_count =
46       MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
47                                          PlatformThread::CurrentId())
48           .GetItems(&items);
49 
50   {
51     ScopedSampleMetadata m("myname", 10, 100, SampleMetadataScope::kProcess);
52 
53     ASSERT_EQ(initial_item_count + 1,
54               MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
55                                                  PlatformThread::CurrentId())
56                   .GetItems(&items));
57     EXPECT_EQ(HashMetricName("myname"), items[initial_item_count].name_hash);
58     ASSERT_TRUE(items[initial_item_count].key.has_value());
59     EXPECT_EQ(10, *items[initial_item_count].key);
60     EXPECT_EQ(100, items[initial_item_count].value);
61   }
62 
63   ASSERT_EQ(initial_item_count,
64             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
65                                                PlatformThread::CurrentId())
66                 .GetItems(&items));
67 }
68 
TEST(SampleMetadataTest,SampleMetadata)69 TEST(SampleMetadataTest, SampleMetadata) {
70   MetadataRecorder::ItemArray items;
71   // TODO(https://crbug/1494111): Locate the other tests that are leaving items
72   // in MetadataRecorder and update them to clean up the state.
73   size_t initial_item_count =
74       MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
75                                          PlatformThread::CurrentId())
76           .GetItems(&items);
77 
78   SampleMetadata metadata("myname", SampleMetadataScope::kProcess);
79   metadata.Set(100);
80   ASSERT_EQ(initial_item_count + 1,
81             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
82                                                PlatformThread::CurrentId())
83                 .GetItems(&items));
84   EXPECT_EQ(HashMetricName("myname"), items[initial_item_count].name_hash);
85   EXPECT_FALSE(items[initial_item_count].key.has_value());
86   EXPECT_EQ(100, items[initial_item_count].value);
87 
88   metadata.Remove();
89   ASSERT_EQ(initial_item_count,
90             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
91                                                PlatformThread::CurrentId())
92                 .GetItems(&items));
93 }
94 
TEST(SampleMetadataTest,SampleMetadataWithKey)95 TEST(SampleMetadataTest, SampleMetadataWithKey) {
96   MetadataRecorder::ItemArray items;
97   // TODO(https://crbug/1494111): Locate the other tests that are leaving items
98   // in MetadataRecorder and update them to clean up the state.
99   size_t initial_item_count =
100       MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
101                                          PlatformThread::CurrentId())
102           .GetItems(&items);
103 
104   SampleMetadata metadata("myname", SampleMetadataScope::kProcess);
105   metadata.Set(10, 100);
106   ASSERT_EQ(initial_item_count + 1,
107             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
108                                                PlatformThread::CurrentId())
109                 .GetItems(&items));
110   EXPECT_EQ(HashMetricName("myname"), items[initial_item_count].name_hash);
111   ASSERT_TRUE(items[initial_item_count].key.has_value());
112   EXPECT_EQ(10, *items[initial_item_count].key);
113   EXPECT_EQ(100, items[initial_item_count].value);
114 
115   metadata.Remove(10);
116   ASSERT_EQ(initial_item_count,
117             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
118                                                PlatformThread::CurrentId())
119                 .GetItems(&items));
120 }
121 
TEST(SampleMetadataTest,SampleMetadataWithThreadId)122 TEST(SampleMetadataTest, SampleMetadataWithThreadId) {
123   MetadataRecorder::ItemArray items;
124   // TODO(https://crbug/1494111): Locate the other tests that are leaving items
125   // in MetadataRecorder and update them to clean up the state.
126   size_t initial_item_count =
127       MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
128                                          PlatformThread::CurrentId())
129           .GetItems(&items);
130 
131   SampleMetadata metadata("myname", SampleMetadataScope::kThread);
132   metadata.Set(100);
133   ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
134                                                    kInvalidThreadId)
135                     .GetItems(&items));
136   ASSERT_EQ(initial_item_count + 1,
137             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
138                                                PlatformThread::CurrentId())
139                 .GetItems(&items));
140   EXPECT_EQ(HashMetricName("myname"), items[initial_item_count].name_hash);
141   EXPECT_FALSE(items[initial_item_count].key.has_value());
142   EXPECT_EQ(100, items[initial_item_count].value);
143 
144   metadata.Remove();
145   ASSERT_EQ(initial_item_count,
146             MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
147                                                PlatformThread::CurrentId())
148                 .GetItems(&items));
149 }
150 
151 }  // namespace base
152