@@ -40,9 +40,18 @@ describe("BenchmarkTestCases", function() {
4040 try {
4141 fs . mkdirSync ( baselinePath ) ;
4242 } catch ( e ) { }
43- git ( baselinePath ) . raw ( [ "--git-dir" , path . join ( rootPath , ".git" ) , "reset" , "--hard" , baselineRevision ] , err => {
43+ const gitIndex = path . resolve ( rootPath , ".git/index" ) ;
44+ const index = fs . readFileSync ( gitIndex ) ;
45+ git ( rootPath ) . raw ( [ "rev-list" , "-n" , "1" , "HEAD" ] , ( err , prevHead ) => {
4446 if ( err ) return callback ( err ) ;
45- doLoadWebpack ( ) ;
47+ git ( baselinePath ) . raw ( [ "--git-dir" , path . join ( rootPath , ".git" ) , "reset" , "--hard" , baselineRevision ] , err => {
48+ if ( err ) return callback ( err ) ;
49+ git ( rootPath ) . raw ( [ "reset" , "--soft" , prevHead . split ( "\n" ) [ 0 ] ] , err => {
50+ if ( err ) return callback ( err ) ;
51+ fs . writeFileSync ( gitIndex , index ) ;
52+ doLoadWebpack ( ) ;
53+ } ) ;
54+ } ) ;
4655 } ) ;
4756 }
4857
@@ -100,6 +109,25 @@ describe("BenchmarkTestCases", function() {
100109 } ) ;
101110 }
102111
112+ function tDistribution ( n ) {
113+ // two-sided, 90%
114+ // https://en.wikipedia.org/wiki/Student%27s_t-distribution
115+ if ( n <= 30 ) {
116+ // 1 2 ...
117+ const data = [ 6.314 , 2.920 , 2.353 , 2.132 , 2.015 , 1.943 , 1.895 , 1.860 , 1.833 , 1.812 , 1.796 , 1.782 , 1.771 , 1.761 , 1.753 , 1.746 , 1.740 , 1.734 , 1.729 , 1.725 , 1.721 , 1.717 , 1.714 , 1.711 , 1.708 , 1.706 , 1.703 , 1.701 , 1.699 , 1.697 ] ;
118+ return data [ n - 1 ] ;
119+ } else if ( n <= 120 ) {
120+ // 30 40 50 60 70 80 90 100 110 120
121+ const data = [ 1.697 , 1.684 , 1.676 , 1.671 , 1.667 , 1.664 , 1.662 , 1.660 , 1.659 , 1.658 ] ;
122+ var a = data [ Math . floor ( n / 10 ) - 3 ] ;
123+ var b = data [ Math . ceil ( n / 10 ) - 3 ] ;
124+ var f = n / 10 - Math . floor ( n / 10 ) ;
125+ return a * ( 1 - f ) + b * f ;
126+ } else {
127+ return 1.645 ;
128+ }
129+ }
130+
103131 function runBenchmark ( webpack , config , callback ) {
104132 // warmup
105133 const warmupCompiler = webpack ( config , ( err , stats ) => {
@@ -108,11 +136,11 @@ describe("BenchmarkTestCases", function() {
108136 const compiler = webpack ( config , ( err , stats ) => {
109137 compiler . purgeInputFileSystem ( ) ;
110138 if ( err ) {
111- deferred . reject ( err ) ;
139+ callback ( err ) ;
112140 return ;
113141 }
114142 if ( stats . hasErrors ( ) ) {
115- deferred . reject ( new Error ( stats . toJson ( ) . errors . join ( "\n\n" ) ) ) ;
143+ callback ( new Error ( stats . toJson ( ) . errors . join ( "\n\n" ) ) ) ;
116144 return ;
117145 }
118146 deferred . resolve ( ) ;
@@ -122,6 +150,13 @@ describe("BenchmarkTestCases", function() {
122150 defer : true ,
123151 initCount : 1 ,
124152 onComplete : function ( ) {
153+ const stats = bench . stats ;
154+ const n = stats . sample . length ;
155+ const nSqrt = Math . sqrt ( n ) ;
156+ const z = tDistribution ( n - 1 ) ;
157+ stats . minConfidence = stats . mean - z * stats . deviation / nSqrt ;
158+ stats . maxConfidence = stats . mean + z * stats . deviation / nSqrt ;
159+ stats . text = `${ Math . round ( stats . mean * 1000 ) } ms ± ${ Math . round ( stats . deviation * 1000 ) } ms [${ Math . round ( stats . minConfidence * 1000 ) } ms; ${ Math . round ( stats . maxConfidence * 1000 ) } ms]` ;
125160 callback ( null , bench . stats ) ;
126161 } ,
127162 onError : callback
@@ -153,7 +188,7 @@ describe("BenchmarkTestCases", function() {
153188 if ( ! config . output . path ) config . output . path = outputDirectory ;
154189 runBenchmark ( baseline . webpack , config , ( err , stats ) => {
155190 if ( err ) return done ( err ) ;
156- console . log ( ` ${ baseline . name } ${ Math . round ( stats . mean * 1000 ) } ms ± ${ Math . round ( stats . deviation * 1000 ) } ms ` ) ;
191+ console . log ( ` ${ baseline . name } ${ stats . text } ` ) ;
157192 if ( baseline . name === "HEAD" )
158193 headStats = stats ;
159194 else
@@ -164,10 +199,10 @@ describe("BenchmarkTestCases", function() {
164199
165200 if ( baseline . name !== "HEAD" ) {
166201 it ( `HEAD should not be slower than ${ baseline . name } (${ baseline . rev } )` , function ( ) {
167- if ( baselineStats . mean + baselineStats . deviation < headStats . mean - headStats . deviation ) {
168- throw new Error ( `HEAD (${ baselineStats . mean } ± ${ baselineStats . deviation } ) is slower than ${ baseline . name } (${ headStats . mean } ± ${ headStats . deviation } )` ) ;
169- } else if ( baselineStats . mean - baselineStats . deviation > headStats . mean + headStats . deviation ) {
170- console . log ( `======> HEAD is ${ Math . round ( baselineStats . mean / headStats . mean * 100 - 100 ) } % faster than ${ baseline . name } !` ) ;
202+ if ( baselineStats . maxConfidence < headStats . minConfidence ) {
203+ throw new Error ( `HEAD (${ headStats . text } ) is slower than ${ baseline . name } (${ baselineStats . text } ) (90% confidence )` ) ;
204+ } else if ( baselineStats . minConfidence > headStats . maxConfidence ) {
205+ console . log ( `======> HEAD is ${ Math . round ( baselineStats . mean / headStats . mean * 100 - 100 ) } % faster than ${ baseline . name } (90% confidence) !` ) ;
171206 }
172207 } ) ;
173208 }
0 commit comments