| 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 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 metrics.recordTime(name, Date.now() - metrics.intervals[name]); | 87 metrics.recordTime(name, Date.now() - metrics.intervals[name]); |
| 88 } else { | 88 } else { |
| 89 console.error('Unknown interval: ' + name); | 89 console.error('Unknown interval: ' + name); |
| 90 } | 90 } |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 /** | 93 /** |
| 94 * Record an enum value. | 94 * Record an enum value. |
| 95 * | 95 * |
| 96 * @param {string} name Metric name. | 96 * @param {string} name Metric name. |
| 97 * @param {Object} value Enum value. | 97 * @param {*} value Enum value. |
| 98 * @param {Array.<Object>|number} validValues Array of valid values | 98 * @param {Array.<*>|number} validValues Array of valid values |
| 99 * or a boundary number value. | 99 * or a boundary number value. |
| 100 */ | 100 */ |
| 101 metrics.recordEnum = function(name, value, validValues) { | 101 metrics.recordEnum = function(name, value, validValues) { |
| 102 var boundaryValue; | 102 var boundaryValue; |
| 103 var index; | 103 var index; |
| 104 if (validValues.constructor.name == 'Array') { | 104 if (validValues.constructor.name == 'Array') { |
| 105 index = validValues.indexOf(value); | 105 index = validValues.indexOf(value); |
| 106 boundaryValue = validValues.length; | 106 boundaryValue = validValues.length; |
| 107 } else { | 107 } else { |
| 108 index = value; | 108 index = /** @type {number} */ (value); |
| 109 boundaryValue = validValues; | 109 boundaryValue = validValues; |
| 110 } | 110 } |
| 111 // Collect invalid values in the overflow bucket at the end. | 111 // Collect invalid values in the overflow bucket at the end. |
| 112 if (index < 0 || index > boundaryValue) | 112 if (index < 0 || index > boundaryValue) |
| 113 index = boundaryValue; | 113 index = boundaryValue; |
| 114 | 114 |
| 115 // Setting min to 1 looks strange but this is exactly the recommended way | 115 // Setting min to 1 looks strange but this is exactly the recommended way |
| 116 // of using histograms for enum-like types. Bucket #0 works as a regular | 116 // of using histograms for enum-like types. Bucket #0 works as a regular |
| 117 // bucket AND the underflow bucket. | 117 // bucket AND the underflow bucket. |
| 118 // (Source: UMA_HISTOGRAM_ENUMERATION definition in base/metrics/histogram.h) | 118 // (Source: UMA_HISTOGRAM_ENUMERATION definition in base/metrics/histogram.h) |
| 119 var metricDescr = { | 119 var metricDescr = { |
| 120 'metricName': metrics.convertName_(name), | 120 'metricName': metrics.convertName_(name), |
| 121 'type': 'histogram-linear', | 121 'type': 'histogram-linear', |
| 122 'min': 1, | 122 'min': 1, |
| 123 'max': boundaryValue, | 123 'max': boundaryValue, |
| 124 'buckets': boundaryValue + 1 | 124 'buckets': boundaryValue + 1 |
| 125 }; | 125 }; |
| 126 metrics.call_('recordValue', [metricDescr, index]); | 126 metrics.call_('recordValue', [metricDescr, index]); |
| 127 if (metrics.log) { | 127 if (metrics.log) { |
| 128 console.log('chrome.metricsPrivate.recordValue', | 128 console.log('chrome.metricsPrivate.recordValue', |
| 129 [metricDescr.metricName, index, value]); | 129 [metricDescr.metricName, index, value]); |
| 130 } | 130 } |
| 131 }; | 131 }; |
| OLD | NEW |