mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-09-01 05:07:59 +00:00
fix(backend): batch-insert stress test metrics to avoid per-insert fsync timeout (#911)
This commit is contained in:
@@ -347,18 +347,19 @@ describe('DatabaseService - stress tests', () => {
|
|||||||
it('handles 1000+ metrics and cleanup bounds growth', () => {
|
it('handles 1000+ metrics and cleanup bounds growth', () => {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
// Insert 1200 metrics spread across 2 hours
|
// Insert 1200 metrics spread across 2 hours in a single transaction.
|
||||||
for (let i = 0; i < 1200; i++) {
|
// Individual auto-committed inserts trigger a disk fsync each, making
|
||||||
db.addContainerMetric({
|
// 1200 separate calls impractically slow on spinning disks and Windows.
|
||||||
container_id: `stress-container-${i % 10}`,
|
const metrics = Array.from({ length: 1200 }, (_, i) => ({
|
||||||
stack_name: 'stress-stack',
|
container_id: `stress-container-${i % 10}`,
|
||||||
cpu_percent: Math.random() * 100,
|
stack_name: 'stress-stack',
|
||||||
memory_mb: Math.random() * 1024,
|
cpu_percent: Math.random() * 100,
|
||||||
net_rx_mb: Math.random() * 10,
|
memory_mb: Math.random() * 1024,
|
||||||
net_tx_mb: Math.random() * 10,
|
net_rx_mb: Math.random() * 10,
|
||||||
timestamp: now - (i * 6000), // Every 6 seconds over ~2 hours
|
net_tx_mb: Math.random() * 10,
|
||||||
});
|
timestamp: now - (i * 6000), // Every 6 seconds over ~2 hours
|
||||||
}
|
}));
|
||||||
|
db.bulkAddContainerMetrics(metrics);
|
||||||
|
|
||||||
// Cleanup with 1 hour retention
|
// Cleanup with 1 hour retention
|
||||||
db.cleanupOldMetrics(1);
|
db.cleanupOldMetrics(1);
|
||||||
|
|||||||
@@ -1798,6 +1798,18 @@ export class DatabaseService {
|
|||||||
stmt.run(metric.container_id, metric.stack_name, metric.cpu_percent, metric.memory_mb, metric.net_rx_mb, metric.net_tx_mb, metric.timestamp);
|
stmt.run(metric.container_id, metric.stack_name, metric.cpu_percent, metric.memory_mb, metric.net_rx_mb, metric.net_tx_mb, metric.timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bulkAddContainerMetrics(metrics: Omit<any, 'id'>[]): void {
|
||||||
|
const stmt = this.db.prepare(
|
||||||
|
'INSERT INTO container_metrics (container_id, stack_name, cpu_percent, memory_mb, net_rx_mb, net_tx_mb, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)'
|
||||||
|
);
|
||||||
|
const insertAll = this.db.transaction((items: Omit<any, 'id'>[]) => {
|
||||||
|
for (const m of items) {
|
||||||
|
stmt.run(m.container_id, m.stack_name, m.cpu_percent, m.memory_mb, m.net_rx_mb, m.net_tx_mb, m.timestamp);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
insertAll(metrics);
|
||||||
|
}
|
||||||
|
|
||||||
public getContainerMetrics(hoursLookback = 24): any[] {
|
public getContainerMetrics(hoursLookback = 24): any[] {
|
||||||
const cutoff = Date.now() - (hoursLookback * 60 * 60 * 1000);
|
const cutoff = Date.now() - (hoursLookback * 60 * 60 * 1000);
|
||||||
// Aggregate into 5-minute buckets (300000ms) to keep response size bounded.
|
// Aggregate into 5-minute buckets (300000ms) to keep response size bounded.
|
||||||
|
|||||||
Reference in New Issue
Block a user