wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
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
/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
 
import {
    IPerformanceMeasurement,
    SubMeasurement,
} from "@azure/msal-common/browser";
 
export class BrowserPerformanceMeasurement implements IPerformanceMeasurement {
    private readonly measureName: string;
    private readonly correlationId: string;
    private readonly startMark: string;
    private readonly endMark: string;
 
    constructor(name: string, correlationId: string) {
        this.correlationId = correlationId;
        this.measureName = BrowserPerformanceMeasurement.makeMeasureName(
            name,
            correlationId
        );
        this.startMark = BrowserPerformanceMeasurement.makeStartMark(
            name,
            correlationId
        );
        this.endMark = BrowserPerformanceMeasurement.makeEndMark(
            name,
            correlationId
        );
    }
 
    private static makeMeasureName(name: string, correlationId: string) {
        return `msal.measure.${name}.${correlationId}`;
    }
 
    private static makeStartMark(name: string, correlationId: string) {
        return `msal.start.${name}.${correlationId}`;
    }
 
    private static makeEndMark(name: string, correlationId: string) {
        return `msal.end.${name}.${correlationId}`;
    }
 
    static supportsBrowserPerformance(): boolean {
        return (
            typeof window !== "undefined" &&
            typeof window.performance !== "undefined" &&
            typeof window.performance.mark === "function" &&
            typeof window.performance.measure === "function" &&
            typeof window.performance.clearMarks === "function" &&
            typeof window.performance.clearMeasures === "function" &&
            typeof window.performance.getEntriesByName === "function"
        );
    }
 
    /**
     * Flush browser marks and measurements.
     * @param {string} correlationId
     * @param {SubMeasurement} measurements
     */
    public static flushMeasurements(
        correlationId: string,
        measurements: SubMeasurement[]
    ): void {
        if (BrowserPerformanceMeasurement.supportsBrowserPerformance()) {
            try {
                measurements.forEach((measurement) => {
                    const measureName =
                        BrowserPerformanceMeasurement.makeMeasureName(
                            measurement.name,
                            correlationId
                        );
                    const entriesForMeasurement =
                        window.performance.getEntriesByName(
                            measureName,
                            "measure"
                        );
                    if (entriesForMeasurement.length > 0) {
                        window.performance.clearMeasures(measureName);
                        window.performance.clearMarks(
                            BrowserPerformanceMeasurement.makeStartMark(
                                measureName,
                                correlationId
                            )
                        );
                        window.performance.clearMarks(
                            BrowserPerformanceMeasurement.makeEndMark(
                                measureName,
                                correlationId
                            )
                        );
                    }
                });
            } catch (e) {
                // Silently catch and return null
            }
        }
    }
 
    startMeasurement(): void {
        if (BrowserPerformanceMeasurement.supportsBrowserPerformance()) {
            try {
                window.performance.mark(this.startMark);
            } catch (e) {
                // Silently catch
            }
        }
    }
 
    endMeasurement(): void {
        if (BrowserPerformanceMeasurement.supportsBrowserPerformance()) {
            try {
                window.performance.mark(this.endMark);
                window.performance.measure(
                    this.measureName,
                    this.startMark,
                    this.endMark
                );
            } catch (e) {
                // Silently catch
            }
        }
    }
 
    flushMeasurement(): number | null {
        if (BrowserPerformanceMeasurement.supportsBrowserPerformance()) {
            try {
                const entriesForMeasurement =
                    window.performance.getEntriesByName(
                        this.measureName,
                        "measure"
                    );
                if (entriesForMeasurement.length > 0) {
                    const durationMs = entriesForMeasurement[0].duration;
                    window.performance.clearMeasures(this.measureName);
                    window.performance.clearMarks(this.startMark);
                    window.performance.clearMarks(this.endMark);
                    return durationMs;
                }
            } catch (e) {
                // Silently catch and return null
            }
        }
        return null;
    }
}