Skip to content

Instantly share code, notes, and snippets.

@ikekou
Created August 15, 2014 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikekou/c309f55e6e2dd274ef6e to your computer and use it in GitHub Desktop.
Save ikekou/c309f55e6e2dd274ef6e to your computer and use it in GitHub Desktop.
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