mw.inspect: add report for mw.loader.store
authorOri Livneh <ori@wikimedia.org>
Thu, 31 Oct 2013 16:26:40 +0000 (09:26 -0700)
committerOri Livneh <ori@wikimedia.org>
Sun, 3 Nov 2013 17:39:11 +0000 (09:39 -0800)
Adds a 'store' report to mw.inspect, which outputs:
* Whether localStorage module caching is enabled.
* Cache hit / miss counts.
* Number of items purged from the cache.
* Total size of the cache blob in localStorage.

Rather than duplicate the logic that converted a numeric byte count to a
human-readable format, I moved it to new helper function: humanSize.

Change-Id: I5b98322ba843f32e6a99829b4cf3d8fb0bc61514

resources/mediawiki/mediawiki.inspect.js

index 668aa2a..d93254b 100644 (file)
                } );
        }
 
+       function humanSize( bytes ) {
+               if ( !$.isNumeric( bytes ) || bytes === 0 ) { return bytes; }
+               var i = 0, units = [ '', ' kB', ' MB', ' GB', ' TB', ' PB' ];
+               for ( ; bytes >= 1024; bytes /= 1024 ) { i++; }
+               return bytes.toFixed( 1 ) + units[i];
+       }
+
        /**
         * @class mw.inspect
         * @singleton
 
                                // Convert size to human-readable string.
                                $.each( modules, function ( i, module ) {
-                                       module.size = module.size > 1024 ?
-                                               ( module.size / 1024 ).toFixed( 2 ) + ' KB' :
-                                               ( module.size !== null ? module.size + ' B' : null );
+                                       module.size = humanSize( module.size );
                                } );
 
                                return modules;
                                } );
                                sortByProperty( modules, 'allSelectors', true );
                                return modules;
+                       },
+
+                       /**
+                        * Report stats on mw.loader.store: the number of localStorage
+                        * cache hits and misses, the number of items purged from the
+                        * cache, and the total size of the module blob in localStorage.
+                        */
+                       store: function () {
+                               var raw, stats = { enabled: mw.loader.store.enabled };
+                               if ( stats.enabled ) {
+                                       $.extend( stats, mw.loader.store.stats );
+                                       try {
+                                               raw = localStorage.getItem( mw.loader.store.getStoreKey() );
+                                               stats.totalSize = humanSize( $.byteLength( raw ) );
+                                       } catch (e) {}
+                               }
+                               return [stats];
                        }
                }
        };