OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
6 * @fileoverview Utility methods for accessing chrome.metricsPrivate API. | 6 * @fileoverview Utility methods for accessing chrome.metricsPrivate API. |
7 * | 7 * |
8 * To be included as a first script in main.html | 8 * To be included as a first script in main.html |
9 */ | 9 */ |
10 | 10 |
11 (function() { | |
12 // Switch to the 'test harness' mode when loading from a file or http url. | |
13 // Do this as early as possible because the metrics code depends on | |
14 // chrome private APIs. | |
15 if (document.location.protocol == 'file:' || | |
16 document.location.protocol == 'http:') { | |
17 console.log('created mock script'); | |
18 document.write('<script src="js/mock_chrome.js"><\57script>'); | |
dgozman
2012/04/12 11:18:14
Oh, now I see.
| |
19 } | |
20 })(); | |
21 | |
11 var metrics = {}; | 22 var metrics = {}; |
12 | 23 |
24 /** | |
25 * A map from interval name to interval start timestamp. | |
26 */ | |
13 metrics.intervals = {}; | 27 metrics.intervals = {}; |
14 | 28 |
29 /** | |
30 * Start the named time interval. | |
31 * Should be followed by a call to recordInterval with the same name. | |
32 * | |
33 * @param {string} name Unique interval name. | |
34 */ | |
15 metrics.startInterval = function(name) { | 35 metrics.startInterval = function(name) { |
16 metrics.intervals[name] = Date.now(); | 36 metrics.intervals[name] = Date.now(); |
17 }; | 37 }; |
18 | 38 |
19 metrics.startInterval('Load.Total'); | 39 metrics.startInterval('Load.Total'); |
20 metrics.startInterval('Load.Script'); | 40 metrics.startInterval('Load.Script'); |
21 | 41 |
42 /** | |
43 * Convert a short metric name to the full format. | |
44 * | |
45 * @param {string} name Short metric name. | |
46 * @return {string} Full metric name. | |
47 * @private | |
48 */ | |
22 metrics.convertName_ = function(name) { | 49 metrics.convertName_ = function(name) { |
23 return 'FileBrowser.' + name; | 50 return 'FileBrowser.' + name; |
24 }; | 51 }; |
25 | 52 |
53 /** | |
54 * Create a decorator function that calls a chrome.metricsPrivate function | |
55 * with the same name and correct parameters. | |
56 * | |
57 * @param {string} name Method name. | |
58 */ | |
26 metrics.decorate = function(name) { | 59 metrics.decorate = function(name) { |
27 this[name] = function() { | 60 metrics[name] = function() { |
28 var args = Array.apply(null, arguments); | 61 var args = Array.apply(null, arguments); |
29 args[0] = metrics.convertName_(args[0]); | 62 args[0] = metrics.convertName_(args[0]); |
30 chrome.metricsPrivate[name].apply(chrome.metricsPrivate, args); | 63 chrome.metricsPrivate[name].apply(chrome.metricsPrivate, args); |
31 if (localStorage.logMetrics) { | 64 if (localStorage.logMetrics) { |
32 console.log('chrome.metricsPrivate.' + name, args); | 65 console.log('chrome.metricsPrivate.' + name, args); |
33 } | 66 } |
34 } | 67 } |
35 }; | 68 }; |
36 | 69 |
37 metrics.decorate('recordMediumCount'); | 70 metrics.decorate('recordMediumCount'); |
38 metrics.decorate('recordSmallCount'); | 71 metrics.decorate('recordSmallCount'); |
39 metrics.decorate('recordTime'); | 72 metrics.decorate('recordTime'); |
40 metrics.decorate('recordUserAction'); | 73 metrics.decorate('recordUserAction'); |
41 | 74 |
75 /** | |
76 * Complete the time interval recording. | |
77 * | |
78 * Should be preceded by a call to startInterval with the same name. * | |
79 * | |
80 * @param {string} name Unique interval name. | |
81 */ | |
42 metrics.recordInterval = function(name) { | 82 metrics.recordInterval = function(name) { |
43 if (name in metrics.intervals) { | 83 if (name in metrics.intervals) { |
44 metrics.recordTime(name, Date.now() - metrics.intervals[name]); | 84 metrics.recordTime(name, Date.now() - metrics.intervals[name]); |
45 } else { | 85 } else { |
46 console.error('Unknown interval: ' + name); | 86 console.error('Unknown interval: ' + name); |
47 } | 87 } |
48 }; | 88 }; |
49 | 89 |
90 /** | |
91 * Record an enum value. | |
92 * | |
93 * @param {string} name Metric name. | |
94 * @param {Object} value Enum value. | |
95 * @param {Array.<Object>|number} validValues Array of valid values | |
96 * or a boundary number value. | |
97 */ | |
50 metrics.recordEnum = function(name, value, validValues) { | 98 metrics.recordEnum = function(name, value, validValues) { |
51 var boundaryValue; | 99 var boundaryValue; |
52 var index; | 100 var index; |
53 if (validValues.constructor.name == 'Array') { | 101 if (validValues.constructor.name == 'Array') { |
54 index = validValues.indexOf(value); | 102 index = validValues.indexOf(value); |
55 boundaryValue = validValues.length; | 103 boundaryValue = validValues.length; |
56 } else { | 104 } else { |
57 index = value; | 105 index = value; |
58 boundaryValue = validValues; | 106 boundaryValue = validValues; |
59 } | 107 } |
(...skipping 11 matching lines...) Expand all Loading... | |
71 'min': 1, | 119 'min': 1, |
72 'max': boundaryValue, | 120 'max': boundaryValue, |
73 'buckets': boundaryValue + 1 | 121 'buckets': boundaryValue + 1 |
74 }; | 122 }; |
75 chrome.metricsPrivate.recordValue(metricDescr, index); | 123 chrome.metricsPrivate.recordValue(metricDescr, index); |
76 if (localStorage.logMetrics) { | 124 if (localStorage.logMetrics) { |
77 console.log('chrome.metricsPrivate.recordValue', | 125 console.log('chrome.metricsPrivate.recordValue', |
78 [metricDescr.metricName, index, value]); | 126 [metricDescr.metricName, index, value]); |
79 } | 127 } |
80 }; | 128 }; |
OLD | NEW |