src/tests/user_sampling_channel_test.js
/**
 * Randomly generate 1,000 client ids (using the actual id generator) and
 * verify that the UserSamplingChannel, with sample rate configured to 25%, only
 * accepts (roughly) 25% of the ids.
 */
Randomly generate 1,000 client ids (using the actual id generator) and
verify that the UserSamplingChannel, with sample rate configured to 25%, only
accepts (roughly) 25% of the ids.
Randomly generate 1 000 client ids ( using the actual id generator ) and verify that the UserSamplingChannel with sample rate configured to 25% only accepts ( roughly ) 25% of the ids .
function testSend_withRandomIds_sampleRate25() {
  var sampleRate = 25;
  var settings = new analytics.testing.TestSettings();
  settings.setSampleRate(sampleRate);
  var hitsAttempted = 0;
  var hitsSent = 0;
  for (var i = 0; i < 1000; i++) {
    var cid = analytics.internal.Identifier.generateUuid();
    var params = new analytics.ParameterMap();
    params.set(analytics.internal.Parameters.CLIENT_ID, cid);
    var delegate = new analytics.testing.TestChannel();
    var channel =
        new analytics.internal.UserSamplingChannel(settings, delegate);
    channel.send(analytics.HitTypes.APPVIEW, params);
    if (delegate.hitWasSent(params)) hitsSent++;
    hitsAttempted++;
  }
  var sentRate = hitsSent / hitsAttempted * 100;
  assertTrue('Expected ' + sampleRate + '% (+/- 5%) of randomly generated ' +
      'client ids to be sent but actual acceptance rate was ' + sentRate,
      sentRate >= (sampleRate - 5) && sentRate <= (sampleRate + 5));
}
function testSend_withRandomIds_sampleRate25 ( ) { var sampleRate = 25 ; var settings = new analytics . testing . TestSettings ( ) ; settings . setSampleRate ( sampleRate ) ; var hitsAttempted = 0 ; var hitsSent = 0 ; for ( var i = 0 ; i < 1000 ; i ++ ) { var cid = analytics . internal . Identifier . generateUuid ( ) ; var params = new analytics . ParameterMap ( ) ; params . set ( analytics . internal . Parameters . CLIENT_ID , cid ) ; var delegate = new analytics . testing . TestChannel ( ) ; var channel = new analytics . internal . UserSamplingChannel ( settings , delegate ) ; channel . send ( analytics . HitTypes . APPVIEW , params ) ; if ( delegate . hitWasSent ( params ) ) hitsSent ++ ; hitsAttempted ++ ; } var sentRate = hitsSent / hitsAttempted * 100 ; assertTrue ( 'Expected ' + sampleRate + '% (+/- 5%) of randomly generated ' + 'client ids to be sent but actual acceptance rate was ' + sentRate , sentRate >= ( sampleRate - 5 ) && sentRate <= ( sampleRate + 5 ) ) ; }
Randomly generate 1,000 client ids (using the actual id generator) and
 verify that the UserSamplingChannel, with sample rate configured to 25%, only
 accepts (roughly) 25% of the ids.
Randomly generate 1 000 client ids ( using the actual id generator ) and verify that the UserSamplingChannel with sample rate configured to 25% only accepts ( roughly ) 25% of the ids .