Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromecast/base/metrics/cast_metrics_helper.h" | 5 #include "chromecast/base/metrics/cast_metrics_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/metrics/user_metrics.h" | 12 #include "base/metrics/user_metrics.h" |
| 13 #include "base/strings/string_split.h" | |
| 14 #include "base/strings/string_util.h" | |
| 13 #include "chromecast/base/metrics/cast_histograms.h" | 15 #include "chromecast/base/metrics/cast_histograms.h" |
| 14 #include "chromecast/base/metrics/grouped_histogram.h" | 16 #include "chromecast/base/metrics/grouped_histogram.h" |
| 15 | 17 |
| 16 namespace chromecast { | 18 namespace chromecast { |
| 17 namespace metrics { | 19 namespace metrics { |
| 18 | 20 |
| 19 // A useful macro to make sure current member function runs on the valid thread. | 21 // A useful macro to make sure current member function runs on the valid thread. |
| 20 #define MAKE_SURE_THREAD(callback, ...) \ | 22 #define MAKE_SURE_THREAD(callback, ...) \ |
| 21 if (!message_loop_proxy_->BelongsToCurrentThread()) { \ | 23 if (!message_loop_proxy_->BelongsToCurrentThread()) { \ |
| 22 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( \ | 24 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( \ |
| 23 &CastMetricsHelper::callback, \ | 25 &CastMetricsHelper::callback, \ |
| 24 base::Unretained(this), ##__VA_ARGS__)); \ | 26 base::Unretained(this), ##__VA_ARGS__)); \ |
| 25 return; \ | 27 return; \ |
| 26 } | 28 } |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 CastMetricsHelper* g_instance = NULL; | 32 CastMetricsHelper* g_instance = NULL; |
| 31 | 33 |
| 32 // Displayed frames are logged in frames per second (but sampling can be over | 34 // Displayed frames are logged in frames per second (but sampling can be over |
| 33 // a longer period of time, e.g. 5 seconds). | 35 // a longer period of time, e.g. 5 seconds). |
| 34 const int kDisplayedFramesPerSecondPeriod = 1000000; | 36 const int kDisplayedFramesPerSecondPeriod = 1000000; |
| 35 | 37 |
| 36 // Sample every 5 seconds, represented in microseconds. | 38 // Sample every 5 seconds, represented in microseconds. |
| 37 const int kNominalVideoSamplePeriod = 5000000; | 39 const int kNominalVideoSamplePeriod = 5000000; |
| 38 | 40 |
| 41 const char kMetricsNameAppInfoDelimiter = '#'; | |
| 42 | |
| 39 } // namespace | 43 } // namespace |
| 40 | 44 |
| 45 // static | |
| 46 | |
| 47 // NOTE(gfhuang): This is a hacky way to encode/decode app infos into a | |
| 48 // string. Mainly because it's hard to add another metrics serialization type | |
| 49 // into components/metrics/serialization/. | |
| 50 // static | |
| 51 bool CastMetricsHelper::DecodeAppInfoFromMetricsName( | |
| 52 const std::string& metrics_name, | |
| 53 std::string* action_name, | |
| 54 std::string* app_id, | |
| 55 std::string* session_id, | |
| 56 std::string* sdk_version) { | |
|
byungchul
2014/12/11 18:35:49
DCHECK()'s for pointers.
gfhuang
2014/12/11 19:20:26
Done.
| |
| 57 if (metrics_name.find(kMetricsNameAppInfoDelimiter) == std::string::npos) { | |
| 58 return false; | |
| 59 } | |
|
byungchul
2014/12/11 18:35:49
remove {}
gfhuang
2014/12/11 19:20:26
Done.
| |
| 60 std::vector<std::string> tokens; | |
| 61 base::SplitString(metrics_name, kMetricsNameAppInfoDelimiter, &tokens); | |
| 62 DCHECK_EQ(tokens.size(), 4u); | |
|
lcwu1
2014/12/11 18:41:57
If the number of the tokens is not 4, which means
gfhuang
2014/12/11 19:20:26
If the metrics name has magic char '#', but tokens
| |
| 63 // The order of tokens should match EncodeAppInfoIntoMetricsName(). | |
| 64 *action_name = tokens[0]; | |
| 65 *app_id = tokens[1]; | |
| 66 *session_id = tokens[2]; | |
| 67 *sdk_version = tokens[3]; | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 // static | |
| 72 std::string CastMetricsHelper::EncodeAppInfoIntoMetricsName( | |
| 73 const std::string& action_name, | |
| 74 const std::string& app_id, | |
| 75 const std::string& session_id, | |
| 76 const std::string& sdk_version) { | |
| 77 std::vector<std::string> parts; | |
| 78 parts.push_back(action_name); | |
| 79 parts.push_back(app_id); | |
| 80 parts.push_back(session_id); | |
| 81 parts.push_back(sdk_version); | |
| 82 return JoinString(parts, kMetricsNameAppInfoDelimiter); | |
| 83 } | |
| 84 | |
| 85 // static | |
| 41 CastMetricsHelper* CastMetricsHelper::GetInstance() { | 86 CastMetricsHelper* CastMetricsHelper::GetInstance() { |
| 42 DCHECK(g_instance); | 87 DCHECK(g_instance); |
| 43 return g_instance; | 88 return g_instance; |
| 44 } | 89 } |
| 45 | 90 |
| 46 CastMetricsHelper::CastMetricsHelper( | 91 CastMetricsHelper::CastMetricsHelper( |
| 47 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) | 92 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) |
| 48 : message_loop_proxy_(message_loop_proxy), | 93 : message_loop_proxy_(message_loop_proxy), |
| 49 metrics_sink_(NULL), | 94 metrics_sink_(NULL), |
| 50 record_action_callback_(base::Bind(&base::RecordComputedAction)) { | 95 record_action_callback_(base::Bind(&base::RecordComputedAction)) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 64 g_instance = NULL; | 109 g_instance = NULL; |
| 65 } | 110 } |
| 66 | 111 |
| 67 void CastMetricsHelper::TagAppStart(const std::string& arg_app_name) { | 112 void CastMetricsHelper::TagAppStart(const std::string& arg_app_name) { |
| 68 MAKE_SURE_THREAD(TagAppStart, arg_app_name); | 113 MAKE_SURE_THREAD(TagAppStart, arg_app_name); |
| 69 app_name_ = arg_app_name; | 114 app_name_ = arg_app_name; |
| 70 app_start_time_ = base::TimeTicks::Now(); | 115 app_start_time_ = base::TimeTicks::Now(); |
| 71 new_startup_time_ = true; | 116 new_startup_time_ = true; |
| 72 | 117 |
| 73 TagAppStartForGroupedHistograms(app_name_); | 118 TagAppStartForGroupedHistograms(app_name_); |
| 119 // Clear app info | |
| 120 UpdateCurrentAppInfo("", "", ""); | |
| 121 } | |
| 122 | |
| 123 void CastMetricsHelper::UpdateCurrentAppInfo(const std::string& app_id, | |
| 124 const std::string& session_id, | |
| 125 const std::string& sdk_version) { | |
| 126 MAKE_SURE_THREAD(UpdateCurrentAppInfo, app_id, session_id, sdk_version); | |
| 127 app_id_ = app_id; | |
| 128 session_id_ = session_id; | |
| 129 sdk_version_ = sdk_version; | |
| 74 } | 130 } |
| 75 | 131 |
| 76 void CastMetricsHelper::LogMediaPlay() { | 132 void CastMetricsHelper::LogMediaPlay() { |
| 77 RecordSimpleAction(GetMetricsNameWithAppName("MediaPlay", "")); | 133 MAKE_SURE_THREAD(LogMediaPlay); |
| 134 RecordSimpleAction(EncodeAppInfoIntoMetricsName( | |
| 135 "MediaPlay", app_id_, session_id_, sdk_version_)); | |
| 78 } | 136 } |
| 79 | 137 |
| 80 void CastMetricsHelper::LogMediaPause() { | 138 void CastMetricsHelper::LogMediaPause() { |
| 81 RecordSimpleAction(GetMetricsNameWithAppName("MediaPause", "")); | 139 MAKE_SURE_THREAD(LogMediaPause); |
| 140 RecordSimpleAction(EncodeAppInfoIntoMetricsName( | |
| 141 "MediaPause", app_id_, session_id_, sdk_version_)); | |
| 82 } | 142 } |
| 83 | 143 |
| 84 void CastMetricsHelper::LogTimeToDisplayVideo() { | 144 void CastMetricsHelper::LogTimeToDisplayVideo() { |
| 85 if (!new_startup_time_) { // For faster check. | 145 if (!new_startup_time_) { // For faster check. |
| 86 return; | 146 return; |
| 87 } | 147 } |
| 88 MAKE_SURE_THREAD(LogTimeToDisplayVideo); | 148 MAKE_SURE_THREAD(LogTimeToDisplayVideo); |
| 89 new_startup_time_ = false; | 149 new_startup_time_ = false; |
| 90 base::TimeDelta launch_time = base::TimeTicks::Now() - app_start_time_; | 150 base::TimeDelta launch_time = base::TimeTicks::Now() - app_start_time_; |
| 91 const std::string uma_name(GetMetricsNameWithAppName("Startup", | 151 const std::string uma_name(GetMetricsNameWithAppName("Startup", |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 const base::TimeDelta& value) { | 300 const base::TimeDelta& value) { |
| 241 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. | 301 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. |
| 242 LogTimeHistogramEvent(name, value, | 302 LogTimeHistogramEvent(name, value, |
| 243 base::TimeDelta::FromMilliseconds(10), | 303 base::TimeDelta::FromMilliseconds(10), |
| 244 base::TimeDelta::FromMinutes(3), | 304 base::TimeDelta::FromMinutes(3), |
| 245 50); | 305 50); |
| 246 } | 306 } |
| 247 | 307 |
| 248 } // namespace metrics | 308 } // namespace metrics |
| 249 } // namespace chromecast | 309 } // namespace chromecast |
| OLD | NEW |