Commit face5ef8 authored by Thomas Urban's avatar Thomas Urban

adding code collecting more detailed stats per client and per URL

parent 6703ea4d
......@@ -76,15 +76,26 @@ class Client extends EventEmitter {
this.id = clientID;
if ( noisy ) {
this.logInfo = debug( "client " + clientID + " info ");
this.logInfo = debug( "client:info:" + clientID );
this.logStats = debug( "client:stats:" + clientID );
} else {
this.logInfo = () => {};
this.logStats = () => {};
}
this.logError = debug( "client " + clientID + " ERROR" );
this.logError = debug( "client:ERROR:" + clientID );
this.success = 0;
this.failed = 0;
this.min = Infinity;
this.max = -Infinity;
this.avg = NaN;
this.urls = {};
this.errors = {};
this.statsTimer = setInterval( () => this.dumpStats(), noisy ? 60000 : 300000 );
}
/**
......@@ -96,6 +107,8 @@ class Client extends EventEmitter {
fetch( url ) {
this.logInfo( `fetching URL ${url}` );
const start = Date.now();
return new Promise( ( resolve, reject ) => {
const options = {
headers: {
......@@ -110,7 +123,7 @@ class Client extends EventEmitter {
( res.headers["set-cookie"] || [] )
.forEach( cookie => {
const match = /^\s*([^=]+)\s*=\s*([^;]*)/;
const match = /^\s*([^=]+)\s*=\s*([^;]*)/.exec( cookie );
if ( match ) {
this.cookies[match[1]] = match[2];
}
......@@ -118,7 +131,7 @@ class Client extends EventEmitter {
if ( res.statusCode >= 400 ) {
this.logError( `ERROR: fetching URL ${url} failed with ${res.statusCode}` );
reject( new Error( `fetching URL ${url} failed with ${res.statusCode}` ) );
reject( Object.assign( new Error( `fetching URL ${url} failed with ${res.statusCode}` ), { httpStatus: res.statusCode } ) );
res.destroy();
return;
}
......@@ -166,13 +179,77 @@ class Client extends EventEmitter {
request.end();
} )
.then( data => {
this.success++;
this.updateStats( url, null, start );
return data;
}, error => {
this.failed++;
this.updateStats( url, error.httpStatus || true, start );
throw error;
} );
}
updateStats( url, error, start ) {
const end = Date.now();
const durationMs = ( end - start );
if ( !this.urls[url] ) {
this.urls[url] = { success: 0, failed: 0, first: start, min: Infinity, max: -Infinity, avg: NaN, errors: {} };
}
const stats = this.urls[url];
stats.latest = start;
if ( error ) {
const _code = parseInt( error );
if ( !isNaN( _code ) ) {
stats.errors[_code] = ( stats.errors[_code] || 0 ) + 1;
this.errors[_code] = ( this.errors[_code] || 0 ) + 1;
}
this.failed++;
stats.failed++;
} else {
this.success++;
stats.success++;
}
// update timing stats of URL
if ( durationMs < stats.min ) {
stats.min = durationMs;
}
if ( durationMs > stats.max ) {
stats.max = durationMs;
}
const num = stats.failed + stats.success;
stats.avg = ( ( ( stats.avg || 0 ) * ( num - 1 ) ) + durationMs ) / num;
// update timing stats of client
if ( durationMs < this.min ) {
this.min = durationMs;
}
if ( durationMs > this.max ) {
this.max = durationMs;
}
const numTotal = this.failed + this.success;
this.avg = ( ( ( this.avg || 0 ) * ( numTotal - 1 ) ) + durationMs ) / numTotal;
}
dumpStats( full = false ) {
this.logStats( `requests: ${this.success + this.failed} - failed: ${this.failed} - min: ${this.time( this.min )} - max: ${this.time( this.max )} - avg: ${this.time( this.avg )}` );
}
time( ms ) {
const _ms = Math.round( ms );
return _ms < 1000 ? `${_ms} ms` : `${Math.round( _ms / 100 ) / 10 } s`;
}
}
......@@ -258,7 +335,7 @@ new Promise( resolve => {
}
} )
.then( list => {
File.writeFileSync( cacheFile, list.join( process.platform === "win32" ? "\r\n" : "\n" ) )
File.writeFileSync( cacheFile, list.join( process.platform === "win32" ? "\r\n" : "\n" ) );
return list;
} );
......@@ -276,7 +353,7 @@ new Promise( resolve => {
const clients = new Array( NumClients )
.fill( 0 )
.map( ( _, i ) => new Client( ( "#" + ( i + 1 ) ).padStart( 4 ) ) );
.map( ( _, i ) => new Client( String( i + 1 ).padStart( 4, "0" ) ) );
clients.forEach( client => {
let tid = setTimeout( fetch, randomDelay() / 100 );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment