Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "chrome/browser/mac/mac_startup_profiler.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 | |
| 10 // static | |
| 11 MacStartupProfiler* MacStartupProfiler::GetInstance() { | |
| 12 return Singleton<MacStartupProfiler>::get(); | |
| 13 } | |
| 14 | |
| 15 MacStartupProfiler::MacStartupProfiler() : recorded_metrics_(false) { | |
| 16 } | |
| 17 | |
| 18 MacStartupProfiler::~MacStartupProfiler() { | |
| 19 } | |
| 20 | |
| 21 void MacStartupProfiler::RecordLaunchTime() { | |
| 22 launch_time_ = base::Time::Now(); | |
| 23 } | |
| 24 | |
| 25 void MacStartupProfiler::Profile(Location location) { | |
| 26 DCHECK(!launch_time_.is_null()); | |
| 27 profiled_times_[location] = base::Time::Now(); | |
| 28 } | |
| 29 | |
| 30 void MacStartupProfiler::RecordMetrics() { | |
| 31 DCHECK(!launch_time_.is_null()); | |
| 32 DCHECK(!recorded_metrics_); | |
| 33 | |
| 34 recorded_metrics_ = true; | |
| 35 | |
| 36 for (std::map<Location, base::Time>::const_iterator it = | |
| 37 profiled_times_.begin(); | |
| 38 it != profiled_times_.end(); | |
| 39 ++it) { | |
| 40 const base::Time& location_time = it->second; | |
| 41 base::TimeDelta delta = location_time - launch_time_; | |
| 42 RecordHistogram(it->first, delta); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 const std::string MacStartupProfiler::HistogramName(Location location) { | |
| 47 std::string base("OSX.Startup."); | |
|
Ilya Sherman
2014/07/15 22:42:00
Optional nit: Perhaps |prefix| would be a slightly
erikchen
2014/07/15 23:04:39
Done.
| |
| 48 const char* extension; | |
|
Ilya Sherman
2014/07/15 22:42:00
Hmm, why did you decide to declare a variable for
erikchen
2014/07/15 23:04:39
Good question. I changed to the previous pattern.
| |
| 49 switch (location) { | |
| 50 case PRE_MAIN_MESSAGE_LOOP_START: | |
| 51 extension = "PreMainMessageLoopStart"; | |
| 52 break; | |
| 53 case AWAKE_FROM_NIB: | |
| 54 extension = "AwakeFromNib"; | |
| 55 break; | |
| 56 case POST_MAIN_MESSAGE_LOOP_START: | |
| 57 extension = "PostMainMessageLoopStart"; | |
| 58 break; | |
| 59 case PRE_PROFILE_INIT: | |
| 60 extension = "PreProfileInit"; | |
| 61 break; | |
| 62 case POST_PROFILE_INIT: | |
| 63 extension = "PostProfileInit"; | |
| 64 break; | |
| 65 case WILL_FINISH_LAUNCHING: | |
| 66 extension = "WillFinishLaunching"; | |
| 67 break; | |
| 68 case DID_FINISH_LAUNCHING: | |
| 69 extension = "DidFinishLaunching"; | |
| 70 break; | |
| 71 } | |
| 72 base += extension; | |
| 73 return base; | |
|
Ilya Sherman
2014/07/15 22:42:00
nit: If you keep this structure, please shorten th
erikchen
2014/07/15 23:04:39
N/A after previous comment.
| |
| 74 } | |
| 75 | |
| 76 void MacStartupProfiler::RecordHistogram(Location location, | |
| 77 const base::TimeDelta& delta) { | |
| 78 const char* name = HistogramName(location).c_str(); | |
|
Ilya Sherman
2014/07/15 22:42:00
nit: Why are you extracting out the C-string? Fac
erikchen
2014/07/15 23:04:39
Right you are.
| |
| 79 base::TimeDelta min = base::TimeDelta::FromMilliseconds(10); | |
| 80 base::TimeDelta max = base::TimeDelta::FromMinutes(1); | |
| 81 int bucket_count = 100; | |
| 82 | |
| 83 // No need to cache the histogram pointers, since each invocation of this | |
| 84 // method will be the first and only usage of a histogram with that given | |
| 85 // name. | |
| 86 base::HistogramBase* histogram = base::Histogram::FactoryTimeGet( | |
| 87 name, min, max, bucket_count, base::HistogramBase::kNoFlags); | |
| 88 histogram->AddTime(delta); | |
| 89 } | |
| OLD | NEW |