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.
5 #include "chrome/browser/mac/mac_startup_profiler.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "components/startup_metric_utils/startup_metric_utils.h"
12 MacStartupProfiler
* MacStartupProfiler::GetInstance() {
13 return Singleton
<MacStartupProfiler
>::get();
16 MacStartupProfiler::MacStartupProfiler() : recorded_metrics_(false) {
19 MacStartupProfiler::~MacStartupProfiler() {
22 void MacStartupProfiler::Profile(Location location
) {
23 profiled_times_
[location
] = base::Time::Now();
26 void MacStartupProfiler::RecordMetrics() {
27 const base::Time
* main_entry_time
=
28 startup_metric_utils::MainEntryPointTime();
29 DCHECK(main_entry_time
);
30 DCHECK(!recorded_metrics_
);
32 recorded_metrics_
= true;
34 for (std::map
<Location
, base::Time
>::const_iterator it
=
35 profiled_times_
.begin();
36 it
!= profiled_times_
.end();
38 const base::Time
& location_time
= it
->second
;
39 base::TimeDelta delta
= location_time
- *main_entry_time
;
40 RecordHistogram(it
->first
, delta
);
44 const std::string
MacStartupProfiler::HistogramName(Location location
) {
45 std::string
prefix("Startup.OSX.");
47 case PRE_MAIN_MESSAGE_LOOP_START
:
48 return prefix
+ "PreMainMessageLoopStart";
50 return prefix
+ "AwakeFromNib";
51 case POST_MAIN_MESSAGE_LOOP_START
:
52 return prefix
+ "PostMainMessageLoopStart";
53 case PRE_PROFILE_INIT
:
54 return prefix
+ "PreProfileInit";
55 case POST_PROFILE_INIT
:
56 return prefix
+ "PostProfileInit";
57 case WILL_FINISH_LAUNCHING
:
58 return prefix
+ "WillFinishLaunching";
59 case DID_FINISH_LAUNCHING
:
60 return prefix
+ "DockIconWillFinishBouncing";
64 void MacStartupProfiler::RecordHistogram(Location location
,
65 const base::TimeDelta
& delta
) {
66 const std::string
name(HistogramName(location
));
67 base::TimeDelta min
= base::TimeDelta::FromMilliseconds(10);
68 base::TimeDelta max
= base::TimeDelta::FromMinutes(1);
69 int bucket_count
= 100;
71 // No need to cache the histogram pointers, since each invocation of this
72 // method will be the first and only usage of a histogram with that given
74 base::HistogramBase
* histogram
= base::Histogram::FactoryTimeGet(
79 base::HistogramBase::kUmaTargetedHistogramFlag
);
80 histogram
->AddTime(delta
);