Last active
January 18, 2017 20:23
-
-
Save jamesmontalvo3/6ecf9bd620c33af1f96abfdb5c101ca2 to your computer and use it in GitHub Desktop.
Very simple load tester for MediaWiki
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
window.data = {}; | |
window.lastUid = null; | |
window.cooldown = 0; | |
window.pending = {}; | |
function requestPage ( interval, loadTestEnd ) { | |
// var milliseconds = Date.now(); | |
var uid = makeid(); | |
window.data[uid] = { | |
uid: uid, | |
start: Date.now(), | |
end: null, | |
duration: null, | |
title: "" | |
}; | |
window.lastUid = uid; | |
window.pending[uid] = true; | |
$.get( | |
"/eva/index.php/Special:Random", | |
{}, | |
function( resp ){ | |
// remove this one from pending | |
window.pending[uid] | |
delete window.pending[uid]; | |
var end = Date.now(), | |
duration = end - window.data[uid].start; | |
window.data[uid].end = end; | |
window.data[uid].duration = duration; | |
var title = resp.match( /<title>(.*?)<\/title>/ ); | |
window.data[uid].title = title && title[1] ? title[1] : "Title read failed"; | |
window.data[uid].interval = interval; | |
console.log( | |
JSON.stringify({ | |
duration: duration, | |
interval: interval, | |
remaining_seconds: parseInt( ( loadTestEnd - Date.now() ) / 1000 ), | |
allTestRequestsSent: allTestRequestsSent, | |
lastUid: lastUid, | |
uid: uid | |
}) | |
); | |
// controller (loadTest method) has determined test is complete | |
// and there are no more pending requests | |
if ( allTestRequestsSent === true && Object.keys(window.pending).length === 0 ) { | |
console.log( "Complete with final request for this test" ); | |
loadNextTest(); | |
} | |
if ( window.lastUid === uid ) { | |
window.lastUid = null; | |
} | |
} | |
); | |
return uid; | |
} | |
function makeid () { | |
var text = ""; | |
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
for( var i=0; i < 20; i++ ) | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
return text; | |
} | |
function loadTest ( interval, duration ) { | |
var interval = interval || 500, // default 500ms | |
duration = duration || 60, // default 60 seconds | |
durationMS = parseInt( duration * 1000 ); // duration in seconds * 1000 ms/sec | |
window.loadTestStart = Date.now(); | |
window.loadTestEnd = loadTestStart + durationMS; | |
console.log( "Starting test\n" | |
+ "-------------\n" | |
+ " Interval: " + interval + " milliseconds\n" | |
+ " Duration: " + duration + " seconds\n" | |
); | |
// fire one off now, before doing setInteval | |
requestPage( interval ); | |
window.intervalID = setInterval( function() { | |
if ( Date.now() < loadTestEnd ) { | |
requestPage( interval, loadTestEnd ); | |
} | |
else { | |
clearInterval( window.intervalID ); | |
// test has ended, and last request has already responded | |
if ( ! window.lastUid ) { | |
loadNextTest(); | |
} | |
// make the final request wrap things up | |
else { | |
window.allTestRequestsSent = true; | |
} | |
} | |
}, interval ); | |
} | |
function loadTests ( tests ) { | |
window.loadTests = tests; | |
loadNextTest(); | |
} | |
function loadNextTest () { | |
window.allTestRequestsSent = false; | |
if ( window.loadTests.length ) { | |
var test = window.loadTests.shift(); | |
var interval = test.interval || 500, // default 500ms | |
duration = test.duration || 60; // default 60 seconds | |
console.log( "Starting new test in " + window.cooldown + " seconds" ); | |
setTimeout( function () { | |
loadTest( interval, duration ); | |
}, window.cooldown * 1000 ); | |
window.cooldown = test.cooldown || window.cooldown; | |
} | |
else { | |
console.log( "ALL LOAD TESTS COMPLETE" ); | |
console.log( testSerialize() ); | |
} | |
} | |
function testSerialize () { | |
var serial = []; | |
for ( var t in window.data ) { | |
serial.push( window.data[t] ); | |
} | |
serial.sort( function( a, b ) { | |
return a.start - b.start; | |
}); | |
var output = "Start\tDuration\tInterval\tTitle", | |
start; | |
for ( var t = 0; t < serial.length; t++ ) { | |
start = new Date( serial[t].start ); | |
start = [ | |
pad( start.getHours() ), | |
pad( start.getMinutes() ), | |
pad( start.getSeconds() ) | |
].join(":"); | |
output += "\n" | |
+ [ | |
start, | |
serial[t].duration, | |
serial[t].interval, | |
serial[t].title | |
].join("\t"); | |
} | |
return output; | |
} | |
function pad( 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; | |
} | |
loadTests([ | |
{ interval: 600, duration: 180, cooldown: 30 }, | |
{ interval: 550, duration: 180, cooldown: 30 }, | |
{ interval: 500, duration: 180, cooldown: 30 }, | |
{ interval: 450, duration: 180, cooldown: 30 }, | |
{ interval: 400, duration: 180, cooldown: 30 }, | |
{ interval: 350, duration: 180, cooldown: 30 } | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment