33const path = require ( "path" ) ;
44const fs = require ( "fs" ) ;
55const asyncLib = require ( "neo-async" ) ;
6-
76const Benchmark = require ( "benchmark" ) ;
7+ const { remove} = require ( "./helpers/remove" ) ;
88
99describe ( "BenchmarkTestCases" , function ( ) {
1010 const casesPath = path . join ( __dirname , "benchmarkCases" ) ;
@@ -62,7 +62,11 @@ describe("BenchmarkTestCases", function() {
6262 err => {
6363 if ( err ) return callback ( err ) ;
6464 fs . writeFileSync ( gitIndex , index ) ;
65- doLoadWebpack ( ) ;
65+ try {
66+ doLoadWebpack ( ) ;
67+ } catch ( err ) {
68+ callback ( err ) ;
69+ }
6670 }
6771 ) ;
6872 }
@@ -72,7 +76,7 @@ describe("BenchmarkTestCases", function() {
7276 }
7377
7478 function doLoadWebpack ( ) {
75- const baselineWebpack = require ( path . resolve (
79+ const baselineWebpack = require . requireActual ( path . resolve (
7680 baselinePath ,
7781 "lib/webpack.js"
7882 ) ) ;
@@ -84,14 +88,24 @@ describe("BenchmarkTestCases", function() {
8488 callback ( ) ;
8589 }
8690 } ,
87- done
91+ ( err ) => {
92+ if ( err ) {
93+ done ( err ) ;
94+ }
95+ createTests ( ) ;
96+ done ( ) ;
97+ }
8898 ) ;
8999 } ) ;
90100 } , 270000 ) ;
91101
102+ afterAll ( ( ) => {
103+ remove ( baselinesPath ) ;
104+ } )
105+
92106 function getBaselineRevs ( rootPath , callback ) {
93107 const git = require ( "simple-git" ) ( rootPath ) ;
94- const lastVersionTag = "v" + require ( "../package.json" ) . version ;
108+ const lastVersionTag = "v" + require . requireActual ( "../package.json" ) . version ;
95109 git . raw ( [ "rev-list" , "-n" , "1" , lastVersionTag ] , ( err , resultVersion ) => {
96110 if ( err ) return callback ( err ) ;
97111 const matchVersion = / ^ ( [ a - f 0 - 9 ] + ) \s * $ / . exec ( resultVersion ) ;
@@ -258,86 +272,84 @@ describe("BenchmarkTestCases", function() {
258272 } ) ;
259273 }
260274
261- tests . forEach ( testName => {
262- const testDirectory = path . join ( casesPath , testName ) ;
263- let headStats = null ;
264- describe ( `${ testName } create benchmarks` , function ( ) {
265- baselines . forEach ( baseline => {
266- let baselineStats = null ;
267- it (
268- `should benchmark ${ baseline . name } (${ baseline . rev } )` ,
269- function ( done ) {
270- const outputDirectory = path . join (
271- __dirname ,
272- "js" ,
273- "benchmark" ,
274- `baseline-${ baseline . name } ` ,
275- testName
276- ) ;
277- const config = Object . create (
278- require ( path . join ( testDirectory , "webpack.config.js" ) )
279- ) ;
280- config . output = Object . create ( config . output || { } ) ;
281- if ( ! config . context ) config . context = testDirectory ;
282- if ( ! config . output . path ) config . output . path = outputDirectory ;
283- runBenchmark ( baseline . webpack , config , ( err , stats ) => {
284- if ( err ) return done ( err ) ;
285- console . log ( ` ${ baseline . name } ${ stats . text } ` ) ;
286- if ( baseline . name === "HEAD" ) headStats = stats ;
287- else baselineStats = stats ;
288- done ( ) ;
289- } ) ;
290- } ,
291- 180000
292- ) ;
293-
294- it (
295- `should benchmark ${ baseline . name } (${ baseline . rev } )` ,
296- done => {
297- const outputDirectory = path . join (
298- __dirname ,
299- "js" ,
300- "benchmark" ,
301- `baseline-${ baseline . name } ` ,
302- testName
303- ) ;
304- const config = Object . create (
305- require ( path . join ( testDirectory , "webpack.config.js" ) )
306- ) ;
307- config . output = Object . create ( config . output || { } ) ;
308- if ( ! config . context ) config . context = testDirectory ;
309- if ( ! config . output . path ) config . output . path = outputDirectory ;
310- runBenchmark ( baseline . webpack , config , ( err , stats ) => {
311- if ( err ) return done ( err ) ;
312- console . log ( ` ${ baseline . name } ${ stats . text } ` ) ;
313- if ( baseline . name === "HEAD" ) headStats = stats ;
314- else baselineStats = stats ;
315- done ( ) ;
316- } ) ;
317- } ,
318- 180000
319- ) ;
320-
321- if ( baseline . name !== "HEAD" ) {
322- it ( `HEAD should not be slower than ${ baseline . name } (${
323- baseline . rev
324- } )`, function ( ) {
325- if ( baselineStats . maxConfidence < headStats . minConfidence ) {
326- throw new Error (
327- `HEAD (${ headStats . text } ) is slower than ${ baseline . name } (${
328- baselineStats . text
329- } ) (90% confidence)`
275+ function createTests ( ) {
276+ tests . forEach ( testName => {
277+ const testDirectory = path . join ( casesPath , testName ) ;
278+ let headStats = null ;
279+ describe ( `${ testName } create benchmarks` , function ( ) {
280+ baselines . forEach ( baseline => {
281+ let baselineStats = null ;
282+ it (
283+ `should benchmark ${ baseline . name } (${ baseline . rev } )` ,
284+ function ( done ) {
285+ const outputDirectory = path . join (
286+ __dirname ,
287+ "js" ,
288+ "benchmark" ,
289+ `baseline-${ baseline . name } ` ,
290+ testName
330291 ) ;
331- } else if ( baselineStats . minConfidence > headStats . maxConfidence ) {
332- console . log (
333- `======> HEAD is ${ Math . round (
334- baselineStats . mean / headStats . mean * 100 - 100
335- ) } % faster than ${ baseline . name } (90% confidence)!`
292+ const config = require . requireActual ( path . join ( testDirectory , "webpack.config.js" ) ) || { }
293+ config . output = config . output || { } ;
294+ if ( ! config . context ) config . context = testDirectory ;
295+ if ( ! config . output . path ) config . output . path = outputDirectory ;
296+ runBenchmark ( baseline . webpack , config , ( err , stats ) => {
297+ if ( err ) return done ( err ) ;
298+ console . log ( ` ${ baseline . name } ${ stats . text } ` ) ;
299+ if ( baseline . name === "HEAD" ) headStats = stats ;
300+ else baselineStats = stats ;
301+ done ( ) ;
302+ } ) ;
303+ } ,
304+ 180000
305+ ) ;
306+
307+ it (
308+ `should benchmark ${ baseline . name } (${ baseline . rev } )` ,
309+ done => {
310+ const outputDirectory = path . join (
311+ __dirname ,
312+ "js" ,
313+ "benchmark" ,
314+ `baseline-${ baseline . name } ` ,
315+ testName
336316 ) ;
337- }
338- } ) ;
339- }
317+ const config = require . requireActual ( path . join ( testDirectory , "webpack.config.js" ) ) || { } ;
318+ config . output = config . output || { } ;
319+ if ( ! config . context ) config . context = testDirectory ;
320+ if ( ! config . output . path ) config . output . path = outputDirectory ;
321+ runBenchmark ( baseline . webpack , config , ( err , stats ) => {
322+ if ( err ) return done ( err ) ;
323+ console . log ( ` ${ baseline . name } ${ stats . text } ` ) ;
324+ if ( baseline . name === "HEAD" ) headStats = stats ;
325+ else baselineStats = stats ;
326+ done ( ) ;
327+ } ) ;
328+ } ,
329+ 180000
330+ ) ;
331+
332+ if ( baseline . name !== "HEAD" ) {
333+ it ( `HEAD should not be slower than ${ baseline . name } (${
334+ baseline . rev
335+ } )`, function ( ) {
336+ if ( baselineStats . maxConfidence < headStats . minConfidence ) {
337+ throw new Error (
338+ `HEAD (${ headStats . text } ) is slower than ${ baseline . name } (${
339+ baselineStats . text
340+ } ) (90% confidence)`
341+ ) ;
342+ } else if ( baselineStats . minConfidence > headStats . maxConfidence ) {
343+ console . log (
344+ `======> HEAD is ${ Math . round (
345+ baselineStats . mean / headStats . mean * 100 - 100
346+ ) } % faster than ${ baseline . name } (90% confidence)!`
347+ ) ;
348+ }
349+ } ) ;
350+ }
351+ } ) ;
340352 } ) ;
341353 } ) ;
342- } ) ;
354+ }
343355} ) ;
0 commit comments