This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
/
benchmark-runner.html
102 lines (92 loc) · 3.23 KB
/
benchmark-runner.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<polymer-element name="benchmark-runner" attributes="variants times maxTime maxRuns" on-setupfinished="{{ setupFinished }}" on-runfinished="{{ runFinished">
<template>
<button on-click="{{ go }}">go</button>
</template>
<script>
(function(global) {
var INIT = 0;
var ESTIMATE = 1;
var TESTING = 2;
var hasForceGc = typeof global.gc === 'function';
Polymer('benchmark-runner', {
maxTime: 400,
maxRuns: 50,
ready: function() {
this.times = [];
this.results = [];
},
go: function() {
this.runBenchmark(this.querySelector('*'));
},
runBenchmark: function(benchmark) {
this.current = benchmark;
this.runSeries(INIT, 1);
},
runSeries: function(state, count) {
this.times.length = 0;
this.state = state;
this.runCount = count;
this.remaining = count;
this.runOne();
},
runOne: function(e) {
if (hasForceGc) {
global.gc();
global.gc();
global.gc();
}
this.fire('setup', undefined, this.current);
},
setupFinished: function() {
this.fire('run', undefined, this.current);
},
runFinished: function(e, detail) {
this.times.push(detail.totalTime);
this.remaining--;
if (this.remaining > 0) {
this.runOne();
return;
}
var total = 0;
for (var i = 0; i < this.times.length; i++) {
total += this.times[0];
}
console.log(this.times, duration);
var duration = Math.round(total / this.times.length);
switch (this.state) {
case INIT:
// Run the test twice to estimate its time.
this.runSeries(ESTIMATE, 2);
break;
case ESTIMATE:
// Run as many tests as will fit in maxTime.
var testingRuns =
Math.min(Math.round(this.maxTime/duration), this.maxRuns);
if (testingRuns >= 4)
this.runSeries(TESTING, testingRuns);
else
this.variantComplete(duration);
break;
case TESTING:
this.variantComplete(duration);
break;
}
},
variantComplete: function(duration) {
this.results.push(duration);
alert('result: ' + duration + 'ms');
}
});
})(this);
</script>
</polymer-element>