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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 229x 229x 228x 55x 55x 1x 1x 1x 1x 21x 21x 21x | /** * Percentile Calculation Utilities * * Functions for calculating performance percentiles (P50, P95, P99, etc.) */ /** * Calculate a specific percentile from an array of numbers * * @param values - Array of numeric values * @param percentile - Percentile to calculate (0-100) * @returns The percentile value, or 0 if array is empty * * @example * ```typescript * const durations = [10, 20, 30, 40, 50, 100, 200, 500, 1000, 2000]; * const p95 = calculatePercentile(durations, 95); // ~1000 * const p99 = calculatePercentile(durations, 99); // ~2000 * ``` */ export function calculatePercentile(values: number[], percentile: number): number { if (values.length === 0) return 0; if (percentile < 0 || percentile > 100) { throw new Error('Percentile must be between 0 and 100'); } const sorted = [...values].sort((a, b) => a - b); const index = Math.ceil((percentile / 100) * sorted.length) - 1; return sorted[Math.max(0, index)]; } /** * Calculate multiple percentiles at once * * @param values - Array of numeric values * @param percentiles - Array of percentiles to calculate * @returns Object with percentile values * * @example * ```typescript * const durations = [10, 20, 30, 40, 50]; * const result = calculatePercentiles(durations, [50, 95, 99]); * // { p50: 30, p95: 50, p99: 50 } * ``` */ export function calculatePercentiles( values: number[], percentiles: number[] ): Record<string, number> { const result: Record<string, number> = {}; for (const p of percentiles) { result[`p${p}`] = calculatePercentile(values, p); } return result; } /** * Calculate common performance percentiles (P50, P90, P95, P99) * * @param values - Array of duration values in milliseconds * @returns Object with p50, p90, p95, p99, min, max, avg */ export function calculatePerformanceStats(values: number[]): { p50: number; p90: number; p95: number; p99: number; min: number; max: number; avg: number; count: number; } { if (values.length === 0) { return { p50: 0, p90: 0, p95: 0, p99: 0, min: 0, max: 0, avg: 0, count: 0 }; } const sorted = [...values].sort((a, b) => a - b); const sum = values.reduce((acc, val) => acc + val, 0); return { p50: calculatePercentile(values, 50), p90: calculatePercentile(values, 90), p95: calculatePercentile(values, 95), p99: calculatePercentile(values, 99), min: sorted[0], max: sorted[sorted.length - 1], avg: sum / values.length, count: values.length, }; } /** * Bucket values for histogram display * * @param values - Array of numeric values * @param bucketCount - Number of buckets to create * @returns Array of bucket objects with range and count */ export function createHistogramBuckets( values: number[], bucketCount: number = 10 ): Array<{ min: number; max: number; count: number; percentage: number }> { if (values.length === 0) return []; const sorted = [...values].sort((a, b) => a - b); const min = sorted[0]; const max = sorted[sorted.length - 1]; const range = max - min || 1; const bucketSize = range / bucketCount; const buckets: Array<{ min: number; max: number; count: number; percentage: number }> = []; for (let i = 0; i < bucketCount; i++) { const bucketMin = min + i * bucketSize; const bucketMax = min + (i + 1) * bucketSize; const count = values.filter((v) => { if (i === bucketCount - 1) { return v >= bucketMin && v <= bucketMax; } return v >= bucketMin && v < bucketMax; }).length; buckets.push({ min: bucketMin, max: bucketMax, count, percentage: (count / values.length) * 100, }); } return buckets; } /** * Time range type for performance queries */ export type TimeRange = '1h' | '6h' | '24h' | '7d' | '30d'; /** * Get start date for a time range */ export function getStartDateForRange(range: TimeRange): Date { const now = new Date(); switch (range) { case '1h': return new Date(now.getTime() - 60 * 60 * 1000); case '6h': return new Date(now.getTime() - 6 * 60 * 60 * 1000); case '24h': return new Date(now.getTime() - 24 * 60 * 60 * 1000); case '7d': return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); case '30d': return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); default: return new Date(now.getTime() - 24 * 60 * 60 * 1000); } } /** * Get appropriate granularity for a time range */ export function getGranularityForRange(range: TimeRange): 'minute' | 'hour' | 'day' { switch (range) { case '1h': return 'minute'; case '6h': return 'minute'; case '24h': return 'hour'; case '7d': return 'hour'; case '30d': return 'day'; default: return 'hour'; } } /** * Format duration for display */ export function formatDuration(ms: number): string { if (ms < 1) return '<1ms'; if (ms < 1000) return `${Math.round(ms)}ms`; return `${(ms / 1000).toFixed(2)}s`; } /** * Format large numbers with K, M suffixes */ export function formatNumber(num: number): string { if (num >= 1000000) return `${(num / 1000000).toFixed(1)}M`; if (num >= 1000) return `${(num / 1000).toFixed(1)}K`; return num.toLocaleString(); } |