Enable module storage for 0.05% of visitors w/storage-capable browsers
authorOri Livneh <ori@wikimedia.org>
Tue, 12 Nov 2013 01:44:44 +0000 (17:44 -0800)
committerOri Livneh <ori@wikimedia.org>
Thu, 14 Nov 2013 20:46:01 +0000 (12:46 -0800)
Module storage was added in If2ad2d80d but hidden behind a feature flag and
disabled by default. Before we enable it by default, we want to make sure that
it really does improve performance. To gauge the performance impact of module
storage, we want to run a controlled experiment that will randomly assign 0.1%
of visitors with eligible browsers to either an experimental or control group.
We will collect page load timing data from both groups, but enable module
storage only for the experimental group. The logging code will live in the
NavigationTiming extension, but the group assignment logic has to happen before
any modules are actually loaded, so it has to be in core. The exact time-frame
of the experiment has not yet been determined but it will be in the
neighborhood of one week, after which I will remove this code from core. Bug
56397 provides a means for tracking my commitment to removing this code.

The experiment was designed by me and Aaron Halfaker.

Bug: 56397
Change-Id: Id2835eca4469310d79d28a243d4b0d76aa59cd2a

resources/mediawiki/mediawiki.js

index 328ba83..8a8215d 100644 (file)
@@ -1749,6 +1749,38 @@ var mw = ( function ( $, undefined ) {
                                        // Cache hit stats
                                        stats: { hits: 0, misses: 0, expired: 0 },
 
+                                       // Experiment data
+                                       experiment: ( function () {
+                                               var start = ( new Date() ).getTime(), id = 0, seed = 0;
+
+                                               try {
+                                                       id = JSON.parse( localStorage.getItem( 'moduleStorageExperiment' ) );
+                                                       if ( typeof id !== 'number' ) {
+                                                               id = Math.floor( Math.random() * Math.random() * 1e16 );
+                                                               localStorage.setItem( 'moduleStorageExperiment', id );
+                                                       }
+                                                       seed = id % 2000;
+                                               } catch ( e ) {}
+
+                                               return {
+                                                       // Unique identifier for this browser. This allows us to group all
+                                                       // datapoints generated by a particular browser, which in turn allows us
+                                                       // to see how the initial load compares to subsequent page loads.
+                                                       id: id,
+
+                                                       // Group assignment may be 0 (not in experiment), 1 (control group), or 2
+                                                       // (experimental group). Browsers that don't implement all the prerequisite APIs
+                                                       // (JSON and Web Storage) are ineligible. Eligible browsers have a 0.1% chance
+                                                       // of being included in the experiment, in which case they are equally likely to
+                                                       // be assigned to either the experimental or control group.
+                                                       group: seed === 1 ? 1 : ( seed === 2 ? 2 : 0 ),
+
+                                                       // Assess module storage performance by measuring the time between this
+                                                       // reference point and the window load event.
+                                                       start: start
+                                               };
+                                       }() ),
+
                                        /**
                                         * Construct a JSON-serializable object representing the content of the store.
                                         * @return {Object} Module store contents.
@@ -1799,20 +1831,19 @@ var mw = ( function ( $, undefined ) {
                                         * code for a full account of why we need a try / catch: <http://git.io/4NEwKg>.
                                         */
                                        init: function () {
-                                               var raw, data, optedIn;
+                                               var raw, data;
 
                                                if ( mw.loader.store.enabled !== null ) {
                                                        // #init already ran.
                                                        return;
                                                }
 
-                                               // Temporarily allow users to opt-in during mw.loader.store test phase by
-                                               // manually setting a cookie (bug 56397).
-                                               optedIn = /ResourceLoaderStorageEnabled=1/.test( document.cookie );
-
-                                               if ( !( mw.config.get( 'wgResourceLoaderStorageEnabled' ) || optedIn ) || mw.config.get( 'debug' ) ) {
-                                                       // Disabled by configuration, or because debug mode is set.
-                                                       mw.loader.store.enabled = false;
+                                               if (
+                                                       // We're in debug mode
+                                                       mw.config.get( 'debug' ) ||
+                                                       // Module storage is neither enabled by default, nor enabled for this user's group.
+                                                       !( mw.config.get( 'wgResourceLoaderStorageEnabled' ) || mw.loader.store.experiment.group === 2 )
+                                               ) {
                                                        return;
                                                }