aboutsummaryrefslogtreecommitdiffstats
path: root/resources/src/startup/profiler.js
blob: 0f044f8834d9b6ef29c60b4f5fa64b96a0578a80 (plain) (blame)
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
/*!
 * Augment mw.loader to facilitate module-level profiling.
 *
 * @since 1.32
 */
/* global mw */
( function () {
	'use strict';

	var moduleTimes = Object.create( null );

	/**
	 * Private hooks inserted into mw.loader code if MediaWiki configuration
	 * `$wgResourceLoaderEnableJSProfiler` is `true`.
	 *
	 * To use this data, run `mw.inspect( 'time' )` from the browser console.
	 * See mw#inspect().
	 *
	 * @private
	 * @class
	 * @singleton
	 */
	mw.loader.profiler = {
		onExecuteStart: function ( moduleName ) {
			var time = performance.now();
			if ( moduleTimes[ moduleName ] ) {
				throw new Error( 'Unexpected perf record for "' + moduleName + '".' );
			}
			moduleTimes[ moduleName ] = {
				executeStart: time,
				executeEnd: null,
				scriptStart: null,
				scriptEnd: null
			};
		},
		onExecuteEnd: function ( moduleName ) {
			var time = performance.now();
			moduleTimes[ moduleName ].executeEnd = time;
		},
		onScriptStart: function ( moduleName ) {
			var time = performance.now();
			moduleTimes[ moduleName ].scriptStart = time;
		},
		onScriptEnd: function ( moduleName ) {
			var time = performance.now();
			moduleTimes[ moduleName ].scriptEnd = time;
		},

		/**
		 * For internal use by inspect.reports#time.
		 *
		 * @private
		 * @param {string} moduleName
		 * @return {Object|null}
		 * @throws {Error} If the perf record is incomplete.
		 */
		getProfile: function ( moduleName ) {
			var times, key, execute, script, total;
			times = moduleTimes[ moduleName ];
			if ( !times ) {
				return null;
			}
			for ( key in times ) {
				if ( times[ key ] === null ) {
					throw new Error( 'Incomplete perf record for "' + moduleName + '".', times );
				}
			}
			execute = times.executeEnd - times.executeStart;
			script = times.scriptEnd - times.scriptStart;
			total = execute + script;
			return {
				name: moduleName,
				execute: execute,
				script: script,
				total: total
			};
		}
	};

}() );