renovate/lib/workers/repository/stats.ts
Sergio Zharinov 2299485afd
refactor(http): Prepare "util/http" for queues (#7119)
Co-authored-by: Rhys Arkins <rhys@arkins.net>
2020-09-22 06:16:21 +02:00

59 lines
1.8 KiB
TypeScript

import URL from 'url';
import { logger } from '../../logger';
import * as memCache from '../../util/cache/memory';
interface RequestStats {
method: string;
url: string;
duration: number;
queueDuration: number;
}
export function printRequestStats(): void {
const httpRequests = memCache.get<RequestStats[]>('http-requests');
if (!httpRequests) {
return;
}
httpRequests.sort((a, b) => {
if (a.url === b.url) {
return 0;
}
if (a.url < b.url) {
return -1;
}
return 1;
});
const allRequests: string[] = [];
const requestHosts: Record<string, RequestStats[]> = {};
for (const httpRequest of httpRequests) {
const { method, url, duration, queueDuration } = httpRequest;
allRequests.push(
`${method.toUpperCase()} ${url} ${duration} ${queueDuration}`
);
const { hostname } = URL.parse(url);
requestHosts[hostname] = requestHosts[hostname] || [];
requestHosts[hostname].push(httpRequest);
}
logger.trace({ allRequests, requestHosts }, 'full stats');
const hostStats: string[] = [];
let totalRequests = 0;
for (const [hostname, requests] of Object.entries(requestHosts)) {
const hostRequests = requests.length;
totalRequests += hostRequests;
const requestSum = requests
.map(({ duration }) => duration)
.reduce((a, b) => a + b, 0);
const requestAvg = Math.round(requestSum / hostRequests);
const queueSum = requests
.map(({ queueDuration }) => queueDuration)
.reduce((a, b) => a + b, 0);
const queueAvg = Math.round(queueSum / hostRequests);
const requestCount =
`${hostRequests} ` + (hostRequests > 1 ? 'requests' : 'request');
hostStats.push(
`${hostname}, ${requestCount}, ${requestAvg}ms request average, ${queueAvg}ms queue average`
);
}
logger.debug({ hostStats, totalRequests }, 'http statistics');
}