Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / resources / src / startup / profiler.js
1 /*!
2 * Augment mw.loader to facilitate module-level profiling.
3 *
4 * @since 1.32
5 */
6 /* global mw */
7 ( function () {
8 'use strict';
9
10 var moduleTimes = Object.create( null );
11
12 /**
13 * Private hooks inserted into mw.loader code if MediaWiki configuration
14 * `$wgResourceLoaderEnableJSProfiler` is `true`.
15 *
16 * To use this data, run `mw.inspect( 'time' )` from the browser console.
17 * See mw#inspect().
18 *
19 * @private
20 * @class
21 * @singleton
22 */
23 mw.loader.profiler = {
24 onExecuteStart: function ( moduleName ) {
25 var time = performance.now();
26 if ( moduleTimes[ moduleName ] ) {
27 throw new Error( 'Unexpected perf record for "' + moduleName + '".' );
28 }
29 moduleTimes[ moduleName ] = {
30 executeStart: time,
31 executeEnd: null,
32 scriptStart: null,
33 scriptEnd: null
34 };
35 },
36 onExecuteEnd: function ( moduleName ) {
37 var time = performance.now();
38 moduleTimes[ moduleName ].executeEnd = time;
39 },
40 onScriptStart: function ( moduleName ) {
41 var time = performance.now();
42 moduleTimes[ moduleName ].scriptStart = time;
43 },
44 onScriptEnd: function ( moduleName ) {
45 var time = performance.now();
46 moduleTimes[ moduleName ].scriptEnd = time;
47 },
48
49 /**
50 * For internal use by inspect.reports#time.
51 *
52 * @private
53 * @param {string} moduleName
54 * @return {Object|null}
55 * @throws {Error} If the perf record is incomplete.
56 */
57 getProfile: function ( moduleName ) {
58 var times, key, execute, script, total;
59 times = moduleTimes[ moduleName ];
60 if ( !times ) {
61 return null;
62 }
63 for ( key in times ) {
64 if ( times[ key ] === null ) {
65 throw new Error( 'Incomplete perf record for "' + moduleName + '".', times );
66 }
67 }
68 execute = times.executeEnd - times.executeStart;
69 script = times.scriptEnd - times.scriptStart;
70 total = execute + script;
71 return {
72 name: moduleName,
73 execute: execute,
74 script: script,
75 total: total
76 };
77 }
78 };
79
80 }() );