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.
5 #include "base/profiler/sample_metadata.h"
7 #include "base/metrics/metrics_hashes.h"
8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(SampleMetadataTest, ScopedSampleMetadata) {
14 MetadataRecorder::ItemArray items;
15 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
16 PlatformThread::CurrentId())
20 ScopedSampleMetadata m("myname", 100, SampleMetadataScope::kProcess);
22 ASSERT_EQ(1u, MetadataRecorder::MetadataProvider(
23 GetSampleMetadataRecorder(), PlatformThread::CurrentId())
25 EXPECT_EQ(HashMetricName("myname"), items[0].name_hash);
26 EXPECT_FALSE(items[0].key.has_value());
27 EXPECT_EQ(100, items[0].value);
30 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
31 PlatformThread::CurrentId())
35 TEST(SampleMetadataTest, ScopedSampleMetadataWithKey) {
36 MetadataRecorder::ItemArray items;
37 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
38 PlatformThread::CurrentId())
42 ScopedSampleMetadata m("myname", 10, 100, SampleMetadataScope::kProcess);
44 ASSERT_EQ(1u, MetadataRecorder::MetadataProvider(
45 GetSampleMetadataRecorder(), PlatformThread::CurrentId())
47 EXPECT_EQ(HashMetricName("myname"), items[0].name_hash);
48 ASSERT_TRUE(items[0].key.has_value());
49 EXPECT_EQ(10, *items[0].key);
50 EXPECT_EQ(100, items[0].value);
53 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
54 PlatformThread::CurrentId())
58 TEST(SampleMetadataTest, SampleMetadata) {
59 MetadataRecorder::ItemArray items;
60 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
61 PlatformThread::CurrentId())
64 SampleMetadata metadata("myname", SampleMetadataScope::kProcess);
66 ASSERT_EQ(1u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
67 PlatformThread::CurrentId())
69 EXPECT_EQ(HashMetricName("myname"), items[0].name_hash);
70 EXPECT_FALSE(items[0].key.has_value());
71 EXPECT_EQ(100, items[0].value);
74 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
75 PlatformThread::CurrentId())
79 TEST(SampleMetadataTest, SampleMetadataWithKey) {
80 MetadataRecorder::ItemArray items;
81 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
82 PlatformThread::CurrentId())
85 SampleMetadata metadata("myname", SampleMetadataScope::kProcess);
86 metadata.Set(10, 100);
87 ASSERT_EQ(1u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
88 PlatformThread::CurrentId())
90 EXPECT_EQ(HashMetricName("myname"), items[0].name_hash);
91 ASSERT_TRUE(items[0].key.has_value());
92 EXPECT_EQ(10, *items[0].key);
93 EXPECT_EQ(100, items[0].value);
96 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
97 PlatformThread::CurrentId())
101 TEST(SampleMetadataTest, SampleMetadataWithThreadId) {
102 MetadataRecorder::ItemArray items;
103 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
104 PlatformThread::CurrentId())
107 SampleMetadata metadata("myname", SampleMetadataScope::kThread);
109 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
112 ASSERT_EQ(1u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
113 PlatformThread::CurrentId())
115 EXPECT_EQ(HashMetricName("myname"), items[0].name_hash);
116 EXPECT_FALSE(items[0].key.has_value());
117 EXPECT_EQ(100, items[0].value);
120 ASSERT_EQ(0u, MetadataRecorder::MetadataProvider(GetSampleMetadataRecorder(),
121 PlatformThread::CurrentId())