Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 1x 25x 24x 24x 25x 25x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1117x 1117x 1117x 1117x 1117x 5x 5x 1117x 1x 1x 1x 1x 1011x 1011x 1011x 1011x 1011x 1011x 1011x 1011x 1011x 2x 2x 1009x 1009x 1009x 1009x 1009x 1009x 1011x 1011x 1011x 1011x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1002x 1002x 2x 2x 2x 1x 1x 1x 1x 15x 15x 15x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x | /**
* Performance Monitoring Utilities
*
* Provides functions to measure and log API performance.
* Helps identify slow queries and endpoints.
*/
import { logger } from "@/lib/logging";
// Threshold for slow API warning (in milliseconds)
const SLOW_API_THRESHOLD = 200;
/**
* Measure API performance and log slow queries
* @param name - Name of the API endpoint
* @param startTime - Performance start time (from performance.now() or Date.now())
* @returns Duration in milliseconds
*/
export function measureApiPerformance(
name: string,
startTime: number
): number {
const duration = Date.now() - startTime;
// Log slow queries
if (duration > SLOW_API_THRESHOLD) {
logger.warn(`Slow API: ${name} took ${duration}ms`, { category: "PERFORMANCE" });
} else {
logger.debug(`${name} completed in ${duration}ms`, { category: "PERFORMANCE" });
}
return duration;
}
/**
* Performance metrics interface
*/
export interface PerformanceMetrics {
endpoint: string;
duration: number;
timestamp: number;
success: boolean;
}
// In-memory metrics storage (for development/single instance)
const metricsHistory: PerformanceMetrics[] = [];
const MAX_METRICS_HISTORY = 1000;
/**
* Record performance metrics
*/
export function recordMetrics(metrics: PerformanceMetrics): void {
metricsHistory.push(metrics);
// Keep history bounded
if (metricsHistory.length > MAX_METRICS_HISTORY) {
metricsHistory.shift();
}
}
/**
* Get performance statistics for an endpoint
*/
export function getEndpointStats(endpoint: string): {
avgDuration: number;
p95Duration: number;
totalRequests: number;
successRate: number;
} | null {
const endpointMetrics = metricsHistory.filter(m => m.endpoint === endpoint);
if (endpointMetrics.length === 0) {
return null;
}
const durations = endpointMetrics.map(m => m.duration).sort((a, b) => a - b);
const successCount = endpointMetrics.filter(m => m.success).length;
return {
avgDuration: durations.reduce((a, b) => a + b, 0) / durations.length,
p95Duration: durations[Math.floor(durations.length * 0.95)] || durations[durations.length - 1],
totalRequests: endpointMetrics.length,
successRate: (successCount / endpointMetrics.length) * 100};
}
/**
* Get all endpoint statistics
*/
export function getAllEndpointStats(): Record<string, ReturnType<typeof getEndpointStats>> {
const endpoints = [...new Set(metricsHistory.map(m => m.endpoint))];
const stats: Record<string, ReturnType<typeof getEndpointStats>> = {};
for (const endpoint of endpoints) {
stats[endpoint] = getEndpointStats(endpoint);
}
return stats;
}
/**
* Clear metrics history
*/
export function clearMetrics(): void {
metricsHistory.length = 0;
}
/**
* Higher-order function to wrap async handlers with performance tracking
*/
export function withPerformanceTracking<T>(
name: string,
fn: () => Promise<T>
): Promise<T> {
const startTime = Date.now();
return fn().then(
(result) => {
const duration = measureApiPerformance(name, startTime);
recordMetrics({
endpoint: name,
duration,
timestamp: Date.now(),
success: true});
return result;
},
(error) => {
const duration = measureApiPerformance(name, startTime);
recordMetrics({
endpoint: name,
duration,
timestamp: Date.now(),
success: false});
throw error;
}
);
}
|