Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 var metrics = {}; | 11 var metrics = {}; |
| 12 | 12 |
| 13 metrics.intervals = {}; | 13 metrics.intervals = {}; |
| 14 | 14 |
| 15 metrics.startInterval = function(name) { | 15 metrics.startInterval = function(name) { |
| 16 metrics.intervals[name] = Date.now(); | 16 metrics.intervals[name] = Date.now(); |
| 17 }; | 17 }; |
| 18 | 18 |
| 19 metrics.startInterval('Load.Total'); | 19 metrics.startInterval('Load.Total'); |
| 20 metrics.startInterval('Load.Script'); | 20 metrics.startInterval('Load.Script'); |
| 21 | 21 |
| 22 metrics.convertName_ = function(name) { | 22 metrics.convertName_ = function(name) { |
| 23 return 'FileBrowser.' + name; | 23 return 'FileBrowser.' + name; |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 metrics.recordTime = function(name) { | 26 metrics.decorate = function(name) { |
| 27 this[name] = function() { | |
| 28 var args = Array.apply(null, arguments); | |
| 29 args[0] = metrics.convertName_(args[0]); | |
| 30 chrome.metricsPrivate[name].apply(chrome.metricsPrivate, args); | |
| 31 if (localStorage.logMetrics) { | |
| 32 console.log('chrome.metricsPrivate.' + name, args); | |
| 33 } | |
| 34 } | |
| 35 }; | |
| 36 | |
| 37 metrics.decorate('recordMediumCount'); | |
| 38 metrics.decorate('recordSmallCount'); | |
| 39 metrics.decorate('recordTime'); | |
| 40 metrics.decorate('recordUserAction'); | |
| 41 | |
| 42 metrics.recordInterval = function(name) { | |
| 27 if (name in metrics.intervals) { | 43 if (name in metrics.intervals) { |
| 28 var elapsed = Date.now() - metrics.intervals[name]; | 44 metrics.recordTime(name, Date.now() - metrics.intervals[name]); |
| 29 console.log(name + ': ' + elapsed + 'ms'); | |
| 30 chrome.metricsPrivate.recordTime(metrics.convertName_(name), elapsed); | |
| 31 } else { | 45 } else { |
| 32 console.error('Unknown interval: ' + name); | 46 console.error('Unknown interval: ' + name); |
| 33 } | 47 } |
| 34 }; | 48 }; |
| 35 | 49 |
| 36 metrics.recordAction = function(name) { | 50 metrics.recordEnum = function(name, value, validValues) { |
| 37 chrome.metricsPrivate.recordUserAction(metrics.convertName_(name)); | 51 var maxValue; |
| 52 var index; | |
| 53 if (validValues.constructor.name == 'Array') { | |
| 54 index = validValues.indexOf(value); | |
| 55 maxValue = validValues.length; | |
|
dgozman
2011/12/06 15:45:56
I think, it should be
validValues.length - 1
Vladislav Kaznacheev
2011/12/06 16:07:44
Done.
| |
| 56 } else { | |
| 57 index = value; | |
| 58 maxValue = validValues - 1; | |
| 59 } | |
| 60 // Collect invalid values in the extra bucket at the end. | |
| 61 if (index < 0 || index > maxValue) | |
| 62 index = maxValue; | |
| 63 | |
| 64 var metricDescr = { | |
| 65 'metricName': metrics.convertName_(name), | |
| 66 'type': 'histogram-linear', | |
| 67 'min': 0, | |
| 68 'max': maxValue, | |
| 69 'buckets': maxValue + 1 | |
| 70 }; | |
| 71 chrome.metricsPrivate.recordValue(metricDescr, index); | |
| 72 if (localStorage.logMetrics) { | |
| 73 console.log('chrome.metricsPrivate.recordValue', | |
| 74 [metricDescr.metricName, index, value]); | |
| 75 } | |
| 38 }; | 76 }; |
| 39 | |
| 40 metrics.reportCount = function(name, value) { | |
| 41 chrome.metricsPrivate.recordMediumCount(metrics.convertName_(name), value); | |
| 42 }; | |
| 43 | |
| 44 metrics.recordEnum = function(name, value, validValues) { | |
| 45 var index = validValues.indexOf(value); | |
| 46 | |
| 47 // Collect invalid values in the extra bucket at the end. | |
| 48 if (index < 0) index = validValues.length; | |
| 49 | |
| 50 chrome.metricsPrivate.recordValue({ | |
| 51 'metricName': metrics.convertName_(name), | |
| 52 'type': 'histogram-linear', | |
| 53 'min': 0, | |
| 54 'max': validValues.length, | |
| 55 'buckets': validValues.length + 1 | |
| 56 }, | |
| 57 index); | |
| 58 }; | |
| OLD | NEW |