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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/* jshint bitwise:false */
( function ( mw, $ ) {
var CONTROL_BUCKET = 'control',
MAX_INT32_UNSIGNED = 4294967295;
/**
* An implementation of Jenkins' one-at-a-time hash.
*
* @see http://en.wikipedia.org/wiki/Jenkins_hash_function
*
* @param {String} string String to hash
* @return {Number} The hash as a 32-bit unsigned integer
* @ignore
*
* @author Ori Livneh <ori@wikimedia.org>
* @see http://jsbin.com/kejewi/4/watch?js,console
*/
function hashString( string ) {
var hash = 0,
i = string.length;
while ( i-- ) {
hash += string.charCodeAt( i );
hash += ( hash << 10 );
hash ^= ( hash >> 6 );
}
hash += ( hash << 3 );
hash ^= ( hash >> 11 );
hash += ( hash << 15 );
return hash >>> 0;
}
/**
* Provides an API for bucketing users in experiments.
*
* @class mw.experiments
* @singleton
*/
mw.experiments = {
/**
* Gets the bucket for the experiment given the token.
*
* The name of the experiment and the token are hashed. The hash is converted
* to a number which is then used to get a bucket.
*
* Consider the following experiment specification:
*
* ```
* {
* name: 'My first experiment',
* enabled: true,
* buckets: {
* control: 0.5
* A: 0.25,
* B: 0.25
* }
* }
* ```
*
* The experiment has three buckets: control, A, and B. The user has a 50%
* chance of being assigned to the control bucket, and a 25% chance of being
* assigned to either the A or B buckets. If the experiment were disabled,
* then the user would always be assigned to the control bucket.
*
* This function is based on the deprecated `mw.user.bucket` function.
*
* @param {Object} experiment
* @param {String} experiment.name The name of the experiment
* @param {Boolean} experiment.enabled Whether or not the experiment is
* enabled. If the experiment is disabled, then the user is always assigned
* to the control bucket
* @param {Object} experiment.buckets A map of bucket name to probability
* that the user will be assigned to that bucket
* @param {String} token A token that uniquely identifies the user for the
* duration of the experiment
* @returns {String} The bucket
*/
getBucket: function ( experiment, token ) {
var buckets = experiment.buckets,
key,
range = 0,
hash,
max,
acc = 0;
if ( !experiment.enabled || $.isEmptyObject( experiment.buckets ) ) {
return CONTROL_BUCKET;
}
for ( key in buckets ) {
range += buckets[ key ];
}
hash = hashString( experiment.name + ':' + token );
max = ( hash / MAX_INT32_UNSIGNED ) * range;
for ( key in buckets ) {
acc += buckets[ key ];
if ( max <= acc ) {
return key;
}
}
}
};
}( mediaWiki, jQuery ) );
|