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) { |
| 57 DCHECK(action_name); |
| 58 DCHECK(app_id); |
| 59 DCHECK(session_id); |
| 60 DCHECK(sdk_version); |
| 61 if (metrics_name.find(kMetricsNameAppInfoDelimiter) == std::string::npos) |
| 62 return false; |
| 63 |
| 64 std::vector<std::string> tokens; |
| 65 base::SplitString(metrics_name, kMetricsNameAppInfoDelimiter, &tokens); |
| 66 DCHECK_EQ(tokens.size(), 4u); |
| 67 // The order of tokens should match EncodeAppInfoIntoMetricsName(). |
| 68 *action_name = tokens[0]; |
| 69 *app_id = tokens[1]; |
| 70 *session_id = tokens[2]; |
| 71 *sdk_version = tokens[3]; |
| 72 return true; |
| 73 } |
| 74 |
| 75 // static |
| 76 std::string CastMetricsHelper::EncodeAppInfoIntoMetricsName( |
| 77 const std::string& action_name, |
| 78 const std::string& app_id, |
| 79 const std::string& session_id, |
| 80 const std::string& sdk_version) { |
| 81 std::vector<std::string> parts; |
| 82 parts.push_back(action_name); |
| 83 parts.push_back(app_id); |
| 84 parts.push_back(session_id); |
| 85 parts.push_back(sdk_version); |
| 86 return JoinString(parts, kMetricsNameAppInfoDelimiter); |
| 87 } |
| 88 |
| 89 // static |
41 CastMetricsHelper* CastMetricsHelper::GetInstance() { | 90 CastMetricsHelper* CastMetricsHelper::GetInstance() { |
42 DCHECK(g_instance); | 91 DCHECK(g_instance); |
43 return g_instance; | 92 return g_instance; |
44 } | 93 } |
45 | 94 |
46 CastMetricsHelper::CastMetricsHelper( | 95 CastMetricsHelper::CastMetricsHelper( |
47 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) | 96 scoped_refptr<base::MessageLoopProxy> message_loop_proxy) |
48 : message_loop_proxy_(message_loop_proxy), | 97 : message_loop_proxy_(message_loop_proxy), |
49 metrics_sink_(NULL), | 98 metrics_sink_(NULL), |
50 record_action_callback_(base::Bind(&base::RecordComputedAction)) { | 99 record_action_callback_(base::Bind(&base::RecordComputedAction)) { |
(...skipping 13 matching lines...) Expand all Loading... |
64 g_instance = NULL; | 113 g_instance = NULL; |
65 } | 114 } |
66 | 115 |
67 void CastMetricsHelper::TagAppStart(const std::string& arg_app_name) { | 116 void CastMetricsHelper::TagAppStart(const std::string& arg_app_name) { |
68 MAKE_SURE_THREAD(TagAppStart, arg_app_name); | 117 MAKE_SURE_THREAD(TagAppStart, arg_app_name); |
69 app_name_ = arg_app_name; | 118 app_name_ = arg_app_name; |
70 app_start_time_ = base::TimeTicks::Now(); | 119 app_start_time_ = base::TimeTicks::Now(); |
71 new_startup_time_ = true; | 120 new_startup_time_ = true; |
72 | 121 |
73 TagAppStartForGroupedHistograms(app_name_); | 122 TagAppStartForGroupedHistograms(app_name_); |
| 123 // Clear app info |
| 124 UpdateCurrentAppInfo("", "", ""); |
| 125 } |
| 126 |
| 127 void CastMetricsHelper::UpdateCurrentAppInfo(const std::string& app_id, |
| 128 const std::string& session_id, |
| 129 const std::string& sdk_version) { |
| 130 MAKE_SURE_THREAD(UpdateCurrentAppInfo, app_id, session_id, sdk_version); |
| 131 app_id_ = app_id; |
| 132 session_id_ = session_id; |
| 133 sdk_version_ = sdk_version; |
74 } | 134 } |
75 | 135 |
76 void CastMetricsHelper::LogMediaPlay() { | 136 void CastMetricsHelper::LogMediaPlay() { |
77 RecordSimpleAction(GetMetricsNameWithAppName("MediaPlay", "")); | 137 MAKE_SURE_THREAD(LogMediaPlay); |
| 138 RecordSimpleAction(EncodeAppInfoIntoMetricsName( |
| 139 "MediaPlay", app_id_, session_id_, sdk_version_)); |
78 } | 140 } |
79 | 141 |
80 void CastMetricsHelper::LogMediaPause() { | 142 void CastMetricsHelper::LogMediaPause() { |
81 RecordSimpleAction(GetMetricsNameWithAppName("MediaPause", "")); | 143 MAKE_SURE_THREAD(LogMediaPause); |
| 144 RecordSimpleAction(EncodeAppInfoIntoMetricsName( |
| 145 "MediaPause", app_id_, session_id_, sdk_version_)); |
82 } | 146 } |
83 | 147 |
84 void CastMetricsHelper::LogTimeToDisplayVideo() { | 148 void CastMetricsHelper::LogTimeToDisplayVideo() { |
85 if (!new_startup_time_) { // For faster check. | 149 if (!new_startup_time_) { // For faster check. |
86 return; | 150 return; |
87 } | 151 } |
88 MAKE_SURE_THREAD(LogTimeToDisplayVideo); | 152 MAKE_SURE_THREAD(LogTimeToDisplayVideo); |
89 new_startup_time_ = false; | 153 new_startup_time_ = false; |
90 base::TimeDelta launch_time = base::TimeTicks::Now() - app_start_time_; | 154 base::TimeDelta launch_time = base::TimeTicks::Now() - app_start_time_; |
91 const std::string uma_name(GetMetricsNameWithAppName("Startup", | 155 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) { | 304 const base::TimeDelta& value) { |
241 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. | 305 // Follow UMA_HISTOGRAM_MEDIUM_TIMES definition. |
242 LogTimeHistogramEvent(name, value, | 306 LogTimeHistogramEvent(name, value, |
243 base::TimeDelta::FromMilliseconds(10), | 307 base::TimeDelta::FromMilliseconds(10), |
244 base::TimeDelta::FromMinutes(3), | 308 base::TimeDelta::FromMinutes(3), |
245 50); | 309 50); |
246 } | 310 } |
247 | 311 |
248 } // namespace metrics | 312 } // namespace metrics |
249 } // namespace chromecast | 313 } // namespace chromecast |
OLD | NEW |