fix: semantic host stats (#11482)

This commit is contained in:
Rhys Arkins 2021-08-30 17:09:08 +02:00 committed by GitHub
parent 60b3d6d933
commit 59cc317156
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,25 +30,26 @@ export function printRequestStats(): void {
requestHosts[hostname].push(httpRequest); requestHosts[hostname].push(httpRequest);
} }
logger.trace({ allRequests, requestHosts }, 'full stats'); logger.trace({ allRequests, requestHosts }, 'full stats');
const hostStats: string[] = []; type HostStats = {
requestCount: number;
requestAvgMs: number;
queueAvgMs: number;
};
const hostStats: Record<string, HostStats> = {};
let totalRequests = 0; let totalRequests = 0;
for (const [hostname, requests] of Object.entries(requestHosts)) { for (const [hostname, requests] of Object.entries(requestHosts)) {
const hostRequests = requests.length; const requestCount = requests.length;
totalRequests += hostRequests; totalRequests += requestCount;
const requestSum = requests const requestSum = requests
.map(({ duration }) => duration) .map(({ duration }) => duration)
.reduce((a, b) => a + b, 0); .reduce((a, b) => a + b, 0);
const requestAvg = Math.round(requestSum / hostRequests); const requestAvgMs = Math.round(requestSum / requestCount);
const queueSum = requests const queueSum = requests
.map(({ queueDuration }) => queueDuration) .map(({ queueDuration }) => queueDuration)
.reduce((a, b) => a + b, 0); .reduce((a, b) => a + b, 0);
const queueAvg = Math.round(queueSum / hostRequests); const queueAvgMs = Math.round(queueSum / requestCount);
const requestCount = hostStats[hostname] = { requestCount, requestAvgMs, queueAvgMs };
`${hostRequests} ` + (hostRequests > 1 ? 'requests' : 'request');
hostStats.push(
`${hostname}, ${requestCount}, ${requestAvg}ms request average, ${queueAvg}ms queue average`
);
} }
logger.debug({ hostStats, totalRequests }, 'http statistics'); logger.debug({ hostStats, totalRequests }, 'http statistics');
} }