-
Notifications
You must be signed in to change notification settings - Fork 0
/
benches.js
298 lines (223 loc) · 7.64 KB
/
benches.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var Benchmark = require('benchmark')
,fs = require('fs')
,path = require('path')
,wrench = require('wrench')
,clc = require('cli-color')
,semver = require('semver')
,diff = require('diff')
,program = require('commander')
,BenchmarkCliChart = require('./benchmark-cli-chart')
,utils = require('./utils')
var loadTemplates = function( next ){
var includes = program.tinclude
,excludes = program.texclude
fs.readdir( path.join( __dirname, 'templates' ), function( err, files ){
if( err ){ next(err); return; }
(function read( err, data ){
if( files.length ){
var name = files.shift()
if( excludes.length && excludes.indexOf(name) === -1 ){
read( err, data );
return;
}
if( includes.length && includes.indexOf(name) === -1 ){
read( err, data );
return;
}
fs.readFile( path.join( __dirname, 'templates', name ), 'utf8', function( err, str ){
if( err ) { program.verbose && console.error( err, 'continuing...'); return; }
var parts = str.split('---')
,tplObj = { filename: name, str: parts[1] }
// not using JSON here because functions should be definable in model
eval( 'var config = ' + parts[0] )
// cheap copy
for( var i in config ){
tplObj[i] = config[i];
}
data.push( tplObj );
read( err, data );
})
} else {
next( null, data );
}
}( null, [] ))
})
}
var requireVashes = function( next ){
var vashes = {};
fs.readdir( path.join( __dirname, 'vashes' ), function( err, files ){
if( err ){ next(err); return; }
var local;
if( program.localversion ){
local = require( path.join( __dirname, program.localversion ) );
files.push( local.version );
vashes[ local.version ] = local;
vashes.local = local; // for ease of accessing later, everything else should ignore this key
}
if( program.versions ){
console.log('Versions available:');
files.sort(semver.compare).forEach(function(f){
console.log( program.localversion && local && local.version === f
? f + ' (' + program.localversion + ')'
: f );
})
process.exit();
}
files.forEach(function(name){
try{
vashes[name] = require( path.join( __dirname, './', 'vashes', name) )
} catch(e){
program.verbose && console.error('Could not require vash@' + name + ', continuing...');
}
})
next( err, vashes );
})
}
var compileWithVersions = function( tpls, vashes, next ){
var options = { debug: false, useWith: false, modelName: 'model' }
var excludes = [ '0.5.2-1238' ]
,includes = program.vinclude
excludes = excludes.concat( program.vexclude )
// avoid 0.5.11 coming before 0.5.2
Object.keys( vashes ).sort( semver.compare ).forEach(function(i){
tpls.forEach(function(tpl){
tpl.compiled = tpl.compiled || {};
var isExcluded = excludes.some(semver.satisfies.bind(semver, i))
,isIncluded = includes.some(semver.satisfies.bind(semver, i))
isIncluded = program.vinclude.length > 0
? isIncluded
: true;
// override if a local version was included and this is it
if( vashes.local && vashes.local.version === i ){
isIncluded = true;
isExcluded = false;
}
try {
if( isExcluded ){
// just do nothing
//process.stderr.write( 'Told to globally skip compiling tpls for vash@' + i + '...\n' );
} else if( isIncluded ){
program.verbose && process.stderr.write( 'Compiling ' + tpl.filename + ' with vash@' + i + '...\n' );
// handle api change
if( semver.gt( vashes[i].version, '0.2.1' ) ){
tpl.compiled[ i ] = vashes[i].compile( tpl.str, tpl.config || options );
} else {
// only for <= 0.2.1
tpl.compiled[ i ] = vashes[i].tpl( tpl.str, tpl.config || options );
}
if( program.dump ){
console.log( clc.xterm(178)(tpl.name), clc.xterm(104)('vash@' + vashes[i].version) )
console.log( tpl.compiled[ i ].toString() )
}
}
} catch(e){
program.verbose && process.stderr.write('Compile error with vash@' + i + ', continuing...\n');
program.verbose && process.stderr.write( e.message + '\n' );
}
})
})
next( null, tpls );
}
var diffAll = function( tpls ){
tpls.forEach(function( tpl ){
var base;
Object.keys(tpl.compiled).forEach(function(v, i){
if( i === 0 ){
base = utils.diff.bind(null, tpl.compiled[v]( tpl.model ), v)
return;
}
var output = base( tpl.compiled[v]( tpl.model ), v ).split('\n')
console.log( output.shift() );
console.log( utils.linenos( output.join('\n') ) );
})
})
}
var diffCompiled = function( tpls ){
tpls.forEach(function( tpl ){
var base;
Object.keys(tpl.compiled).forEach(function(v, i){
if( i === 0 ){
base = utils.diff.bind(null, tpl.compiled[v].toString(), v)
return;
}
var output = base( tpl.compiled[v].toString(), v ).split('\n')
console.log( output.shift() );
console.log( utils.linenos( output.join('\n') ) );
})
});
}
var mkBench = function( tpls, next ){
tpls.forEach(function( tpl ){
var suite = new Benchmark.Suite({
name: tpl.filename + ( tpl.name ? ' ' + tpl.name : '' )
});
Object.keys(tpl.compiled).forEach(function(v){
suite.add( v, {
fn: function(){
tpl.compiled[v]( tpl.model );
}
})
})
if( program.chart ){
var chart = new BenchmarkCliChart( suite, { fields: program.chart } )
} else {
suite.on('cycle', function( ev ){
var obj = {
suite: suite.name
,vashv: ev.target.name
,hz: ~~ev.target.hz // ops / sec
,stats: ev.target.stats
};
console.log( JSON.stringify( obj ) )
})
suite.on('complete', function(){
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
}
suite.run();
})
next( null );
}
function splitVersions(versions){
return versions.split(',');
}
program
// input options
.option('-ve, --vexclude <versions>', 'Comma-delimited semver versions of vash to exclude from the benchmarks', splitVersions, [])
.option('-vi, --vinclude <versions>', 'Comma-delimited semver versions of vash to include in the benchmarks', splitVersions, [])
.option('-te, --texclude <filenames>', 'Comma-delimited template filenames to exclude', splitVersions, [])
.option('-ti, --tinclude <filenames>', 'Comma-delimited template filenames to include', splitVersions, [])
.option('-l, --localversion <vash path>', 'Provide a `require` path to a local copy of vash to include in benchmarks', '')
// query options
.option('-vs, --versions', 'Output valid vash versions and exit', false)
// output options
.option('-c, --chart [columns]', 'Output a chart instead of JSON, with optional comma-delimited column names: vashv,bar,ops,rme,percent,om,xfast. Set to false to output JSON instead', splitVersions, ['vashv','bar','ops','rme','percent'])
.option('-d, --dump', 'Output the decompiled templates without benchmarking', false)
.option('-dc, --diffcompiled', 'Diff the decompiled templates without benchmarking', false)
.option('-vf, --verify', 'Diff the rendered template output instead of benchmarking, using newest version as base', false)
.option('-v, --verbose', 'More output', false)
// TODO: add --compile option, to bench compile times
// TOOD: try forking each benchmark for multicore, for sake of duration
program.parse(process.argv);
//console.log(program);
loadTemplates(function( err, tpls ){
requireVashes(function( err, vashes ){
compileWithVersions( tpls, vashes, function( err, tpls ){
//console.log( tpls );
if( program.dump ){
process.exit();
}
if( program.verify ){
diffAll( tpls, function(){} );
process.exit();
}
if( program.diffcompiled ){
diffCompiled( tpls, function(){} );
process.exit();
}
mkBench( tpls, function( err, suite ){
//console.log('done?')
})
})
})
})