2016-11-16 116 views
0

我試過benchmark.js,它似乎在運行基準測試並向我展示最快的測試,我不知道如何從中獲得一個很好的報告我試過這個:如何從benchmark.js中獲取報告

suite.add('My#test', function() { 
    console.log("test") 
}).on('complete', function() { 
     console.log('Fastest is ' + this.filter('fastest').map('name')); 
     console.log('stats: ' + suite.stats) // but stats seems undefined, do i miss anything? how come I couldn't find a guide on showing how to print stats? 
}).run({ 'async': true }); 

數據似乎不確定,我錯過了什麼?爲什麼我找不到關於如何打印統計信息的指南?我如何獲得一份報告,告訴我爲測試每種方法運行測試需要多少時間,中位數,錯誤數量和所有這些總結在一起?謝謝。

+0

檢查這個【答案】(http://stackoverflow.com/a/27999506/453767) –

回答

0

您應該使用this

// add listeners 
suite.on('cycle', function(event) { 
    console.log(String(event.target)); 
    console.log(event.target.name); 
    console.log(event.target.stats.rme); 
    console.log(event.target.stats.sample.length); 
    console.log(event.target.count); // The number of times a test was executed. 
    console.log(event.target.cycles); // The number of cycles performed while benchmarking. 
    console.log(event.target.hz); //The number of executions per second. 
}) 
.on('complete', function() { 
    for (var i = 0; i < this.length; i++) { 
     console.log(this[i].hz + " ops/sec"); 
     console.log(this[i].stats.sample.length); 
     //console.log(this[i].stats); 
    } 
    console.log(color('Fastest is ' + this.filter('fastest').map('name'),'green')); 
    console.log(color('Slowest is ' + this.filter('slowest').map('name'),'red')); 
}) 
// run async 
.run({ 'async': true });