Home | History | Annotate | Download | only in profiler
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      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 "components/metrics/profiler/profiler_metrics_provider.h"
      6 
      7 #include "base/tracked_objects.h"
      8 #include "components/metrics/metrics_hashes.h"
      9 #include "content/public/common/process_type.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 using tracked_objects::ProcessDataSnapshot;
     13 using tracked_objects::TaskSnapshot;
     14 
     15 namespace metrics {
     16 
     17 TEST(ProfilerMetricsProviderTest, RecordData) {
     18   // WARNING: If you broke the below check, you've modified how
     19   // HashMetricName works. Please also modify all server-side code that
     20   // relies on the existing way of hashing.
     21   EXPECT_EQ(GG_UINT64_C(1518842999910132863), HashMetricName("birth_thread*"));
     22 
     23   ProfilerMetricsProvider profiler_metrics_provider;
     24 
     25   {
     26     // Add data from the browser process.
     27     ProcessDataSnapshot process_data;
     28     process_data.process_id = 177;
     29     process_data.tasks.push_back(TaskSnapshot());
     30     process_data.tasks.back().birth.location.file_name = "a/b/file.h";
     31     process_data.tasks.back().birth.location.function_name = "function";
     32     process_data.tasks.back().birth.location.line_number = 1337;
     33     process_data.tasks.back().birth.thread_name = "birth_thread";
     34     process_data.tasks.back().death_data.count = 37;
     35     process_data.tasks.back().death_data.run_duration_sum = 31;
     36     process_data.tasks.back().death_data.run_duration_max = 17;
     37     process_data.tasks.back().death_data.run_duration_sample = 13;
     38     process_data.tasks.back().death_data.queue_duration_sum = 8;
     39     process_data.tasks.back().death_data.queue_duration_max = 5;
     40     process_data.tasks.back().death_data.queue_duration_sample = 3;
     41     process_data.tasks.back().death_thread_name = "Still_Alive";
     42     process_data.tasks.push_back(TaskSnapshot());
     43     process_data.tasks.back().birth.location.file_name = "c\\d\\file2";
     44     process_data.tasks.back().birth.location.function_name = "function2";
     45     process_data.tasks.back().birth.location.line_number = 1773;
     46     process_data.tasks.back().birth.thread_name = "birth_thread2";
     47     process_data.tasks.back().death_data.count = 19;
     48     process_data.tasks.back().death_data.run_duration_sum = 23;
     49     process_data.tasks.back().death_data.run_duration_max = 11;
     50     process_data.tasks.back().death_data.run_duration_sample = 7;
     51     process_data.tasks.back().death_data.queue_duration_sum = 0;
     52     process_data.tasks.back().death_data.queue_duration_max = 0;
     53     process_data.tasks.back().death_data.queue_duration_sample = 0;
     54     process_data.tasks.back().death_thread_name = "death_thread";
     55 
     56     profiler_metrics_provider.RecordProfilerData(process_data,
     57                                                  content::PROCESS_TYPE_BROWSER);
     58   }
     59 
     60   {
     61     // Add data from a renderer process.
     62     ProcessDataSnapshot process_data;
     63     process_data.process_id = 1177;
     64     process_data.tasks.push_back(TaskSnapshot());
     65     process_data.tasks.back().birth.location.file_name = "file3";
     66     process_data.tasks.back().birth.location.function_name = "function3";
     67     process_data.tasks.back().birth.location.line_number = 7331;
     68     process_data.tasks.back().birth.thread_name = "birth_thread3";
     69     process_data.tasks.back().death_data.count = 137;
     70     process_data.tasks.back().death_data.run_duration_sum = 131;
     71     process_data.tasks.back().death_data.run_duration_max = 117;
     72     process_data.tasks.back().death_data.run_duration_sample = 113;
     73     process_data.tasks.back().death_data.queue_duration_sum = 108;
     74     process_data.tasks.back().death_data.queue_duration_max = 105;
     75     process_data.tasks.back().death_data.queue_duration_sample = 103;
     76     process_data.tasks.back().death_thread_name = "death_thread3";
     77     process_data.tasks.push_back(TaskSnapshot());
     78     process_data.tasks.back().birth.location.file_name = "";
     79     process_data.tasks.back().birth.location.function_name = "";
     80     process_data.tasks.back().birth.location.line_number = 7332;
     81     process_data.tasks.back().birth.thread_name = "";
     82     process_data.tasks.back().death_data.count = 138;
     83     process_data.tasks.back().death_data.run_duration_sum = 132;
     84     process_data.tasks.back().death_data.run_duration_max = 118;
     85     process_data.tasks.back().death_data.run_duration_sample = 114;
     86     process_data.tasks.back().death_data.queue_duration_sum = 109;
     87     process_data.tasks.back().death_data.queue_duration_max = 106;
     88     process_data.tasks.back().death_data.queue_duration_sample = 104;
     89     process_data.tasks.back().death_thread_name = "";
     90 
     91     profiler_metrics_provider.RecordProfilerData(
     92         process_data, content::PROCESS_TYPE_RENDERER);
     93 
     94     // Capture the data and verify that it is as expected.
     95     ChromeUserMetricsExtension uma_proto;
     96     profiler_metrics_provider.ProvideGeneralMetrics(&uma_proto);
     97 
     98     ASSERT_EQ(1, uma_proto.profiler_event_size());
     99     EXPECT_EQ(ProfilerEventProto::STARTUP_PROFILE,
    100               uma_proto.profiler_event(0).profile_type());
    101     EXPECT_EQ(ProfilerEventProto::WALL_CLOCK_TIME,
    102               uma_proto.profiler_event(0).time_source());
    103     ASSERT_EQ(4, uma_proto.profiler_event(0).tracked_object_size());
    104 
    105     const ProfilerEventProto::TrackedObject* tracked_object =
    106         &uma_proto.profiler_event(0).tracked_object(0);
    107     EXPECT_EQ(HashMetricName("file.h"),
    108               tracked_object->source_file_name_hash());
    109     EXPECT_EQ(HashMetricName("function"),
    110               tracked_object->source_function_name_hash());
    111     EXPECT_EQ(1337, tracked_object->source_line_number());
    112     EXPECT_EQ(HashMetricName("birth_thread"),
    113               tracked_object->birth_thread_name_hash());
    114     EXPECT_EQ(37, tracked_object->exec_count());
    115     EXPECT_EQ(31, tracked_object->exec_time_total());
    116     EXPECT_EQ(13, tracked_object->exec_time_sampled());
    117     EXPECT_EQ(8, tracked_object->queue_time_total());
    118     EXPECT_EQ(3, tracked_object->queue_time_sampled());
    119     EXPECT_EQ(HashMetricName("Still_Alive"),
    120               tracked_object->exec_thread_name_hash());
    121     EXPECT_EQ(177U, tracked_object->process_id());
    122     EXPECT_EQ(ProfilerEventProto::TrackedObject::BROWSER,
    123               tracked_object->process_type());
    124 
    125     tracked_object = &uma_proto.profiler_event(0).tracked_object(1);
    126     EXPECT_EQ(HashMetricName("file2"),
    127               tracked_object->source_file_name_hash());
    128     EXPECT_EQ(HashMetricName("function2"),
    129               tracked_object->source_function_name_hash());
    130     EXPECT_EQ(1773, tracked_object->source_line_number());
    131     EXPECT_EQ(HashMetricName("birth_thread*"),
    132               tracked_object->birth_thread_name_hash());
    133     EXPECT_EQ(19, tracked_object->exec_count());
    134     EXPECT_EQ(23, tracked_object->exec_time_total());
    135     EXPECT_EQ(7, tracked_object->exec_time_sampled());
    136     EXPECT_EQ(0, tracked_object->queue_time_total());
    137     EXPECT_EQ(0, tracked_object->queue_time_sampled());
    138     EXPECT_EQ(HashMetricName("death_thread"),
    139               tracked_object->exec_thread_name_hash());
    140     EXPECT_EQ(177U, tracked_object->process_id());
    141     EXPECT_EQ(ProfilerEventProto::TrackedObject::BROWSER,
    142               tracked_object->process_type());
    143 
    144     tracked_object = &uma_proto.profiler_event(0).tracked_object(2);
    145     EXPECT_EQ(HashMetricName("file3"),
    146               tracked_object->source_file_name_hash());
    147     EXPECT_EQ(HashMetricName("function3"),
    148               tracked_object->source_function_name_hash());
    149     EXPECT_EQ(7331, tracked_object->source_line_number());
    150     EXPECT_EQ(HashMetricName("birth_thread*"),
    151               tracked_object->birth_thread_name_hash());
    152     EXPECT_EQ(137, tracked_object->exec_count());
    153     EXPECT_EQ(131, tracked_object->exec_time_total());
    154     EXPECT_EQ(113, tracked_object->exec_time_sampled());
    155     EXPECT_EQ(108, tracked_object->queue_time_total());
    156     EXPECT_EQ(103, tracked_object->queue_time_sampled());
    157     EXPECT_EQ(HashMetricName("death_thread*"),
    158               tracked_object->exec_thread_name_hash());
    159     EXPECT_EQ(1177U, tracked_object->process_id());
    160     EXPECT_EQ(ProfilerEventProto::TrackedObject::RENDERER,
    161               tracked_object->process_type());
    162 
    163     tracked_object = &uma_proto.profiler_event(0).tracked_object(3);
    164     EXPECT_EQ(HashMetricName(""),
    165               tracked_object->source_file_name_hash());
    166     EXPECT_EQ(HashMetricName(""),
    167               tracked_object->source_function_name_hash());
    168     EXPECT_EQ(7332, tracked_object->source_line_number());
    169     EXPECT_EQ(HashMetricName(""),
    170               tracked_object->birth_thread_name_hash());
    171     EXPECT_EQ(138, tracked_object->exec_count());
    172     EXPECT_EQ(132, tracked_object->exec_time_total());
    173     EXPECT_EQ(114, tracked_object->exec_time_sampled());
    174     EXPECT_EQ(109, tracked_object->queue_time_total());
    175     EXPECT_EQ(104, tracked_object->queue_time_sampled());
    176     EXPECT_EQ(HashMetricName(""),
    177               tracked_object->exec_thread_name_hash());
    178     EXPECT_EQ(ProfilerEventProto::TrackedObject::RENDERER,
    179               tracked_object->process_type());
    180   }
    181 }
    182 
    183 }  // namespace metrics
    184