Last active
January 18, 2017 23:21
-
-
Save jamesmontalvo3/24332b5d02ab2ee04d50613a84d983a1 to your computer and use it in GitHub Desktop.
Even simpler load testing script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A very simple load-testing script | |
// Fires off requests to MediaWiki random page, fires another after receiving response | |
// Optionally multi-threaded. | |
var help = "Commands: \n" | |
+ "showData(): Display data in tab-delimited format\n" | |
+ "start(): Start sending requests\n" | |
+ "stop(): Stop sending requests\n" | |
+ "doForSeconds(seconds): Send requests for specified number of seconds\n" | |
+ "doForMinutes(minutes): Same as above, but for minutes\n" | |
+ "doMultiForSeconds(seconds, threads): Same as doForSeconds() but with multiple\n" | |
+ " requests sent at a time.\n" | |
+ "doMultiForMinutes(minutes, threads): Same as above but for minutes.\n"; | |
var startTime, | |
testData = [], | |
usingThreads; | |
function doRequest () { | |
startTime = Date.now(); | |
$.get( | |
"/eva/index.php/Special:Random", | |
{}, | |
function( resp ){ | |
var title = resp.match( /<title>(.*?)<\/title>/ ); | |
title = title && title[1] ? title[1] : "Title read failed"; | |
var endTime = Date.now(); | |
var dataRow = { | |
start: startTime, | |
end: endTime, | |
duration: endTime - startTime, | |
threads: usingThreads, | |
title: title, | |
}; | |
testData.push( dataRow ); | |
console.log( JSON.stringify(dataRow) ); | |
if ( ! stopRequests ) { | |
doRequest(); | |
} | |
else { | |
console.log( "Stop command encountered." ); | |
console.log( help ); | |
} | |
} | |
); | |
} | |
var stopRequests = false; | |
function stop () { | |
console.log( "Stopping..." ); | |
stopRequests = true; | |
} | |
function start () { | |
stopRequests = false; | |
usingThreads = 1; | |
doRequest(); | |
} | |
function endInSeconds ( seconds ) { | |
setTimeout( stop, seconds * 1000 ); | |
} | |
function doForSeconds ( seconds ) { | |
start(); | |
endInSeconds( seconds ); | |
} | |
function doForMinutes ( minutes ) { | |
doForSeconds( minutes * 60 ); | |
} | |
function doMultiForSeconds ( seconds, threads ) { | |
stopRequests = false; | |
threads = parseInt(threads) > 0 ? parseInt(threads) : 1; | |
usingThreads = threads; | |
for( var i = 0; i < threads; i++ ) { | |
doRequest(); | |
} | |
endInSeconds( seconds ); | |
} | |
function doMultiForMinutes ( minutes, threads ) { | |
doMultiForSeconds( minutes * 60, threads ); | |
} | |
function showData () { | |
var output = "Start\tDuration\tThreads\tTitle", | |
start, | |
pad = function ( n, width, z ) { | |
z = z || '0'; | |
n = n + ''; | |
width = width || 2; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
}; | |
for ( var t = 0; t < testData.length; t++ ) { | |
start = new Date( testData[t].start ); | |
start = [ | |
pad( start.getHours() ), | |
pad( start.getMinutes() ), | |
pad( start.getSeconds() ) | |
].join(":"); | |
output += "\n" | |
+ [ | |
start, | |
testData[t].duration, | |
testData[t].threads, | |
testData[t].title | |
].join("\t"); | |
} | |
console.log( output ); | |
} | |
console.log( help ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment