Skip to content

Instantly share code, notes, and snippets.

@ikekou ikekou/main.js
Created Aug 15, 2014

Embed
What would you like to do?
node.jsとphantomasで複数回計測していくつかの項目で平均とる
var exec=require('child_process').exec;
var arr=[
'requests',
'httpsRequests',
'bodySize',
'contentLength',
'httpTrafficCompleted',
'timeToLastByte',
'timeToFirstByte',
'ajaxRequests',
'htmlCount',
'htmlSize',
'cssCount',
'cssSize',
'jsCount',
'jsSize',
'imageCount',
'imageSize',
'otherCount',
'otherSize',
'DOMelementsCount',
'DOMelementMaxDepth'
];
var maxTry=5;
var tryCount=0;
var results={};
var url='https://www.google.co.jp';
arr.forEach(function(v,i){
results[v]=[];
});
phantomas();
function phantomas(){
var child=exec('phantomas --url '+url+' --reporter json', function(err,stdout,stderr){
if (!err) {
// console.log('stdout: ' + stdout);
// console.log('stderr: ' + stderr)
var json=JSON.parse(stdout);
var m=json.metrics;
arr.forEach(function(v,i){
results[v].push(m[v]);
});
tryCount++;
if(tryCount<maxTry){
phantomas();
}else{
complete();
}
} else {
console.log(err);
// err.code will be the exit code of the child process
console.log(err.code);
// err.signal will be set to the signal that terminated the process
console.log(err.signal);
}
})
}
function complete(){
var averages={};
arr.forEach(function(v,i){
var sum=0;
results[v].forEach(function(vv,ii){
sum+=vv;
});
var average=sum/maxTry;
averages[v]=average;
});
console.log({
results:results,
averages:averages
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.