• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts {
2
3export enum TimePhase {
4    START = 'start',
5    GET_PROGRAM = 'getProgram(not ArkTSLinter)',
6    UPDATE_ERROR_FILE = 'updateErrorFile',
7    INIT = 'init',
8    STRICT_PROGRAM_GET_SEMANTIC_DIAGNOSTICS = 'strictProgramGetSemanticDiagnostics',
9    NON_STRICT_PROGRAM_GET_SEMANTIC_DIAGNOSTICS = 'nonStrictProgramGetSemanticDiagnostics',
10    NON_STRICT_PROGRAM_GET_SYNTACTIC_DIAGNOSTICS = 'nonStrictProgramGetSyntacticDiagnostics',
11    GET_TSC_DIAGNOSTICS = 'getTscDiagnostics',
12    EMIT_BUILD_INFO = 'emitBuildInfo',
13    LINT = 'lint'
14}
15
16export class ArkTSLinterTimePrinter {
17    private static instance?: ArkTSLinterTimePrinter;
18    private arkTSTimePrintSwitch: boolean;
19    private timeMap = new Map<string, number>();
20    private constructor(ArkTSTimePrintSwitch: boolean = false) {
21        this.arkTSTimePrintSwitch = ArkTSTimePrintSwitch;
22    }
23
24    public static getInstance(): ArkTSLinterTimePrinter {
25        if (!ArkTSLinterTimePrinter.instance) {
26            ArkTSLinterTimePrinter.instance = new ArkTSLinterTimePrinter();
27        }
28        return ArkTSLinterTimePrinter.instance;
29    }
30
31    public static destroyInstance(): void {
32        ArkTSLinterTimePrinter.instance = undefined;
33    }
34
35    public setArkTSTimePrintSwitch(arkTSTimePrintSwitch: boolean): void {
36        this.arkTSTimePrintSwitch = arkTSTimePrintSwitch;
37    }
38
39    public appendTime(key: string): void {
40        if (this.arkTSTimePrintSwitch) {
41            this.timeMap.set(key, Date.now());
42        }
43    }
44
45    private formatMapAsTable(map: Map<number>): string {
46        if (!map.has(TimePhase.START)) {
47            return 'ArkTSLinterTimePrinter: START phase is not exist!';
48        }
49        let maxKeyLength = 0;
50        let lastVal = 0;
51        let sum = 0;
52        let printMap = new Map(map);
53        printMap.forEach((value, key) => {
54            maxKeyLength = Math.max(maxKeyLength, key.toString().length);
55            if (key !== TimePhase.START) {
56                let relativeVal = value - lastVal;
57                if (key !== TimePhase.GET_PROGRAM) {
58                    sum += relativeVal;
59                }
60                printMap.set(key, relativeVal / 1000.0);
61            }
62            lastVal = value;
63        });
64        printMap.set('total', sum / 1000.0);
65
66        let table = '';
67        printMap.forEach((value, key) => {
68            if (key !== TimePhase.START) {
69                const paddedKey = key.toString().padEnd(maxKeyLength, ' ');
70                table += `${paddedKey} | ${value.toString()}\n`;
71            }
72        });
73        const header = `${'Phase'.padEnd(maxKeyLength, ' ')} | Time(seconds)\n`;
74        const FIXLENGTH = ('Phase' + 'Time').length;
75        const separator = '-'.repeat(maxKeyLength + FIXLENGTH) + '\n';
76        return `${header}${separator}${table}`;
77    }
78
79    public printTimes(): void {
80        if (this.arkTSTimePrintSwitch) {
81            const formattedMap = this.formatMapAsTable(this.timeMap);
82            console.log(formattedMap);
83        }
84    }
85}
86
87}