Skip to content

Instantly share code, notes, and snippets.

@richardfriedman
Created December 8, 2016 15:26
Show Gist options
  • Save richardfriedman/3b075bbb092f71668dab2270bda2f9c1 to your computer and use it in GitHub Desktop.
Save richardfriedman/3b075bbb092f71668dab2270bda2f9c1 to your computer and use it in GitHub Desktop.
var LoadTestingSession = require("./loadTestingSession.js");
var fs = require('fs');
var readline = require('readline');
function ReplayJson(redlineApi, testNum, rand, config)
{
// Redline API
this.redlineApi = redlineApi;
// Test info
this.testNum = testNum;
this.rand = rand;
// INI Config
this.config = config;
this.remainingIterations = 1;
this.basePath = 'http://bleacherreport.com';
}
/** Run test */
ReplayJson.prototype.runTest = function(callback)
{
var that = this;
var session = new LoadTestingSession(that.testNum, that.redlineApi, true);
// Find JSON in CWD which is not package.json ;)
fs.readdir( '.', function(err, files){
if ( err || !files ) {
callback( that, true );
} else {
files.forEach( function(file){
if ( file.endsWith( '.log' ) ){
var lineReader = readline.createInterface({
input: fs.createReadStream(file)
});
var index = 0;
var completed = 0;
var finalized = false;
lineReader.on('close', function(){
finalized = true;
if ( completed >= index ){
console.log( "Completed Test (on close)");
callback( that, true );
}
});
lineReader.on('line', function (line) {
index++;
try {
var j = JSON.parse( line );
if ( j.method == 'GET' && j.path ){
var url = that.basePath + j.path;
if ( j.params ){
var q = "";
for ( var prop in j.params ){
q += (q.length?'&':'?');
q += prop + '=' + j.params[prop];
}
url += q;
}
console.log( index, url );
session.loadPage(url, function(failed) {
completed++;
console.log("Page Completed " + completed );
if ( finalized && completed >= index ){
console.log( "Completed Test");
callback( that, true );
}
});
}
}
catch(err){
completed++;
that.redlineApi.recordError( "Line " + index + ":" + err.message );
console.log( "Line " + index + ":" + err.message );
if ( finalized && completed >= index ){
console.log( "Completed Test (last line error)");
callback( that, true );
}
}
});
}
})
}
});
};
module.exports = ReplayJson;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment