Skip to content

Instantly share code, notes, and snippets.

@zfael
Last active October 7, 2024 19:24
Show Gist options
  • Save zfael/97299e73ea234235b3d73a006a2bd651 to your computer and use it in GitHub Desktop.
Save zfael/97299e73ea234235b3d73a006a2bd651 to your computer and use it in GitHub Desktop.
Node script for load testing!
/**
* Usage
* - npm install loadtest
* - npx ts-node load-test.ts
*/
import loadtest from 'loadtest';
const method = 'GET';
const statusCallback = (error, result, latency) => {
console.log(
`Progress: [Requests ${latency.totalRequests}] [totalErrors ${latency.totalErrors}] [time (in sec) ${latency.totalTimeSeconds}] [meanLatencyMS ${latency.meanLatencyMs}] [minLatencyMs ${latency.minLatencyMs}] [maxLatencyMs ${latency.maxLatencyMs}]`,
);
};
const requestGenerator = (_, options, client, callback) => {
const request = client(options, callback);
options.headers['Content-Type'] = 'application/json';
options.headers.Authorization = 'some-token';
request.end();
return request;
};
const options = {
url: `https://your-url-goes-here.com`,
method,
requestsPerSecond: 6,
concurrency: 6,
maxSeconds: 20,
requestGenerator,
statusCallback,
};
loadtest.loadTest(options, (error, results) => {
if (error) {
return console.error('Got an error: %s', error);
}
console.log(results);
console.log('Tests run successfully');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment