Skip to content

Instantly share code, notes, and snippets.

@liron-navon
Last active October 14, 2021 08:26
Show Gist options
  • Select an option

  • Save liron-navon/8e11426614286d76691aa79e03ed9636 to your computer and use it in GitHub Desktop.

Select an option

Save liron-navon/8e11426614286d76691aa79e03ed9636 to your computer and use it in GitHub Desktop.
Measure tests time in code signal
/**
* This script is used to run measurements on CodeSignal https://app.codesignal.com
* to use it simply open the dev tools, and paste in the console,
* it will automate the browser for you to run the tests and measure the times it took.
* be patient, there are measurements to prevent blocking from CodeSignal,
* and this script just automates the browser which means it cannot run tests in parallel
* so the tests will take a while, just run this and go grab a coffee.
*
* !!!CAUTION!!! this isn't an official script and might get you banned from CodeSignal
* even though there are measurements to try and prevent this, use caution.
*
* ***NOTICE*** by no means this tool is accurate, but it's a good way to know
* if there is a big difference in the implementations you made for the same test
*
* Some slow computers will need to adjust the timeToWaitWhenObservingButtonTextChange number.
**/
// set this to define how many times to run the tests
const runTestsXTimes = 3;
// these can change depending on language settings
const buttonIdentifier = '.run-button';
const buttonReadyText = 'Run tests';
const buttonRunningText = 'Running tests';
// wait 5-10 seconds between tests, prevents blocking
// set to 0 if you don't wish to wait
const timeToWaitBetweenTests = () => 5000 + (Math.random() * 5000)
// usually should be 0, on slower computers maybe around 5 or 10 milliseconds?
// the highest this number is, the lest accurate the measurement
const timeToWaitWhenObservingButtonTextChange = 0;
// -----------------------------------------------
const measureSingleTest = (id) => {
return new Promise((resolve) => {
const buttonWrapper = document.querySelector(buttonIdentifier);
const btn = document.querySelector(`${buttonIdentifier} [role="button"]`);
let started = 0;
let finished = 0;
// we don't use request animation frame,
// since this should run as fast as possible and not wait for the frame to update
const myInterval = setInterval(() => {
if (!started && buttonWrapper.textContent === buttonRunningText) {
started = Date.now();
}
if (started && buttonWrapper.textContent === buttonReadyText) {
finished = Date.now();
clearInterval(myInterval);
const time = finished - started;
resolve(time);
console.log(`test ${id} took ~ ${time} milliseconds`)
}
}, timeToWaitWhenObservingButtonTextChange);
btn.click();
})
}
// helper functions
const wait = (time) => new Promise(resolve => setTimeout(resolve, time));
const average = numbers => numbers.reduce((acc, n) => acc + n, 0) / numbers.length;
const median = numbers => numbers.slice().sort((a, b) => a - b)[Math.floor(numbers.length / 2)];
const runMeasurements = async (times) => {
console.log('running tests....');
const measurements = [];
for (let i = 0; i < times; i++) {
const timeTook = await measureSingleTest(i);
measurements.push(timeTook);
if(i !== times - 1) {
// this wait line is not required,
// it's here to prevent getting banned for automation
await wait(timeToWaitBetweenTests());
}
}
console.log('------ Tests Report ------');
console.log(`average: ${average(measurements)}`);
console.log(`median: ${median(measurements)}`);
console.log(`low: ${Math.min(...measurements)}`);
console.log(`high: ${Math.max(...measurements)}`);
console.log(`run: ${measurements.length} tests`);
console.log(`measurements:`, measurements);
console.log('------ ------ ------ ------');
}
// run the tests X times
runMeasurements(runTestsXTimes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment