|
@@ -1,97 +0,0 @@
|
1
|
|
-
|
2
|
|
-import { getLogger } from '@jitsi/logger';
|
3
|
|
-
|
4
|
|
-import * as StatisticsEvents from '../../service/statistics/Events';
|
5
|
|
-import { RunningAverage } from '../util/MathUtil';
|
6
|
|
-
|
7
|
|
-const logger = getLogger('modules/statistics/PerformanceObserverStats');
|
8
|
|
-const MILLI_SECONDS = 1000;
|
9
|
|
-const SECONDS = 60;
|
10
|
|
-
|
11
|
|
-/**
|
12
|
|
- * This class creates an observer that monitors browser's performance measurement events
|
13
|
|
- * as they are recorded in the browser's performance timeline and computes an average and
|
14
|
|
- * a maximum value for the long task events. Tasks are classified as long tasks if they take
|
15
|
|
- * longer than 50ms to execute on the main thread.
|
16
|
|
- */
|
17
|
|
-export class PerformanceObserverStats {
|
18
|
|
- /**
|
19
|
|
- * Creates a new instance of Performance observer statistics.
|
20
|
|
- *
|
21
|
|
- * @param {*} emitter Event emitter for emitting stats periodically
|
22
|
|
- * @param {*} statsInterval interval for calculating the stats
|
23
|
|
- */
|
24
|
|
- constructor(emitter, statsInterval) {
|
25
|
|
- this.eventEmitter = emitter;
|
26
|
|
- this.longTasks = 0;
|
27
|
|
- this.maxDuration = 0;
|
28
|
|
- this.performanceStatsInterval = statsInterval;
|
29
|
|
- this.stats = new RunningAverage();
|
30
|
|
- }
|
31
|
|
-
|
32
|
|
- /**
|
33
|
|
- * Obtains the average rate of long tasks observed per min and the
|
34
|
|
- * duration of the longest task recorded by the observer.
|
35
|
|
- * @returns {Object}
|
36
|
|
- */
|
37
|
|
- getLongTasksStats() {
|
38
|
|
- return {
|
39
|
|
- avgRatePerMinute: (this.stats.getAverage() * SECONDS).toFixed(2), // calc rate per min
|
40
|
|
- maxDurationMs: this.maxDuration
|
41
|
|
- };
|
42
|
|
- }
|
43
|
|
-
|
44
|
|
- /**
|
45
|
|
- * Starts the performance observer by registering the callback function
|
46
|
|
- * that calculates the performance statistics periodically.
|
47
|
|
- * @returns {void}
|
48
|
|
- */
|
49
|
|
- startObserver() {
|
50
|
|
- // Create a handler for when the long task event is fired.
|
51
|
|
- this.longTaskEventHandler = list => {
|
52
|
|
- const entries = list.getEntries();
|
53
|
|
-
|
54
|
|
- for (const task of entries) {
|
55
|
|
- this.longTasks++;
|
56
|
|
- this.maxDuration = Math.max(this.maxDuration, task.duration).toFixed(3);
|
57
|
|
- }
|
58
|
|
- };
|
59
|
|
-
|
60
|
|
- // Create an observer for monitoring long tasks.
|
61
|
|
- logger.info('Creating a Performance Observer for monitoring Long Tasks');
|
62
|
|
- this.observer = new PerformanceObserver(this.longTaskEventHandler);
|
63
|
|
- this.observer.observe({ type: 'longtask',
|
64
|
|
- buffered: true });
|
65
|
|
- const startTime = Date.now();
|
66
|
|
-
|
67
|
|
- // Calculate the average # of events/sec and emit a stats event.
|
68
|
|
- this.longTasksIntervalId = setInterval(() => {
|
69
|
|
- const now = Date.now();
|
70
|
|
- const interval = this._lastTimeStamp
|
71
|
|
- ? (now - this._lastTimeStamp) / MILLI_SECONDS
|
72
|
|
- : (now - startTime) / MILLI_SECONDS;
|
73
|
|
- const rate = this.longTasks / interval;
|
74
|
|
-
|
75
|
|
- this.stats.addNext(rate);
|
76
|
|
- this.eventEmitter.emit(
|
77
|
|
- StatisticsEvents.LONG_TASKS_STATS, this.getLongTasksStats());
|
78
|
|
-
|
79
|
|
- // Reset the counter and start counting events again.
|
80
|
|
- this.longTasks = 0;
|
81
|
|
- this._lastTimeStamp = Date.now();
|
82
|
|
- }, this.performanceStatsInterval);
|
83
|
|
- }
|
84
|
|
-
|
85
|
|
- /**
|
86
|
|
- * Stops the performance observer.
|
87
|
|
- * @returns {void}
|
88
|
|
- */
|
89
|
|
- stopObserver() {
|
90
|
|
- this.observer && this.observer.disconnect();
|
91
|
|
- this.longTaskEventHandler = null;
|
92
|
|
- if (this.longTasksIntervalId) {
|
93
|
|
- clearInterval(this.longTasksIntervalId);
|
94
|
|
- this.longTasksIntervalId = null;
|
95
|
|
- }
|
96
|
|
- }
|
97
|
|
-}
|