Exemplo n.º 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);
    }
Exemplo n.º 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);
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 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));
    }
Exemplo n.º 7
0
        public void should_decrease_the_average_at_each_minute()
        {
            var m1_ewma = ExponentialWeightedMovingAverage
                          .ForOneMinute(new MetricConfig("test1"), TimeUnit.Seconds,
                                        new MetricContext(mailbox_, step_clock_));

            double offset = 0.000001;

            m1_ewma.Mark(3, step_clock_.Tick);

            step_clock_.TickNow(1);
            Assert.That(m1_ewma.Compute(step_clock_.Tick).Value,
                        Is.InRange(0.6 - offset, 0.6 + offset));

            var averages = new[] {
                0.22072766,
                0.08120117,
                0.02987224,
                0.01098938,
                0.00404277,
                0.00148725,
                0.00054713,
                0.00020128,
                0.00007405,
                0.00002724,
                0.00001002,
                0.00000369,
                0.00000136,
                0.00000050,
                0.00000018
            };

            foreach (var average in averages)
            {
                step_clock_.TickNow(12); // 12* 5 seconds = 1 minute
                Assert.That(m1_ewma.Compute(step_clock_.Tick).Value,
                            Is.InRange(average - offset, average + offset));
            }

            step_clock_.TickNow(10);
            Assert.That(m1_ewma.Compute(step_clock_.Tick).Value,
                        Is.InRange(0.00000018 - offset, 0.00000018 + offset));
        }
Exemplo n.º 8
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));
    }
Exemplo n.º 9
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));
        }
Exemplo n.º 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));
    }
Exemplo n.º 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));
        }