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
3 // found in the LICENSE file.
7 * @extends {WebInspector.Object}
9 WebInspector.TimelinePowerOverviewDataProvider = function()
12 if (Capabilities.canProfilePower)
13 WebInspector.powerProfiler.addEventListener(WebInspector.PowerProfiler.EventTypes.PowerEventRecorded, this._onRecordAdded, this);
16 WebInspector.TimelinePowerOverviewDataProvider.prototype = {
18 * @return {!Array.<!PowerAgent.PowerEvent>}
22 // The last record is not used, as its "value" is not set.
23 return this._records.slice(0, this._records.length - 1);
26 _onRecordAdded: function(event)
28 // "value" of original PowerEvent means the average power between previous sampling to current one.
29 // Here, it is converted to average power between current sampling to next one.
30 var record = event.data;
31 var length = this._records.length;
33 this._records[length - 1].value = record.value;
34 this._records.push(record);
37 __proto__: WebInspector.Object.prototype
42 * @extends {WebInspector.TimelineOverviewBase}
43 * @param {!WebInspector.TimelineModel} model
45 WebInspector.TimelinePowerOverview = function(model)
47 WebInspector.TimelineOverviewBase.call(this, model);
48 this.element.id = "timeline-overview-power";
49 this._dataProvider = new WebInspector.TimelinePowerOverviewDataProvider();
51 this._maxPowerLabel = this.element.createChild("div", "max memory-graph-label");
52 this._minPowerLabel = this.element.createChild("div", "min memory-graph-label");
55 WebInspector.TimelinePowerOverview.prototype = {
56 timelineStarted: function()
58 if (Capabilities.canProfilePower)
59 WebInspector.powerProfiler.startProfile();
62 timelineStopped: function()
64 if (Capabilities.canProfilePower)
65 WebInspector.powerProfiler.stopProfile();
68 _resetPowerLabels: function()
70 this._maxPowerLabel.textContent = "";
71 this._minPowerLabel.textContent = "";
78 var records = this._dataProvider.records();
79 if (!records.length) {
80 this._resetPowerLabels();
84 const lowerOffset = 3;
86 var minPower = 100000000000;
87 var minTime = this._model.minimumRecordTime();
88 var maxTime = this._model.maximumRecordTime();
89 for (var i = 0; i < records.length; i++) {
90 var record = records[i];
91 if (record.timestamp < minTime || record.timestamp > maxTime)
93 maxPower = Math.max(maxPower, record.value);
94 minPower = Math.min(minPower, record.value);
96 minPower = Math.min(minPower, maxPower);
99 var width = this._canvas.width;
100 var height = this._canvas.height - lowerOffset;
101 var xFactor = width / (maxTime - minTime);
102 var yFactor = height / Math.max(maxPower - minPower, 1);
104 var histogram = new Array(width);
105 for (var i = 0; i < records.length - 1; i++) {
106 var record = records[i];
107 if (record.timestamp < minTime || record.timestamp > maxTime)
109 var x = Math.round((record.timestamp - minTime) * xFactor);
110 var y = Math.round((record.value- minPower ) * yFactor);
111 histogram[x] = Math.max(histogram[x] || 0, y);
115 var isFirstPoint = true;
116 var ctx = this._context;
118 ctx.translate(0.5, 0.5);
120 ctx.moveTo(-1, this._canvas.height);
121 for (var x = 0; x < histogram.length; x++) {
122 if (typeof histogram[x] === "undefined")
125 isFirstPoint = false;
127 ctx.lineTo(-1, height - y);
129 ctx.lineTo(x, height - y);
131 ctx.lineTo(x, height - y);
134 ctx.lineTo(width, height - y);
135 ctx.lineTo(width, this._canvas.height);
136 ctx.lineTo(-1, this._canvas.height);
139 ctx.fillStyle = "rgba(255,192,0, 0.8);";
143 ctx.strokeStyle = "rgba(20,0,0,0.8)";
147 this._maxPowerLabel.textContent = WebInspector.UIString("%.2f\u2009watts", maxPower);
148 this._minPowerLabel.textContent = WebInspector.UIString("%.2f\u2009watts", minPower);;
151 __proto__: WebInspector.TimelineOverviewBase.prototype