Example #1
1
    public void should_report_the_rate_of_change() {
      var minute = TimeSpan.FromMinutes(1);
      var config = new MetricConfig("counter1");
      var clock = new StepClock(TimeSpan.FromMinutes(1));
      var context = new MetricContext(clock);
      var counter = new StepCounter(config, context);
      
      clock.TickNow(1);
      counter.Increment(10);

      Measure measure = Testing.Sync<Measure>(counter, counter.GetMeasure,
        counter.context_);
      Assert.That(measure.Value, Is.EqualTo(10d/minute.Ticks));
      
      counter.OnStep();
      clock.TickNow(1);
      counter.Increment(10);

      measure = Testing.Sync<Measure>(counter, counter.GetMeasure,
        counter.context_);
      Assert.That(measure.Value, Is.EqualTo(10d/minute.Ticks),
        "Should report the same value as previously, since the rate was the same.");
      
      counter.OnStep();
      clock.TickNow(1);
      counter.Increment(20);

      measure = Testing.Sync<Measure>(counter, counter.GetMeasure,
        counter.context_);
      Assert.That(measure.Value, Is.EqualTo(10d/minute.Ticks*2),
        "Should report the double of the previously value, since the rate was doubled.");
    }
    public void long_period_of_inactivity_should_not_corrupt_sampling_state() {
      var clock = new StepClock(TimeSpan.FromMilliseconds(1));
      var resevoir = new ExponentiallyDecayingResevoir(10, 0.015, clock);

      // add 1000 values at a rate of 10 values/second.
      for (int i = 0; i < 1000; i++) {
        resevoir.Update(1000 + i);
        clock.TickNow(100);
      }

      AssertAllValuesBetween(resevoir, 1000, 2000);

      // wait for 15 hours and add another value.
      // this should trigger a rescale. Note that the number of samples will be
      // reduced to 2 because of the  very small factor that will make all
      // existing priorities equal to zero after rescale.
      clock.TickNow(15*60*60*1000);
      resevoir.Update(2000);
      Assert.That(resevoir.Snapshot.Size, Is.EqualTo(2));
      AssertAllValuesBetween(resevoir, 1000, 3000);

      // add 1000 values at a rate of 10 values/second.
      for (int i = 0; i < 1000; i++) {
        resevoir.Update(3000 + i);
        clock.TickNow(100);
      }

      Assert.That(resevoir.Snapshot.Size, Is.EqualTo(10));
      AssertAllValuesBetween(resevoir, 3000, 4000);
    }
Example #3
0
        public void should_report_the_rate_of_change()
        {
            var minute  = TimeSpan.FromMinutes(1);
            var config  = new MetricConfig("counter1");
            var clock   = new StepClock(TimeSpan.FromMinutes(1));
            var context = new MetricContext(clock);
            var counter = new StepCounter(config, context);

            clock.TickNow(1);
            counter.Increment(10);

            Measure measure = Testing.Sync <Measure>(counter, counter.GetMeasure,
                                                     counter.context_);

            Assert.That(measure.Value, Is.EqualTo(10d / minute.Ticks));

            counter.OnStep();
            clock.TickNow(1);
            counter.Increment(10);

            measure = Testing.Sync <Measure>(counter, counter.GetMeasure,
                                             counter.context_);
            Assert.That(measure.Value, Is.EqualTo(10d / minute.Ticks),
                        "Should report the same value as previously, since the rate was the same.");

            counter.OnStep();
            clock.TickNow(1);
            counter.Increment(20);

            measure = Testing.Sync <Measure>(counter, counter.GetMeasure,
                                             counter.context_);
            Assert.That(measure.Value, Is.EqualTo(10d / minute.Ticks * 2),
                        "Should report the double of the previously value, since the rate was doubled.");
        }
        public void long_period_of_inactivity_should_not_corrupt_sampling_state()
        {
            var clock    = new StepClock(TimeSpan.FromMilliseconds(1));
            var resevoir = new ExponentiallyDecayingResevoir(10, 0.015, clock);

            // add 1000 values at a rate of 10 values/second.
            for (int i = 0; i < 1000; i++)
            {
                resevoir.Update(1000 + i);
                clock.TickNow(100);
            }

            AssertAllValuesBetween(resevoir, 1000, 2000);

            // wait for 15 hours and add another value.
            // this should trigger a rescale. Note that the number of samples will be
            // reduced to 2 because of the  very small factor that will make all
            // existing priorities equal to zero after rescale.
            clock.TickNow(15 * 60 * 60 * 1000);
            resevoir.Update(2000);
            Assert.That(resevoir.Snapshot.Size, Is.EqualTo(2));
            AssertAllValuesBetween(resevoir, 1000, 3000);

            // add 1000 values at a rate of 10 values/second.
            for (int i = 0; i < 1000; i++)
            {
                resevoir.Update(3000 + i);
                clock.TickNow(100);
            }

            Assert.That(resevoir.Snapshot.Size, Is.EqualTo(10));
            AssertAllValuesBetween(resevoir, 3000, 4000);
        }
Example #5
0
    public void should_compute_the_mean_rate_of_values() {
      var clock = new StepClock(TimeSpan.FromMilliseconds(1));
      var mean = new MeanRate(new MetricConfig("test"), TimeUnit.Seconds,
        new MetricContext(clock));

      // add 1000 elemets at rate of 10 events/second
      for (int i = 0; i < 1000; i++) {
        mean.Mark();
        clock.TickNow(100);
      }

      double value =
        Testing.Sync<Measure>(mean, mean.GetMeasure, mean.context_).Value;
      Assert.That(value, Is.EqualTo(10));
    }
Example #6
0
        public void should_measure_the_total_time_per_step()
        {
            var minute  = TimeSpan.FromMinutes(1);
            var config  = new MetricConfig("counter1");
            var clock   = new StepClock(TimeSpan.FromMinutes(1));
            var context = new MetricContext(clock);
            var timer   =
                new BucketTimer.Builder(config)
                .WithBuckets(new long[] { 60, 120, 180 })
                .WithTimeUnit(TimeUnit.Seconds)
                .WithMeasureUnit(TimeUnit.Minutes)
                .WithContext(context)
                .Build();

            IMetric total =
                timer
                .Metrics
                .First(
                    x => x.Config.Tags.FirstOrDefault(t => t.Value == "total") != null);

            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));

            var measure = Testing.Sync <Measure>(total, total.GetMeasure, context);

            Assert.That(measure.Value, Is.EqualTo(30d / 60));

            OnStep(total);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(30));

            measure = Testing.Sync <Measure>(total, total.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(30d / 60));

            OnStep(total);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(60));

            measure = Testing.Sync <Measure>(total, total.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(1));
        }
Example #7
0
        public void should_compute_the_mean_rate_of_values()
        {
            var clock = new StepClock(TimeSpan.FromMilliseconds(1));
            var mean  = new MeanRate(new MetricConfig("test"), TimeUnit.Seconds,
                                     new MetricContext(clock));

            // add 1000 elemets at rate of 10 events/second
            for (int i = 0; i < 1000; i++)
            {
                mean.Mark();
                clock.TickNow(100);
            }

            double value =
                Testing.Sync <Measure>(mean, mean.GetMeasure, mean.context_).Value;

            Assert.That(value, Is.EqualTo(10));
        }
Example #8
0
    public void should_measure_the_total_time_per_step() {
      var minute = TimeSpan.FromMinutes(1);
      var config = new MetricConfig("counter1");
      var clock = new StepClock(TimeSpan.FromMinutes(1));
      var context = new MetricContext(clock);
      var timer =
        new BucketTimer.Builder(config)
          .WithBuckets(new long[] {60, 120, 180})
          .WithTimeUnit(TimeUnit.Seconds)
          .WithMeasureUnit(TimeUnit.Minutes)
          .WithContext(context)
          .Build();

      IMetric total =
        timer
          .Metrics
          .First(
            x => x.Config.Tags.FirstOrDefault(t => t.Value == "total") != null);

      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));

      var measure = Testing.Sync<Measure>(total, total.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(30d/60));
      
      OnStep(total);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(30));

      measure = Testing.Sync<Measure>(total, total.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(30d / 60));

      OnStep(total);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(60));

      measure = Testing.Sync<Measure>(total, total.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1));
    }
 public void SetUp() {
   mailbox_ = new Mailbox<Action>(x => x());
   step_clock_ = new StepClock(TimeSpan.FromMilliseconds(5*1001));
 }
Example #10
0
    public void should_count_the_number_of_times_a_bucket_was_hit() {
      var minute = TimeSpan.FromMinutes(1);
      var config = new MetricConfig("counter1");
      var clock = new StepClock(TimeSpan.FromMinutes(3));
      var context = new MetricContext(clock);
      var timer =
        new BucketTimer.Builder(config)
          .WithBuckets(new long[] { 60, 120, 180 })
          .WithTimeUnit(TimeUnit.Seconds)
          .WithMeasureUnit(TimeUnit.Minutes)
          .WithContext(context)
          .Build();

      IMetric b60 = GetMetricWithTag(timer, "bucket=060s");
      IMetric b120 = GetMetricWithTag(timer, "bucket=120s");
      IMetric b180 = GetMetricWithTag(timer, "bucket=180s");

      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));

      var measure = Testing.Sync<Measure>(b60, b60.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1));

      measure = Testing.Sync<Measure>(b120, b120.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      measure = Testing.Sync<Measure>(b180, b180.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      OnStep(b60, b120, b180);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(30));
      timer.Update(TimeSpan.FromSeconds(61));
      timer.Update(TimeSpan.FromSeconds(65));

      measure = Testing.Sync<Measure>(b60, b60.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1/3d));

      measure = Testing.Sync<Measure>(b120, b120.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(2/3d));

      measure = Testing.Sync<Measure>(b180, b180.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      OnStep(b60, b120, b180);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(180));

      measure = Testing.Sync<Measure>(b60, b60.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      measure = Testing.Sync<Measure>(b120, b120.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      measure = Testing.Sync<Measure>(b180, b180.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1/3d));
    }
Example #11
0
        public void should_count_the_number_of_times_a_bucket_was_hit()
        {
            var minute  = TimeSpan.FromMinutes(1);
            var config  = new MetricConfig("counter1");
            var clock   = new StepClock(TimeSpan.FromMinutes(3));
            var context = new MetricContext(clock);
            var timer   =
                new BucketTimer.Builder(config)
                .WithBuckets(new long[] { 60, 120, 180 })
                .WithTimeUnit(TimeUnit.Seconds)
                .WithMeasureUnit(TimeUnit.Minutes)
                .WithContext(context)
                .Build();

            IMetric b60  = GetMetricWithTag(timer, "bucket=060s");
            IMetric b120 = GetMetricWithTag(timer, "bucket=120s");
            IMetric b180 = GetMetricWithTag(timer, "bucket=180s");

            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));

            var measure = Testing.Sync <Measure>(b60, b60.GetMeasure, context);

            Assert.That(measure.Value, Is.EqualTo(1));

            measure = Testing.Sync <Measure>(b120, b120.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            measure = Testing.Sync <Measure>(b180, b180.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            OnStep(b60, b120, b180);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(30));
            timer.Update(TimeSpan.FromSeconds(61));
            timer.Update(TimeSpan.FromSeconds(65));

            measure = Testing.Sync <Measure>(b60, b60.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(1 / 3d));

            measure = Testing.Sync <Measure>(b120, b120.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(2 / 3d));

            measure = Testing.Sync <Measure>(b180, b180.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            OnStep(b60, b120, b180);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(180));

            measure = Testing.Sync <Measure>(b60, b60.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            measure = Testing.Sync <Measure>(b120, b120.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            measure = Testing.Sync <Measure>(b180, b180.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(1 / 3d));
        }
Example #12
0
 public void SetUp()
 {
     mailbox_    = new Mailbox <Action>(x => x());
     step_clock_ = new StepClock(TimeSpan.FromMilliseconds(5 * 1001));
 }