OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 /** |
| 5 * @unrestricted |
| 6 */ |
| 7 WebInspector.LineLevelProfile = class { |
| 8 constructor() { |
| 9 this._locationPool = new WebInspector.LiveLocationPool(); |
| 10 this.reset(); |
| 11 } |
| 12 |
| 13 /** |
| 14 * @return {!WebInspector.LineLevelProfile} |
| 15 */ |
| 16 static instance() { |
| 17 if (!WebInspector.LineLevelProfile._instance) |
| 18 WebInspector.LineLevelProfile._instance = new WebInspector.LineLevelProfil
e(); |
| 19 return WebInspector.LineLevelProfile._instance; |
| 20 } |
| 21 |
| 22 /** |
| 23 * @param {!WebInspector.CPUProfileDataModel} profile |
| 24 */ |
| 25 appendCPUProfile(profile) { |
| 26 var nodesToGo = [profile.profileHead]; |
| 27 var sampleDuration = (profile.profileEndTime - profile.profileStartTime) / p
rofile.totalHitCount; |
| 28 while (nodesToGo.length) { |
| 29 var nodes = nodesToGo.pop().children; |
| 30 for (var i = 0; i < nodes.length; ++i) { |
| 31 var node = nodes[i]; |
| 32 nodesToGo.push(node); |
| 33 if (!node.url || !node.positionTicks) |
| 34 continue; |
| 35 var fileInfo = this._files.get(node.url); |
| 36 if (!fileInfo) { |
| 37 fileInfo = new Map(); |
| 38 this._files.set(node.url, fileInfo); |
| 39 } |
| 40 for (var j = 0; j < node.positionTicks.length; ++j) { |
| 41 var lineInfo = node.positionTicks[j]; |
| 42 var line = lineInfo.line; |
| 43 var time = lineInfo.ticks * sampleDuration; |
| 44 fileInfo.set(line, (fileInfo.get(line) || 0) + time); |
| 45 } |
| 46 } |
| 47 } |
| 48 this._scheduleUpdate(); |
| 49 } |
| 50 |
| 51 reset() { |
| 52 /** @type {!Map<string, !Map<number, number>>} */ |
| 53 this._files = new Map(); |
| 54 this._scheduleUpdate(); |
| 55 } |
| 56 |
| 57 _scheduleUpdate() { |
| 58 if (this._updateTimer) |
| 59 return; |
| 60 this._updateTimer = setTimeout(() => { |
| 61 this._updateTimer = null; |
| 62 this._doUpdate(); |
| 63 }, 0); |
| 64 } |
| 65 |
| 66 _doUpdate() { |
| 67 // TODO(alph): use scriptId instead of urls for the target. |
| 68 this._locationPool.disposeAll(); |
| 69 WebInspector.workspace.uiSourceCodes().forEach( |
| 70 uiSourceCode => uiSourceCode.removeAllLineDecorations(WebInspector.LineL
evelProfile.LineDecorator.type)); |
| 71 for (var fileInfo of this._files) { |
| 72 var url = /** @type {string} */ (fileInfo[0]); |
| 73 var uiSourceCode = WebInspector.workspace.uiSourceCodeForURL(url); |
| 74 if (!uiSourceCode) |
| 75 continue; |
| 76 var target = |
| 77 WebInspector.NetworkProject.targetForUISourceCode(uiSourceCode) || Web
Inspector.targetManager.mainTarget(); |
| 78 var debuggerModel = target ? WebInspector.DebuggerModel.fromTarget(target)
: null; |
| 79 if (!debuggerModel) |
| 80 continue; |
| 81 for (var lineInfo of fileInfo[1]) { |
| 82 var line = lineInfo[0] - 1; |
| 83 var time = lineInfo[1]; |
| 84 var rawLocation = debuggerModel.createRawLocationByURL(url, line, 0); |
| 85 if (rawLocation) |
| 86 new WebInspector.LineLevelProfile.Presentation(rawLocation, time, this
._locationPool); |
| 87 else if (uiSourceCode) |
| 88 uiSourceCode.addLineDecoration(line, WebInspector.LineLevelProfile.Lin
eDecorator.type, time); |
| 89 } |
| 90 } |
| 91 } |
| 92 }; |
| 93 |
4 | 94 |
5 /** | 95 /** |
6 * @constructor | 96 * @unrestricted |
7 */ | 97 */ |
8 WebInspector.LineLevelProfile = function() | 98 WebInspector.LineLevelProfile.Presentation = class { |
9 { | 99 /** |
10 this._locationPool = new WebInspector.LiveLocationPool(); | 100 * @param {!WebInspector.DebuggerModel.Location} rawLocation |
11 this.reset(); | 101 * @param {number} time |
| 102 * @param {!WebInspector.LiveLocationPool} locationPool |
| 103 */ |
| 104 constructor(rawLocation, time, locationPool) { |
| 105 this._time = time; |
| 106 WebInspector.debuggerWorkspaceBinding.createLiveLocation(rawLocation, this.u
pdateLocation.bind(this), locationPool); |
| 107 } |
| 108 |
| 109 /** |
| 110 * @param {!WebInspector.LiveLocation} liveLocation |
| 111 */ |
| 112 updateLocation(liveLocation) { |
| 113 if (this._uiLocation) |
| 114 this._uiLocation.uiSourceCode.removeLineDecoration( |
| 115 this._uiLocation.lineNumber, WebInspector.LineLevelProfile.LineDecorat
or.type); |
| 116 this._uiLocation = liveLocation.uiLocation(); |
| 117 if (this._uiLocation) |
| 118 this._uiLocation.uiSourceCode.addLineDecoration( |
| 119 this._uiLocation.lineNumber, WebInspector.LineLevelProfile.LineDecorat
or.type, this._time); |
| 120 } |
12 }; | 121 }; |
13 | 122 |
14 /** | 123 /** |
15 * @return {!WebInspector.LineLevelProfile} | 124 * @implements {WebInspector.UISourceCodeFrame.LineDecorator} |
| 125 * @unrestricted |
16 */ | 126 */ |
17 WebInspector.LineLevelProfile.instance = function() | 127 WebInspector.LineLevelProfile.LineDecorator = class { |
18 { | 128 /** |
19 if (!WebInspector.LineLevelProfile._instance) | 129 * @override |
20 WebInspector.LineLevelProfile._instance = new WebInspector.LineLevelProf
ile(); | 130 * @param {!WebInspector.UISourceCode} uiSourceCode |
21 return WebInspector.LineLevelProfile._instance; | 131 * @param {!WebInspector.CodeMirrorTextEditor} textEditor |
| 132 */ |
| 133 decorate(uiSourceCode, textEditor) { |
| 134 var gutterType = 'CodeMirror-gutter-performance'; |
| 135 var decorations = uiSourceCode.lineDecorations(WebInspector.LineLevelProfile
.LineDecorator.type); |
| 136 textEditor.uninstallGutter(gutterType); |
| 137 if (!decorations) |
| 138 return; |
| 139 textEditor.installGutter(gutterType, false); |
| 140 for (var decoration of decorations.values()) { |
| 141 var time = /** @type {number} */ (decoration.data()); |
| 142 var text = WebInspector.UIString('%.1f\xa0ms', time); |
| 143 var intensity = Number.constrain(Math.log10(1 + 2 * time) / 5, 0.02, 1); |
| 144 var element = createElementWithClass('div', 'text-editor-line-marker-perfo
rmance'); |
| 145 element.textContent = text; |
| 146 element.style.backgroundColor = `hsla(44, 100%, 50%, ${intensity.toFixed(3
)})`; |
| 147 textEditor.setGutterDecoration(decoration.line(), gutterType, element); |
| 148 } |
| 149 } |
22 }; | 150 }; |
23 | 151 |
24 WebInspector.LineLevelProfile.prototype = { | 152 WebInspector.LineLevelProfile.LineDecorator.type = 'performance'; |
25 /** | |
26 * @param {!WebInspector.CPUProfileDataModel} profile | |
27 */ | |
28 appendCPUProfile: function(profile) | |
29 { | |
30 var nodesToGo = [profile.profileHead]; | |
31 var sampleDuration = (profile.profileEndTime - profile.profileStartTime)
/ profile.totalHitCount; | |
32 while (nodesToGo.length) { | |
33 var nodes = nodesToGo.pop().children; | |
34 for (var i = 0; i < nodes.length; ++i) { | |
35 var node = nodes[i]; | |
36 nodesToGo.push(node); | |
37 if (!node.url || !node.positionTicks) | |
38 continue; | |
39 var fileInfo = this._files.get(node.url); | |
40 if (!fileInfo) { | |
41 fileInfo = new Map(); | |
42 this._files.set(node.url, fileInfo); | |
43 } | |
44 for (var j = 0; j < node.positionTicks.length; ++j) { | |
45 var lineInfo = node.positionTicks[j]; | |
46 var line = lineInfo.line; | |
47 var time = lineInfo.ticks * sampleDuration; | |
48 fileInfo.set(line, (fileInfo.get(line) || 0) + time); | |
49 } | |
50 } | |
51 } | |
52 this._scheduleUpdate(); | |
53 }, | |
54 | |
55 reset: function() | |
56 { | |
57 /** @type {!Map<string, !Map<number, number>>} */ | |
58 this._files = new Map(); | |
59 this._scheduleUpdate(); | |
60 }, | |
61 | |
62 _scheduleUpdate: function() | |
63 { | |
64 if (this._updateTimer) | |
65 return; | |
66 this._updateTimer = setTimeout(() => { | |
67 this._updateTimer = null; | |
68 this._doUpdate(); | |
69 }, 0); | |
70 }, | |
71 | |
72 _doUpdate: function() | |
73 { | |
74 // TODO(alph): use scriptId instead of urls for the target. | |
75 this._locationPool.disposeAll(); | |
76 WebInspector.workspace.uiSourceCodes().forEach(uiSourceCode => uiSourceC
ode.removeAllLineDecorations(WebInspector.LineLevelProfile.LineDecorator.type)); | |
77 for (var fileInfo of this._files) { | |
78 var url = /** @type {string} */ (fileInfo[0]); | |
79 var uiSourceCode = WebInspector.workspace.uiSourceCodeForURL(url); | |
80 if (!uiSourceCode) | |
81 continue; | |
82 var target = WebInspector.NetworkProject.targetForUISourceCode(uiSou
rceCode) || WebInspector.targetManager.mainTarget(); | |
83 var debuggerModel = target ? WebInspector.DebuggerModel.fromTarget(t
arget) : null; | |
84 if (!debuggerModel) | |
85 continue; | |
86 for (var lineInfo of fileInfo[1]) { | |
87 var line = lineInfo[0] - 1; | |
88 var time = lineInfo[1]; | |
89 var rawLocation = debuggerModel.createRawLocationByURL(url, line
, 0); | |
90 if (rawLocation) | |
91 new WebInspector.LineLevelProfile.Presentation(rawLocation,
time, this._locationPool); | |
92 else if (uiSourceCode) | |
93 uiSourceCode.addLineDecoration(line, WebInspector.LineLevelP
rofile.LineDecorator.type, time); | |
94 } | |
95 } | |
96 } | |
97 }; | |
98 | |
99 /** | |
100 * @constructor | |
101 * @param {!WebInspector.DebuggerModel.Location} rawLocation | |
102 * @param {number} time | |
103 * @param {!WebInspector.LiveLocationPool} locationPool | |
104 */ | |
105 WebInspector.LineLevelProfile.Presentation = function(rawLocation, time, locatio
nPool) | |
106 { | |
107 this._time = time; | |
108 WebInspector.debuggerWorkspaceBinding.createLiveLocation(rawLocation, this.u
pdateLocation.bind(this), locationPool); | |
109 }; | |
110 | |
111 WebInspector.LineLevelProfile.Presentation.prototype = { | |
112 /** | |
113 * @param {!WebInspector.LiveLocation} liveLocation | |
114 */ | |
115 updateLocation: function(liveLocation) | |
116 { | |
117 if (this._uiLocation) | |
118 this._uiLocation.uiSourceCode.removeLineDecoration(this._uiLocation.
lineNumber, WebInspector.LineLevelProfile.LineDecorator.type); | |
119 this._uiLocation = liveLocation.uiLocation(); | |
120 if (this._uiLocation) | |
121 this._uiLocation.uiSourceCode.addLineDecoration(this._uiLocation.lin
eNumber, WebInspector.LineLevelProfile.LineDecorator.type, this._time); | |
122 } | |
123 }; | |
124 | |
125 /** | |
126 * @constructor | |
127 * @implements {WebInspector.UISourceCodeFrame.LineDecorator} | |
128 */ | |
129 WebInspector.LineLevelProfile.LineDecorator = function() | |
130 { | |
131 }; | |
132 | |
133 WebInspector.LineLevelProfile.LineDecorator.type = "performance"; | |
134 | |
135 WebInspector.LineLevelProfile.LineDecorator.prototype = { | |
136 /** | |
137 * @override | |
138 * @param {!WebInspector.UISourceCode} uiSourceCode | |
139 * @param {!WebInspector.CodeMirrorTextEditor} textEditor | |
140 */ | |
141 decorate: function(uiSourceCode, textEditor) | |
142 { | |
143 var gutterType = "CodeMirror-gutter-performance"; | |
144 var decorations = uiSourceCode.lineDecorations(WebInspector.LineLevelPro
file.LineDecorator.type); | |
145 textEditor.uninstallGutter(gutterType); | |
146 if (!decorations) | |
147 return; | |
148 textEditor.installGutter(gutterType, false); | |
149 for (var decoration of decorations.values()) { | |
150 var time = /** @type {number} */ (decoration.data()); | |
151 var text = WebInspector.UIString("%.1f\xa0ms", time); | |
152 var intensity = Number.constrain(Math.log10(1 + 2 * time) / 5, 0.02,
1); | |
153 var element = createElementWithClass("div", "text-editor-line-marker
-performance"); | |
154 element.textContent = text; | |
155 element.style.backgroundColor = `hsla(44, 100%, 50%, ${intensity.toF
ixed(3)})`; | |
156 textEditor.setGutterDecoration(decoration.line(), gutterType, elemen
t); | |
157 } | |
158 } | |
159 }; | |
OLD | NEW |