|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { isEmpty } from 'lodash'; |
| 4 | + |
| 5 | +interface ConcurrentPerformanceRecord { |
| 6 | + [key: string]: [{ group?: string; diff?: number }]; |
| 7 | +} |
| 8 | +let is_analysis = false; |
| 9 | +let performanceRecord: ConcurrentPerformanceRecord = {}; |
| 10 | +let keyStatistics: Record< |
| 11 | + string, |
| 12 | + { |
| 13 | + min?: number; |
| 14 | + max?: number; |
| 15 | + avg?: number; |
| 16 | + median?: number; |
| 17 | + p90?: number; |
| 18 | + } |
| 19 | +> = {}; |
| 20 | +/** |
| 21 | + * This is a performance analysis tool for concurrent tasks |
| 22 | + * You can use it to collect the start and end time of a task, the collected data with the same key will be summarized |
| 23 | + * the summarzied report contains the min, max, avg, median, p90 of the task |
| 24 | + * When the code snippet is executed, the performance analysis tool will automatically collect the start and end time of the task |
| 25 | + * |
| 26 | + * example: |
| 27 | + * const start = Date.now(); |
| 28 | + * await fn_to_measure() |
| 29 | + * const end = Date.now(); |
| 30 | + * PerformanceAnalysis.collect('fn_to_measure', start, end) |
| 31 | + * |
| 32 | + * You can choose when to summarize the performance data |
| 33 | + * for example, you can summarize the performance data before server closed |
| 34 | + * |
| 35 | + * public async close() { |
| 36 | + if (this.servers) { |
| 37 | + ... close server |
| 38 | + } |
| 39 | + PerformanceAnalysis.count(); |
| 40 | + } |
| 41 | + * |
| 42 | + * Note: If you want to view the performance by each API call, you can use k6 or you can specify the group name when collecting the performance data |
| 43 | + * and implement another count & writePerformanceReport funtion to summarize the performance data by group name |
| 44 | + * |
| 45 | + */ |
| 46 | +export class PerformanceAnalysis { |
| 47 | + public static collect( |
| 48 | + key: string, |
| 49 | + start: number, |
| 50 | + end: number, |
| 51 | + group?: string |
| 52 | + ) { |
| 53 | + if (!start || !end) { |
| 54 | + throw new Error( |
| 55 | + `should provide start and end time when doing performance analysis task "${key}"` |
| 56 | + ); |
| 57 | + } |
| 58 | + if (!performanceRecord[key]) { |
| 59 | + performanceRecord[key] = [] as any; |
| 60 | + } |
| 61 | + const diff = end - start; |
| 62 | + performanceRecord[key].push({ group, diff }); |
| 63 | + if (process.env['PRINT_COLLECTION']) { |
| 64 | + console.log( |
| 65 | + `${key}: collected, start: ${start}, end: ${end}, diff: ${diff}` |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static count(): boolean { |
| 71 | + // sort by time diff |
| 72 | + if (isEmpty(performanceRecord)) { |
| 73 | + console.log('performanceRecord is empty'); |
| 74 | + return false; |
| 75 | + } |
| 76 | + Object.values(performanceRecord).map((records) => { |
| 77 | + records.sort((a, b) => { |
| 78 | + return <number>a.diff - <number>b.diff; |
| 79 | + }); |
| 80 | + }); |
| 81 | + // count statistics |
| 82 | + Object.entries(performanceRecord).map(([key, records]) => { |
| 83 | + const count = records.length; |
| 84 | + const min = records[0].diff; |
| 85 | + const max = records[count - 1].diff; |
| 86 | + const avg = |
| 87 | + records.reduce((acc, cur) => { |
| 88 | + return acc + <number>cur.diff; |
| 89 | + }, 0) / count; |
| 90 | + const median = records[Math.floor(count / 2)].diff; |
| 91 | + const p90 = records[Math.floor(count * 0.9)].diff; |
| 92 | + keyStatistics[key] = { min, max, avg, median, p90 }; |
| 93 | + }); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + public static getStatistic(key: string): any { |
| 98 | + return keyStatistics[key]; |
| 99 | + } |
| 100 | + |
| 101 | + public static clean = () => { |
| 102 | + performanceRecord = {}; |
| 103 | + keyStatistics = {}; |
| 104 | + }; |
| 105 | + |
| 106 | + // write to txt file |
| 107 | + public static writePerformanceReport() { |
| 108 | + const filePath = path.join('./performanceRecord.txt'); |
| 109 | + // print current date, time as human readable format |
| 110 | + fs.appendFileSync(filePath, `------${new Date().toLocaleString()}\n`); |
| 111 | + for (const key of Object.keys(keyStatistics)) { |
| 112 | + fs.appendFileSync(filePath, `${key}\n`); |
| 113 | + let staticLine = ''; |
| 114 | + if (keyStatistics[key]) { |
| 115 | + const statics = keyStatistics[key]; |
| 116 | + Object.entries(statics).map(([k, v]) => { |
| 117 | + staticLine += `${k}: ${v}, `; |
| 118 | + }); |
| 119 | + fs.appendFileSync(filePath, `${staticLine}\n`); |
| 120 | + } |
| 121 | + } |
| 122 | + fs.appendFileSync(filePath, `------\n`); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export function getAnalysis() { |
| 127 | + const counted = PerformanceAnalysis.count(); |
| 128 | + if (counted && !is_analysis) { |
| 129 | + PerformanceAnalysis.writePerformanceReport(); |
| 130 | + console.log( |
| 131 | + 'performance analysis finished, check the performanceRecord.txt file for details' |
| 132 | + ); |
| 133 | + is_analysis = true; |
| 134 | + } |
| 135 | +} |
0 commit comments