Revert of [telemetry] Fix decorator hack for benchmark_smoke_unittest. (patchset...
[chromium-blink-merge.git] / remoting / webapp / crd / js / connection_stats.js
blob8b144c4484425ce609a0118f64b9efcf385792ad
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
3 // found in the LICENSE file.
5 /**
6 * @fileoverview
7 * Module to support debug overlay window with connection stats.
8 */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16 * @constructor
17 * @implements {remoting.WindowShape.ClientUI}
18 * @param {Element} statsElement The HTML div to which to update stats.
20 remoting.ConnectionStats = function(statsElement) {
21 /**
22 * @private
24 this.statsElement_ = statsElement;
26 /**
27 * @type {remoting.ClientSession.PerfStats}
28 * @private
30 this.mostRecent_ = null
32 remoting.windowShape.addCallback(this);
35 /**
36 * @return {remoting.ClientSession.PerfStats} The most recently-set PerfStats,
37 * or null if update() has not yet been called.
39 remoting.ConnectionStats.prototype.mostRecent = function() {
40 return this.mostRecent_;
43 /**
44 * Show or hide the connection stats div.
46 remoting.ConnectionStats.prototype.toggle = function() {
47 this.statsElement_.hidden = !this.statsElement_.hidden;
50 /**
51 * If the stats panel is visible, add its bounding rectangle to the specified
52 * region.
53 * @param {Array.<{left: number, top: number, width: number, height: number}>}
54 * rects List of rectangles.
57 remoting.ConnectionStats.prototype.addToRegion = function(rects) {
58 if (!this.statsElement_.hidden) {
59 rects.push(this.statsElement_.getBoundingClientRect());
63 /**
64 * Update the statistics panel.
65 * @param {remoting.ClientSession.PerfStats} stats The connection statistics.
67 remoting.ConnectionStats.prototype.update = function(stats) {
68 this.mostRecent_ = stats;
69 var units = '';
70 var videoBandwidth = stats.videoBandwidth;
71 if (videoBandwidth != undefined) {
72 if (videoBandwidth < 1024) {
73 units = 'Bps';
74 } else if (videoBandwidth < 1048576) {
75 units = 'KiBps';
76 videoBandwidth = videoBandwidth / 1024;
77 } else if (videoBandwidth < 1073741824) {
78 units = 'MiBps';
79 videoBandwidth = videoBandwidth / 1048576;
80 } else {
81 units = 'GiBps';
82 videoBandwidth = videoBandwidth / 1073741824;
86 /**
87 * @param {number} value
88 * @param {string} units
89 * @return {string} Formatted number.
91 function formatStatNumber(value, units) {
92 if (value != undefined) {
93 return value.toFixed(2) + ' ' + units;
94 } else {
95 return "n/a";
99 var statistics = document.getElementById('statistics');
100 this.statsElement_.innerText = (
101 'Bandwidth: ' + formatStatNumber(videoBandwidth, units) +
102 ', Frame Rate: ' + formatStatNumber(stats.videoFrameRate, 'fps') +
103 ', Capture: ' + formatStatNumber(stats.captureLatency, 'ms') +
104 ', Encode: ' + formatStatNumber(stats.encodeLatency, 'ms') +
105 ', Decode: ' + formatStatNumber(stats.decodeLatency, 'ms') +
106 ', Render: ' + formatStatNumber(stats.renderLatency, 'ms') +
107 ', Latency: ' + formatStatNumber(stats.roundtripLatency, 'ms'));
111 * Check for the debug toggle hot-key.
113 * @param {Event} event The keyboard event.
114 * @return {void} Nothing.
116 remoting.ConnectionStats.onKeydown = function(event) {
117 var element = /** @type {Element} */ (event.target);
118 if (element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') {
119 return;
121 if (String.fromCharCode(event.which) == 'D') {
122 remoting.stats.toggle();
126 /** @type {remoting.ConnectionStats} */
127 remoting.stats = null;