Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/testing/performancetable.js
4050 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A table for showing the results of performance testing.
9
*
10
* {@see goog.testing.benchmark} for an easy way to use this functionality.
11
*/
12
13
goog.setTestOnly('goog.testing.PerformanceTable');
14
goog.provide('goog.testing.PerformanceTable');
15
16
goog.require('goog.asserts');
17
goog.require('goog.dom');
18
goog.require('goog.dom.TagName');
19
goog.require('goog.dom.safe');
20
goog.require('goog.string.Const');
21
goog.require('goog.testing.PerformanceTimer');
22
23
24
25
/**
26
* A UI widget that runs performance tests and displays the results.
27
* @param {Element} root The element where the table should be attached.
28
* @param {goog.testing.PerformanceTimer=} opt_timer A timer to use for
29
* executing functions and profiling them.
30
* @param {number=} opt_precision Number of digits of precision to include in
31
* results. Defaults to 0.
32
* @param {number=} opt_numSamples The number of samples to take. Defaults to 5.
33
* @constructor
34
* @final
35
*/
36
goog.testing.PerformanceTable = function(
37
root, opt_timer, opt_precision, opt_numSamples) {
38
'use strict';
39
/**
40
* Where the table should be attached.
41
* @private {Element}
42
*/
43
this.root_ = root;
44
45
/**
46
* Number of digits of precision to include in results.
47
* Defaults to 0.
48
* @private {number}
49
*/
50
this.precision_ = opt_precision || 0;
51
52
var timer = opt_timer;
53
if (!timer) {
54
timer = new goog.testing.PerformanceTimer();
55
timer.setNumSamples(opt_numSamples || 5);
56
timer.setDiscardOutliers(true);
57
}
58
59
/**
60
* A timer for running the tests.
61
* @private {goog.testing.PerformanceTimer}
62
*/
63
this.timer_ = timer;
64
65
this.initRoot_();
66
};
67
68
69
/**
70
* @return {goog.testing.PerformanceTimer} The timer being used.
71
*/
72
goog.testing.PerformanceTable.prototype.getTimer = function() {
73
'use strict';
74
return this.timer_;
75
};
76
77
78
/**
79
* Render the initial table.
80
* @private
81
*/
82
goog.testing.PerformanceTable.prototype.initRoot_ = function() {
83
'use strict';
84
goog.dom.safe.setInnerHtmlFromConstant(
85
goog.asserts.assert(this.root_),
86
goog.string.Const.from(
87
'<table class="test-results" cellspacing="1">' +
88
' <thead>' +
89
' <tr>' +
90
' <th rowspan="2">Test Description</th>' +
91
' <th rowspan="2">Runs</th>' +
92
' <th colspan="4">Results (ms)</th>' +
93
' </tr>' +
94
' <tr>' +
95
' <th>Average</th>' +
96
' <th>Median</th>' +
97
' <th>Std Dev</th>' +
98
' <th>Minimum</th>' +
99
' <th>Maximum</th>' +
100
' </tr>' +
101
' </thead>' +
102
' <tbody>' +
103
' </tbody>' +
104
'</table>'));
105
};
106
107
108
/**
109
* @return {!Element} The body of the table.
110
* @private
111
*/
112
goog.testing.PerformanceTable.prototype.getTableBody_ = function() {
113
'use strict';
114
return goog.dom.getElementsByTagName(
115
goog.dom.TagName.TBODY, goog.asserts.assert(this.root_))[0];
116
};
117
118
119
/**
120
* Round to the specified precision.
121
* @param {number} num The number to round.
122
* @return {string} The rounded number, as a string.
123
* @private
124
*/
125
goog.testing.PerformanceTable.prototype.round_ = function(num) {
126
'use strict';
127
var factor = Math.pow(10, this.precision_);
128
return String(Math.round(num * factor) / factor);
129
};
130
131
132
/**
133
* Run the given function with the performance timer, and show the results.
134
* @param {Function} fn The function to run.
135
* @param {string=} opt_desc A description to associate with this run.
136
*/
137
goog.testing.PerformanceTable.prototype.run = function(fn, opt_desc) {
138
'use strict';
139
this.runTask(
140
new goog.testing.PerformanceTimer.Task(/** @type {function()} */ (fn)),
141
opt_desc);
142
};
143
144
145
/**
146
* Run the given task with the performance timer, and show the results.
147
* @param {goog.testing.PerformanceTimer.Task} task The performance timer task
148
* to run.
149
* @param {string=} opt_desc A description to associate with this run.
150
*/
151
goog.testing.PerformanceTable.prototype.runTask = function(task, opt_desc) {
152
'use strict';
153
var results = this.timer_.runTask(task);
154
this.recordResults(results, opt_desc);
155
};
156
157
158
/**
159
* Record a performance timer results object to the performance table. See
160
* `goog.testing.PerformanceTimer` for details of the format of this
161
* object.
162
* @param {Object} results The performance timer results object.
163
* @param {string=} opt_desc A description to associate with these results.
164
*/
165
goog.testing.PerformanceTable.prototype.recordResults = function(
166
results, opt_desc) {
167
'use strict';
168
var average = results['average'];
169
var standardDeviation = results['standardDeviation'];
170
var isSuspicious = average < 0 || standardDeviation > average * .5;
171
var resultsRow = goog.dom.createDom(
172
goog.dom.TagName.TR, null,
173
goog.dom.createDom(
174
goog.dom.TagName.TD, 'test-description',
175
opt_desc || 'No description'),
176
goog.dom.createDom(
177
goog.dom.TagName.TD, 'test-count', String(results['count'])),
178
goog.dom.createDom(
179
goog.dom.TagName.TD, 'test-average', this.round_(average)),
180
goog.dom.createDom(
181
goog.dom.TagName.TD, 'test-median', String(results['median'])),
182
goog.dom.createDom(
183
goog.dom.TagName.TD, 'test-standard-deviation',
184
this.round_(standardDeviation)),
185
goog.dom.createDom(
186
goog.dom.TagName.TD, 'test-minimum', String(results['minimum'])),
187
goog.dom.createDom(
188
goog.dom.TagName.TD, 'test-maximum', String(results['maximum'])));
189
if (isSuspicious) {
190
resultsRow.className = 'test-suspicious';
191
}
192
this.getTableBody_().appendChild(resultsRow);
193
};
194
195
196
/**
197
* Report an error in the table.
198
* @param {*} reason The reason for the error.
199
*/
200
goog.testing.PerformanceTable.prototype.reportError = function(reason) {
201
'use strict';
202
this.getTableBody_().appendChild(goog.dom.createDom(
203
goog.dom.TagName.TR, null,
204
goog.dom.createDom(
205
goog.dom.TagName.TD, {'class': 'test-error', 'colSpan': 5},
206
String(reason))));
207
};
208
209