Skip to content

Instantly share code, notes, and snippets.

@rcmilan
Created September 6, 2023 18:31
Show Gist options
  • Save rcmilan/3c149300934dc37cea3651d7ce1b87a2 to your computer and use it in GitHub Desktop.
Save rcmilan/3c149300934dc37cea3651d7ce1b87a2 to your computer and use it in GitHub Desktop.
Load Test JS
const axios = require('axios'); // npm install axios
// Configure your test parameters
const endpoint = ''; // Replace with your API endpoint
const numRequests = 1000; // Number of requests to send
const concurrency = 10; // Number of concurrent requests
// Function to send a single HTTP request
async function sendRequest() {
try {
const response = await axios.get(endpoint);
console.log(`Status code: ${response.status}`);
} catch (error) {
console.error(`Error: ${error.message}`);
}
}
// Function to simulate concurrent requests
async function runLoadTest() {
const requestPromises = [];
for (let i = 0; i < numRequests; i++) {
requestPromises.push(sendRequest());
if (requestPromises.length >= concurrency) {
await Promise.all(requestPromises);
requestPromises.length = 0; // Clear the array
}
}
// Wait for any remaining requests to finish
await Promise.all(requestPromises);
console.log('Load test completed.');
}
// Start the load test
runLoadTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment