@Test
public void sampler() throws Exception {

  final long value[] = new long[1];
  Instrumentation.Variable<Long> var = new Instrumentation.Variable<Long>() {
    @Override
    public Long getValue() {
      return value[0];
    }
  };
  InstrumentationService.Sampler sampler = new InstrumentationService.Sampler();
  sampler.init(4, var);
  assertEquals(sampler.getRate(), 0f, 0.0001);
  sampler.sample();
  assertEquals(sampler.getRate(), 0f, 0.0001);
  value[0] = 1;
  sampler.sample();
  assertEquals(sampler.getRate(), (0d + 1) / 2, 0.0001);
  value[0] = 2;
  sampler.sample();
  assertEquals(sampler.getRate(), (0d + 1 + 2) / 3, 0.0001);
  value[0] = 3;
  sampler.sample();
  assertEquals(sampler.getRate(), (0d + 1 + 2 + 3) / 4, 0.0001);
  value[0] = 4;
  sampler.sample();
  assertEquals(sampler.getRate(), (4d + 1 + 2 + 3) / 4, 0.0001);
  JSONObject json = (JSONObject) new JSONParser().parse(sampler.toJSONString());
  assertEquals(json.size(), 2);
  assertEquals(json.get("sampler"), sampler.getRate());
  assertEquals(json.get("size"), 4L);
  StringWriter writer = new StringWriter();
  sampler.writeJSONString(writer);
  writer.close();
  json = (JSONObject) new JSONParser().parse(writer.toString());
  assertEquals(json.size(), 2);
  assertEquals(json.get("sampler"), sampler.getRate());
  assertEquals(json.get("size"), 4L);
}