router / src /concurrency-test.ts
xinxiang.wang
../..
53cc475
import { fetch } from "bun";
// Configuration
const PROXY_PORT = process.env.PORT || 8001; // Changed to match server port
const PROXY_URL = `http://localhost:${PROXY_PORT}`;
const CONCURRENT_REQUESTS = 20; // Testing with more than the required 16
const TARGET_URL = "https://httpbin.org/delay/1"; // This endpoint has a 1-second delay
console.log(`Starting concurrency test with ${CONCURRENT_REQUESTS} concurrent requests...`);
console.log(`Target: ${TARGET_URL}`);
console.log(`Proxy: ${PROXY_URL}`);
const startTime = Date.now();
// Create an array of promises for concurrent requests
const requests = Array(CONCURRENT_REQUESTS).fill(0).map((_, index) => {
return (async () => {
const requestStart = Date.now();
console.log(`[${index}] Starting request`);
try {
const response = await fetch(`${PROXY_URL}/${TARGET_URL}`);
// We don't need to read the response body for the test
try {
await response.json();
} catch (e) {
// Ignore JSON parsing errors
}
const requestEnd = Date.now();
const duration = requestEnd - requestStart;
console.log(`[${index}] Request completed in ${duration}ms with status ${response.status}`);
return { index, success: true, status: response.status, duration };
} catch (error: any) {
console.error(`[${index}] Request failed:`, error);
return { index, success: false, error: error.message };
}
})();
});
// Execute all requests concurrently and wait for them to complete
Promise.all(requests)
.then(results => {
const endTime = Date.now();
const totalDuration = endTime - startTime;
// Count successful and failed requests
const successful = results.filter(r => r.success).length;
const failed = results.filter(r => !r.success).length;
console.log("\n--- Concurrency Test Results ---");
console.log(`Total time: ${totalDuration}ms`);
console.log(`Successful requests: ${successful}/${CONCURRENT_REQUESTS}`);
console.log(`Failed requests: ${failed}/${CONCURRENT_REQUESTS}`);
// If all requests were successful, the test passed
if (successful === CONCURRENT_REQUESTS) {
console.log("\n✅ SUCCESS: All concurrent requests completed successfully!");
console.log(`The proxy server successfully handled ${CONCURRENT_REQUESTS} concurrent requests.`);
} else {
console.log("\n❌ FAILURE: Some requests failed!");
}
})
.catch(error => {
console.error("Error running concurrency test:", error);
});