Created
April 21, 2024 19:23
-
-
Save joshtwist/330da7daaddcf91c3faff01f2f7daa65 to your computer and use it in GitHub Desktop.
Fire async requests and summarize results in table (node)
This file contains 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
import fetch from "node-fetch"; | |
import ProgressBar from "progress"; | |
// Function to handle the individual fetch operation | |
async function sendRequest(url, index) { | |
try { | |
// console.log(`Sending request ${index + 1} to ${url}`); | |
const startTime = process.hrtime(); // Start timing here | |
const response = await fetch(url); | |
const duration = process.hrtime(startTime); | |
// console.log( | |
// `Request ${index + 1} completed in ${duration[0]}s ${duration[1] / 1e6}ms` | |
// ); | |
const durationInMs = (duration[0] * 1000 + duration[1] / 1e6).toFixed(2); | |
let preview = ""; | |
let detail = ""; | |
let status = ""; | |
let instance = response.headers.get("zp-instance-id"); | |
if (response.ok) { | |
const text = await response.text(); | |
preview = text.substring(0, 50); // Get the first 50 characters of the response | |
status = "Success"; | |
} else if (response.status === 500) { | |
const json = await response.json(); | |
detail = json.detail || "No detail provided"; // Extracting detail from JSON if available | |
status = "Error 500"; | |
} else { | |
throw new Error(`HTTP error! status: ${response.status}`); | |
} | |
return { | |
Status: status, | |
Duration: `${durationInMs} ms`, | |
Preview: preview, | |
Detail: detail, | |
Instance: instance, | |
}; | |
} catch (error) { | |
return { | |
Status: "Fetch Error", | |
Duration: "Error", | |
Preview: "", | |
Detail: error.message, | |
Instance: instance, | |
}; | |
} | |
} | |
// Function to manage the sequence of requests | |
async function fireRequests(url, n, delay) { | |
const results = new Array(n); | |
const bar = new ProgressBar(":bar :current/:total Requests", { | |
total: n, | |
width: 20, | |
complete: "=", | |
incomplete: " ", | |
}); | |
let completedRequests = 0; | |
for (let i = 0; i < n; i++) { | |
setTimeout(async () => { | |
bar.tick(); | |
results[i] = await sendRequest(url, i); | |
completedRequests++; | |
if (completedRequests === n) { | |
displayResults(results); | |
summarizeResults(results); | |
} | |
}, delay * 1000 * i); | |
} | |
} | |
// Function to display the results | |
function displayResults(results) { | |
console.log("All requests completed."); | |
console.table(results); | |
} | |
function summarizeResults(results) { | |
const statusSummary = results.reduce((acc, result) => { | |
acc[result.Status] = (acc[result.Status] || 0) + 1; | |
return acc; | |
}, {}); | |
const averageDuration = results.reduce( | |
(acc, result) => { | |
if (!isNaN(result.Duration)) { | |
acc.total += parseFloat(result.Duration); | |
acc.count += 1; | |
} | |
return acc; | |
}, | |
{ total: 0, count: 0 } | |
); | |
const averageDurationResult = | |
averageDuration.count > 0 | |
? (averageDuration.total / averageDuration.count).toFixed(2) | |
: 0; | |
console.log("\nSummary:"); | |
console.table(statusSummary); | |
console.log("Average Duration:", averageDurationResult, "ms"); | |
} | |
fireRequests("https://bgl-test-main-3738d64.d2.zuplo.dev/path-0", 1000, 0.1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment