2 * Tools for inspecting page composition and performance.
11 hasOwn
= Object
.prototype.hasOwnProperty
;
13 function sortByProperty( array
, prop
, descending
) {
14 var order
= descending
? -1 : 1;
15 return array
.sort( function ( a
, b
) {
16 return a
[ prop
] > b
[ prop
] ? order
: a
[ prop
] < b
[ prop
] ? -order
: 0;
20 function humanSize( bytes
) {
21 if ( !$.isNumeric( bytes
) || bytes
=== 0 ) { return bytes
; }
23 units
= [ '', ' KiB', ' MiB', ' GiB', ' TiB', ' PiB' ];
25 for ( ; bytes
>= 1024; bytes
/= 1024 ) { i
++; }
26 // Maintain one decimal for kB and above, but don't
27 // add ".0" for bytes.
28 return bytes
.toFixed( i
> 0 ? 1 : 0 ) + units
[ i
];
38 * Return a map of all dependency relationships between loaded modules.
40 * @return {Object} Maps module names to objects. Each sub-object has
41 * two properties, 'requires' and 'requiredBy'.
43 getDependencyGraph: function () {
44 var modules
= inspect
.getLoadedModules(),
47 $.each( modules
, function ( moduleIndex
, moduleName
) {
48 var dependencies
= mw
.loader
.moduleRegistry
[ moduleName
].dependencies
|| [];
50 if ( !hasOwn
.call( graph
, moduleName
) ) {
51 graph
[ moduleName
] = { requiredBy
: [] };
53 graph
[ moduleName
].requires
= dependencies
;
55 $.each( dependencies
, function ( depIndex
, depName
) {
56 if ( !hasOwn
.call( graph
, depName
) ) {
57 graph
[ depName
] = { requiredBy
: [] };
59 graph
[ depName
].requiredBy
.push( moduleName
);
66 * Calculate the byte size of a ResourceLoader module.
68 * @param {string} moduleName The name of the module
69 * @return {number|null} Module size in bytes or null
71 getModuleSize: function ( moduleName
) {
72 var module
= mw
.loader
.moduleRegistry
[ moduleName
],
75 if ( mw
.loader
.getState( moduleName
) !== 'ready' ) {
79 if ( !module
.style
&& !module
.script
) {
84 if ( module
.style
&& $.isArray( module
.style
.css
) ) {
85 $.each( module
.style
.css
, function ( i
, stylesheet
) {
86 payload
+= $.byteLength( stylesheet
);
91 if ( $.isFunction( module
.script
) ) {
92 payload
+= $.byteLength( module
.script
.toString() );
99 * Given CSS source, count both the total number of selectors it
100 * contains and the number which match some element in the current
103 * @param {string} css CSS source
104 * @return {Object} Selector counts
105 * @return {number} return.selectors Total number of selectors
106 * @return {number} return.matched Number of matched selectors
108 auditSelectors: function ( css
) {
109 var selectors
= { total
: 0, matched
: 0 },
110 style
= document
.createElement( 'style' );
112 style
.textContent
= css
;
113 document
.body
.appendChild( style
);
114 $.each( style
.sheet
.cssRules
, function ( index
, rule
) {
116 // document.querySelector() on prefixed pseudo-elements can throw exceptions
117 // in Firefox and Safari. Ignore these exceptions.
118 // https://bugs.webkit.org/show_bug.cgi?id=149160
119 // https://bugzilla.mozilla.org/show_bug.cgi?id=1204880
121 if ( document
.querySelector( rule
.selectorText
) !== null ) {
126 document
.body
.removeChild( style
);
131 * Get a list of all loaded ResourceLoader modules.
133 * @return {Array} List of module names
135 getLoadedModules: function () {
136 return $.grep( mw
.loader
.getModuleNames(), function ( module
) {
137 return mw
.loader
.getState( module
) === 'ready';
142 * Print tabular data to the console, using console.table, console.log,
143 * or mw.log (in declining order of preference).
145 * @param {Array} data Tabular data represented as an array of objects
146 * with common properties.
148 dumpTable: function ( data
) {
150 // Bartosz made me put this here.
151 if ( window
.opera
) { throw window
.opera
; }
152 // Use Function.prototype#call to force an exception on Firefox,
153 // which doesn't define console#table but doesn't complain if you
155 console
.table
.call( console
, data
);
159 console
.log( JSON
.stringify( data
, null, 2 ) );
166 * Generate and print one more reports. When invoked with no arguments,
169 * @param {...string} [reports] Report names to run, or unset to print
170 * all available reports.
172 runReports: function () {
173 var reports
= arguments
.length
> 0 ?
174 Array
.prototype.slice
.call( arguments
) :
175 $.map( inspect
.reports
, function ( v
, k
) { return k
; } );
177 $.each( reports
, function ( index
, name
) {
178 inspect
.dumpTable( inspect
.reports
[ name
]() );
183 * @class mw.inspect.reports
188 * Generate a breakdown of all loaded modules and their size in
189 * kilobytes. Modules are ordered from largest to smallest.
192 // Map each module to a descriptor object.
193 var modules
= $.map( inspect
.getLoadedModules(), function ( module
) {
196 size
: inspect
.getModuleSize( module
)
200 // Sort module descriptors by size, largest first.
201 sortByProperty( modules
, 'size', true );
203 // Convert size to human-readable string.
204 $.each( modules
, function ( i
, module
) {
205 module
.size
= humanSize( module
.size
);
212 * For each module with styles, count the number of selectors, and
213 * count how many match against some element currently in the DOM.
218 $.each( inspect
.getLoadedModules(), function ( index
, name
) {
219 var css
, stats
, module
= mw
.loader
.moduleRegistry
[ name
];
222 css
= module
.style
.css
.join();
223 } catch ( e
) { return; } // skip
225 stats
= inspect
.auditSelectors( css
);
228 allSelectors
: stats
.total
,
229 matchedSelectors
: stats
.matched
,
230 percentMatched
: stats
.total
!== 0 ?
231 ( stats
.matched
/ stats
.total
* 100 ).toFixed( 2 ) + '%' : null
234 sortByProperty( modules
, 'allSelectors', true );
239 * Report stats on mw.loader.store: the number of localStorage
240 * cache hits and misses, the number of items purged from the
241 * cache, and the total size of the module blob in localStorage.
244 var raw
, stats
= { enabled
: mw
.loader
.store
.enabled
};
245 if ( stats
.enabled
) {
246 $.extend( stats
, mw
.loader
.store
.stats
);
248 raw
= localStorage
.getItem( mw
.loader
.store
.getStoreKey() );
249 stats
.totalSize
= humanSize( $.byteLength( raw
) );
257 * Perform a string search across the JavaScript and CSS source code
258 * of all loaded modules and return an array of the names of the
259 * modules that matched.
261 * @param {string|RegExp} pattern String or regexp to match.
262 * @return {Array} Array of the names of modules that matched.
264 grep: function ( pattern
) {
265 if ( typeof pattern
.test
!== 'function' ) {
266 pattern
= new RegExp( mw
.RegExp
.escape( pattern
), 'g' );
269 return $.grep( inspect
.getLoadedModules(), function ( moduleName
) {
270 var module
= mw
.loader
.moduleRegistry
[ moduleName
];
272 // Grep module's JavaScript
273 if ( $.isFunction( module
.script
) && pattern
.test( module
.script
.toString() ) ) {
279 $.isPlainObject( module
.style
) && $.isArray( module
.style
.css
)
280 && pattern
.test( module
.style
.css
.join( '' ) )
282 // Module's CSS source matches
291 if ( mw
.config
.get( 'debug' ) ) {
292 mw
.log( 'mw.inspect: reports are not available in debug mode.' );
295 mw
.inspect
= inspect
;
297 }( mediaWiki
, jQuery
) );