1
1
var bp = window . bp = { } ;
2
2
bp . steps = window . benchmarkSteps = [ ] ;
3
- bp . runState = { } ;
3
+ bp . runState = {
4
+ numSamples : 10 ,
5
+ recentTimePerStep : { } ,
6
+ timesPerAction : { }
7
+ } ;
4
8
5
- bp . setRunState = function ( samples , iterations , ignoreCount ) {
6
- bp . runState . numSamples = samples ;
9
+ bp . setIterations = function ( iterations ) {
7
10
bp . runState . iterations = iterations ;
8
- bp . runState . ignoreCount = ignoreCount ;
9
- bp . runState . recentTimePerStep = { } ;
10
- bp . runState . timesPerAction = { } ;
11
11
} ;
12
12
13
- bp . resetRunState = function ( ) {
14
- bp . runState = {
15
- numSamples : 0 ,
16
- iterations : 0 ,
17
- ignoreCount : 0 ,
18
- recentTimePerStep : { } ,
19
- timesPerAction : { }
20
- } ;
13
+ bp . resetIterations = function ( ) {
14
+ bp . runState . iterations = 0 ;
21
15
} ;
22
16
23
17
bp . interpolateHtml = function ( string , list ) {
@@ -42,17 +36,17 @@ bp.numMilliseconds = function() {
42
36
bp . loopBenchmark = function ( ) {
43
37
if ( bp . runState . iterations <= - 1 ) {
44
38
//Time to stop looping
45
- bp . setRunState ( 10 , 0 ) ;
39
+ bp . setIterations ( 0 ) ;
46
40
bp . loopBtn . innerText = 'Loop' ;
47
41
return ;
48
42
}
49
- bp . setRunState ( 10 , - 1 ) ;
43
+ bp . setIterations ( - 1 ) ;
50
44
bp . loopBtn . innerText = 'Pause' ;
51
45
bp . runAllTests ( ) ;
52
46
} ;
53
47
54
48
bp . onceBenchmark = function ( ) {
55
- bp . setRunState ( 10 , 1 ) ;
49
+ bp . setIterations ( 1 ) ;
56
50
bp . onceBtn . innerText = '...' ;
57
51
bp . runAllTests ( function ( ) {
58
52
bp . onceBtn . innerText = 'Once' ;
@@ -61,13 +55,27 @@ bp.onceBenchmark = function() {
61
55
62
56
bp . twentyFiveBenchmark = function ( ) {
63
57
var twentyFiveBtn = bp . twentyFiveBtn ;
64
- bp . setRunState ( 20 , 25 ) ;
58
+ bp . setIterations ( 25 ) ;
65
59
twentyFiveBtn . innerText = 'Looping...' ;
66
60
bp . runAllTests ( function ( ) {
67
61
twentyFiveBtn . innerText = 'Loop 25x' ;
68
62
} , 5 ) ;
69
63
} ;
70
64
65
+ bp . addSampleRange = function ( ) {
66
+ bp . sampleRange = bp . container ( ) . querySelector ( '#sampleRange' ) ;
67
+ bp . sampleRange . value = Math . max ( bp . runState . numSamples , 1 ) ;
68
+ bp . sampleRange . addEventListener ( 'input' , bp . onSampleRangeChanged ) ;
69
+ bp . sampleRangeValue = bp . container ( ) . querySelector ( '#sampleRangeValue' ) ;
70
+ bp . sampleRangeValue . innerText = bp . runState . numSamples ;
71
+ } ;
72
+
73
+ bp . onSampleRangeChanged = function ( evt ) {
74
+ var value = evt . target . value ;
75
+ bp . runState . numSamples = parseInt ( value , 10 ) ;
76
+ bp . sampleRangeValue . innerText = value ;
77
+ } ;
78
+
71
79
bp . runTimedTest = function ( bs ) {
72
80
if ( typeof window . gc === 'function' ) {
73
81
window . gc ( ) ;
@@ -90,31 +98,23 @@ bp.runAllTests = function (done) {
90
98
}
91
99
else {
92
100
bp . writeReport ( bp . report ) ;
93
- bp . resetRunState ( ) ;
101
+ bp . resetIterations ( ) ;
94
102
done && done ( ) ;
95
103
}
96
104
}
97
105
98
- bp . padName = function ( name ) {
99
- return name . length < 10 ?
100
- ( ' ' + name ) . slice ( - 10 ) . replace ( / / g, ' ' ) :
101
- name ;
102
- } ;
103
-
104
106
bp . generateReportPartial = function ( name , avg , times ) {
105
107
return bp . interpolateHtml (
106
- '<div>%0: avg-%1:<b>%2ms</b> [%3 ]ms</div >' ,
108
+ '<tr><td>%0</td><td class="average">%1ms</td><td>[%2 ]ms</td></tr >' ,
107
109
[
108
- bp . padName ( name ) ,
109
- bp . runState . numSamples ,
110
+ name ,
110
111
( '' + avg ) . substr ( 0 , 6 ) ,
111
112
times . join ( ', ' )
112
113
] ) ;
113
114
} ;
114
115
115
116
bp . getAverage = function ( times , runState ) {
116
- var avg = 0 , ignoreCount = ( runState && runState . ignoreCount ) || 0 ;
117
- times = times . slice ( ignoreCount ) ;
117
+ var avg = 0 ;
118
118
times . forEach ( function ( x ) { avg += x ; } ) ;
119
119
return avg / times . length ;
120
120
} ;
@@ -126,7 +126,7 @@ bp.writeReport = function(reportContent) {
126
126
bp . getTimesPerAction = function ( name ) {
127
127
var tpa = bp . runState . timesPerAction [ name ] ;
128
128
if ( ! tpa ) {
129
- tpa = bp . runState . timesPerAction [ name ] = {
129
+ tpa = bp . runState . timesPerAction [ name ] = {
130
130
times : [ ] , // circular buffer
131
131
fmtTimes : [ ] ,
132
132
nextEntry : 0
@@ -135,15 +135,27 @@ bp.getTimesPerAction = function(name) {
135
135
return tpa ;
136
136
} ;
137
137
138
+ bp . rightSizeTimes = function ( times ) {
139
+ var delta = times . length - bp . runState . numSamples ;
140
+ if ( delta > 0 ) {
141
+ return times . slice ( delta ) ;
142
+ }
143
+
144
+ return times ;
145
+ } ;
146
+
138
147
bp . calcStats = function ( ) {
139
148
var report = '' ;
140
149
bp . steps . forEach ( function ( bs ) {
141
150
var stepName = bs . name ,
142
151
timeForStep = bp . runState . recentTimePerStep [ stepName ] ,
143
152
tpa = bp . getTimesPerAction ( stepName ) ,
144
153
avg ;
154
+
145
155
tpa . fmtTimes [ tpa . nextEntry ] = ( '' + timeForStep ) . substr ( 0 , 6 ) ;
156
+ tpa . fmtTimes = bp . rightSizeTimes ( tpa . fmtTimes ) ;
146
157
tpa . times [ tpa . nextEntry ++ ] = timeForStep ;
158
+ tpa . times = bp . rightSizeTimes ( tpa . times ) ;
147
159
tpa . nextEntry %= bp . runState . numSamples ;
148
160
avg = bp . getAverage ( tpa . times , bp . runState ) ;
149
161
@@ -159,49 +171,37 @@ bp.container = function() {
159
171
return bp . _container ;
160
172
}
161
173
162
- bp . addButton = function ( reference , text , handler ) {
174
+ bp . addButton = function ( reference , handler ) {
163
175
var container = bp . container ( ) ;
164
- bp [ reference ] = document . createElement ( 'button' ) ;
165
- bp [ reference ] . innerText = text ;
166
-
176
+ bp [ reference ] = container . querySelector ( 'button.' + reference ) ;
167
177
bp [ reference ] . addEventListener ( 'click' , handler ) ;
168
-
169
- container . appendChild ( bp [ reference ] ) ;
170
178
}
171
179
172
180
bp . addLinks = function ( ) {
173
181
// Add links to everything
174
- var linkDiv = document . createElement ( 'div' ) ;
175
- linkDiv . style [ 'margin-bottom' ] = '1.5em' ;
176
- var linkHtml = [
177
- '<style>' ,
178
- '.bpLink { background: lightblue; padding: 1em }' ,
179
- '</style>' ,
180
- '<span class=bpLink>Benchmark Versions: </span>'
181
- ] . join ( '\n' ) ;
182
+ var linkDiv = bp . container ( ) . querySelector ( '.versionContent' ) ;
183
+ var linkHtml = '' ;
182
184
183
185
[
184
186
// Add new benchmark suites here
185
187
[ 'tree.html' , 'TreeComponent' ]
186
188
] . forEach ( ( function ( link ) {
187
- linkHtml += bp . interpolateHtml ( '<a class=bpLink href=%0>%1</a>' , link ) ;
189
+ linkHtml += bp . interpolateHtml ( '<a href=%0>%1</a>' , link ) ;
188
190
} ) ) ;
189
191
190
192
linkDiv . innerHTML = linkHtml ;
191
- bp . container ( ) . appendChild ( linkDiv ) ;
192
193
} ;
193
194
194
195
bp . addInfo = function ( ) {
195
- bp . infoDiv = document . createElement ( 'div' ) ;
196
- bp . infoDiv . style [ 'font-family' ] = 'monospace' ;
197
- bp . container ( ) . appendChild ( bp . infoDiv ) ;
196
+ bp . infoDiv = bp . container ( ) . querySelector ( 'tbody.info' ) ;
198
197
} ;
199
198
200
199
bp . onDOMContentLoaded = function ( ) {
201
200
bp . addLinks ( ) ;
202
- bp . addButton ( 'loopBtn' , 'Loop' , bp . loopBenchmark ) ;
203
- bp . addButton ( 'onceBtn' , 'Once' , bp . onceBenchmark ) ;
204
- bp . addButton ( 'twentyFiveBtn' , 'Loop 25x' , bp . twentyFiveBenchmark ) ;
201
+ bp . addButton ( 'loopBtn' , bp . loopBenchmark ) ;
202
+ bp . addButton ( 'onceBtn' , bp . onceBenchmark ) ;
203
+ bp . addButton ( 'twentyFiveBtn' , bp . twentyFiveBenchmark ) ;
204
+ bp . addSampleRange ( ) ;
205
205
bp . addInfo ( ) ;
206
206
} ;
207
207
0 commit comments