test: Add baseline memory test (#19102)

This commit is contained in:
shortstacked
2025-09-02 18:30:49 +01:00
committed by GitHub
parent 5be3181f2b
commit a12e782225
4 changed files with 70 additions and 1 deletions

View File

@@ -29,6 +29,45 @@ export async function getAllPerformanceMetrics(page: Page) {
});
}
/**
* Polls the memory metric from Prometheus endpoint and calculates average
* @param baseUrl - The base URL of the n8n instance
* @param durationMs - How long to poll in milliseconds
* @param intervalMs - Interval between polls in milliseconds
* @returns Average memory consumption in bytes
*/
export async function pollMemoryMetric(
baseUrl: string,
durationMs: number = 30000,
intervalMs: number = 1000,
): Promise<number> {
const samples: number[] = [];
const startTime = Date.now();
while (Date.now() - startTime < durationMs) {
try {
const response = await fetch(`${baseUrl}/metrics`);
const metricsText = await response.text();
const memoryMatch = metricsText.match(/n8n_process_resident_memory_bytes\s+(\d+(?:\.\d+)?)/);
if (memoryMatch) {
const memoryBytes = parseFloat(memoryMatch[1]);
samples.push(memoryBytes);
}
} catch (error) {
console.error('Error polling memory metric:', error);
}
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
if (samples.length === 0) {
throw new Error('No memory samples collected');
}
return samples.reduce((sum, sample) => sum + sample, 0) / samples.length;
}
/**
* Attach a performance metric for collection by the metrics reporter
* @param testInfo - The Playwright TestInfo object