2 * @author Ori Livneh <ori@wikimedia.org>
6 * Provides an API for bucketing users in experiments.
8 * @namespace mw.experiments
12 const CONTROL_BUCKET = 'control',
13 MAX_INT32_UNSIGNED = 4294967295;
16 * An implementation of Jenkins' one-at-a-time hash.
18 * See <https://en.wikipedia.org/wiki/Jenkins_hash_function>.
21 * @param {string} string String to hash
22 * @return {number} The hash as a 32-bit unsigned integer
24 function hashString( string ) {
25 /* eslint-disable no-bitwise */
30 hash += string.charCodeAt( i );
31 hash += ( hash << 10 );
32 hash ^= ( hash >> 6 );
34 hash += ( hash << 3 );
35 hash ^= ( hash >> 11 );
36 hash += ( hash << 15 );
39 /* eslint-enable no-bitwise */
45 * Gets the bucket for the experiment given the token.
47 * The name of the experiment and the token are hashed. The hash is converted
48 * to a number which is then used to get a bucket.
51 * // The experiment has three buckets: control, A, and B. The user has a 50% chance of
52 * // being assigned to the control bucket, and a 25% chance of being assigned to either
53 * // the A or B bucket. If the experiment were disabled, then the user would always be
54 * // assigned to the control bucket.
56 * name: 'My first experiment',
65 * @param {Object} experiment
66 * @param {string} experiment.name The name of the experiment
67 * @param {boolean} experiment.enabled Whether or not the experiment is
68 * enabled. If the experiment is disabled, then the user is always assigned
69 * to the control bucket
70 * @param {Object} experiment.buckets A map of bucket name to probability
71 * that the user will be assigned to that bucket
72 * @param {string} token A token that uniquely identifies the user for the
73 * duration of the experiment
74 * @return {string|undefined} The bucket
76 getBucket: function ( experiment, token ) {
77 const buckets = experiment.buckets;
81 if ( !experiment.enabled || !Object.keys( experiment.buckets ).length ) {
82 return CONTROL_BUCKET;
85 for ( const key in buckets ) {
86 range += buckets[ key ];
89 const hash = hashString( experiment.name + ':' + token );
90 const max = ( hash / MAX_INT32_UNSIGNED ) * range;
92 for ( const key in buckets ) {
93 acc += buckets[ key ];