Beispiel #1
0
        public void EDR_SpotFall()
        {
            TestClock     clock     = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);
            ExponentiallyDecayingReservoir reservoir = new ExponentiallyDecayingReservoir(1000, 0.015, clock, scheduler);

            int valuesRatePerMinute  = 10;
            int valuesIntervalMillis = (int)(TimeUnit.Minutes.ToMilliseconds(1) / valuesRatePerMinute);

            // mode 1: steady regime for 120 minutes
            for (int i = 0; i < 120 * valuesRatePerMinute; i++)
            {
                reservoir.Update(9998);
                clock.Advance(TimeUnit.Milliseconds, valuesIntervalMillis);
            }

            // switching to mode 2: 10 minutes more with the same rate, but smaller value
            for (int i = 0; i < 10 * valuesRatePerMinute; i++)
            {
                reservoir.Update(178);
                clock.Advance(TimeUnit.Milliseconds, valuesIntervalMillis);
            }

            // expect that quantiles should be more about mode 2 after 10 minutes
            reservoir.Snapshot.Percentile95.Should().Be(178);
        }
            public TestModule(TestClock clock)
                : base("/test")
            {
                Get["/action"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 100);
                    return(Response.AsText("response"));
                };

                Post["/post"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 200);
                    return(HttpStatusCode.OK);
                };

                Put["/put"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 200);
                    return(HttpStatusCode.OK);
                };

                Patch["/patch"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 200);
                    return(HttpStatusCode.OK);
                };

                Get["/error"] = _ => { throw new InvalidOperationException(); };
            }
Beispiel #3
0
        public void TimerMetric_CanTrackTime()
        {
            using (timer.NewContext())
            {
                clock.Advance(TimeUnit.Milliseconds, 100);
            }

            timer.Value.Histogram.Count.Should().Be(1);
            timer.Value.Histogram.Max.Should().Be(TimeUnit.Milliseconds.ToNanoseconds(100));
        }
        public void MeterMetric_CanCalculateMeanRate()
        {
            meter.Mark();
            clock.Advance(TimeUnit.Seconds, 1);

            meter.Value.MeanRate.Should().Be(1);

            clock.Advance(TimeUnit.Seconds, 1);

            meter.Value.MeanRate.Should().Be(0.5);
        }
Beispiel #5
0
        public void apdex_score_should_be_between_zero_and_one()
        {
            const double apdexTSeconds    = 0.5;
            const int    fromMilliSeconds = 20;
            const int    toMilliSeconds   = 5000;
            const int    sampleSize       = 1024;
            var          random           = new Random();
            var          clock            = new TestClock();

            IApdexMetric apdexMetric = new ApdexMetric(SamplingType.ExponentiallyDecaying, sampleSize,
                                                       Constants.ReservoirSampling.DefaultExponentialDecayFactor, clock, apdexTSeconds, false);

            foreach (var requestNumber in Enumerable.Range(0, 1000))
            {
                using (apdexMetric.NewContext())
                {
                    clock.Advance(TimeUnit.Milliseconds, random.Next(fromMilliSeconds, toMilliSeconds));
                }
            }

            var score = apdexMetric.GetValue().Score;

            score.Should().BeGreaterOrEqualTo(0);
            score.Should().BeLessOrEqualTo(1);
        }
Beispiel #6
0
        public void MeterCanCalculateMeanRate()
        {
            TestClock clock = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);

            var meter = new MeterMetric(clock, scheduler);

            meter.Mark();
            clock.Advance(TimeUnit.Seconds, 1);

            meter.Value.MeanRate.Should().Be(1);

            clock.Advance(TimeUnit.Seconds, 1);

            meter.Value.MeanRate.Should().Be(0.5);
        }
Beispiel #7
0
        public void apdex_score_should_be_between_zero_and_one()
        {
            const double apdexTSeconds    = 0.5;
            const int    fromMilliSeconds = 20;
            const int    toMilliSeconds   = 5000;
            var          random           = new Random();
            var          clock            = new TestClock();

            var reservoir = new Lazy <IReservoir>(() => new DefaultForwardDecayingReservoir());

            IApdexMetric apdexMetric = new DefaultApdexMetric(reservoir, apdexTSeconds, clock, false);

            foreach (var unused in Enumerable.Range(0, 1000))
            {
                using (apdexMetric.NewContext())
                {
                    clock.Advance(TimeUnit.Milliseconds, random.Next(fromMilliSeconds, toMilliSeconds));
                }
            }

            var score = apdexMetric.GetValue().Score;

            score.Should().BeGreaterOrEqualTo(0);
            score.Should().BeLessOrEqualTo(1);
        }
Beispiel #8
0
        public void can_calculate_the_hit_ratio_as_a_guage_with_one_min_rate_as_default()
        {
            var clock     = new TestClock();
            var scheduler = new TestTaskScheduler(clock);

            var cacheHitMeter = new DefaultMeterMetric(clock, scheduler);
            var dbQueryTimer  = new DefaultTimerMetric(new DefaultAlgorithmRReservoir(1028), clock);

            foreach (var index in Enumerable.Range(0, 1000))
            {
                using (dbQueryTimer.NewContext())
                {
                    clock.Advance(TimeUnit.Milliseconds, 100);
                }

                if (index % 2 == 0)
                {
                    cacheHitMeter.Mark();
                }
            }

            var cacheHitRatioGauge = new HitRatioGauge(cacheHitMeter, dbQueryTimer);

            cacheHitRatioGauge.Value.Should().BeGreaterThan(0.0);
        }
Beispiel #9
0
        public void TimerContextRecordsTimeOnlyOnFirstDispose()
        {
            TestClock clock = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);

            TimerMetric timer = new TimerMetric(SamplingType.LongTerm, new MeterMetric(clock, scheduler), clock);

            var context = timer.NewContext();
            clock.Advance(TimeUnit.Milliseconds, 100);
            using (context) { }
            clock.Advance(TimeUnit.Milliseconds, 100);
            using (context) { }

            timer.Value.Histogram.Count.Should().Be(1);
            timer.Value.Histogram.Max.Should().Be(TimeUnit.Milliseconds.ToNanoseconds(100));
        }
Beispiel #10
0
        public void EDR_QuantiliesShouldBeBasedOnWeights()
        {
            TestClock     clock     = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);
            ExponentiallyDecayingReservoir reservoir = new ExponentiallyDecayingReservoir(1000, 0.015, clock, scheduler);

            for (int i = 0; i < 40; i++)
            {
                reservoir.Update(177);
            }

            clock.Advance(TimeUnit.Seconds, 120);

            for (int i = 0; i < 10; i++)
            {
                reservoir.Update(9999);
            }

            reservoir.Snapshot.Size.Should().Be(50);

            // the first added 40 items (177) have weights 1
            // the next added 10 items (9999) have weights ~6
            // so, it's 40 vs 60 distribution, not 40 vs 10
            reservoir.Snapshot.Median.Should().Be(9999);
            reservoir.Snapshot.Percentile75.Should().Be(9999);
        }
            public TestModule(TestClock clock)
                : base("/test")
            {
                Get["/action"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 100);
                    return Response.AsText("response");
                };

                Post["/post"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 200);
                    return HttpStatusCode.OK;
                };

                Get["/error"] = _ => { throw new InvalidOperationException(); };
            }
Beispiel #12
0
 private static void RunSamples(IEnumerable <int> satisfiedRequestsDurations, IApdexMetric apdexMetric, TestClock clock)
 {
     foreach (var duration in satisfiedRequestsDurations)
     {
         using (apdexMetric.NewContext())
         {
             clock.Advance(TimeUnit.Milliseconds, duration);
         }
     }
 }
            public TestModule(TestClock clock)
                : base("/test")
            {
                this.MetricForRequestTimeAndResponseSize("ActionRequest", "Get", "/");
                this.MetricForRequestSize("RequestSize", "Put", "/");

                Get["/action"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 100);
                    return(Response.AsText("response"));
                };

                Get["/contentWithLength"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 100);
                    return(Response.AsText("response").WithHeader("Content-Length", "100"));
                };

                Put["/size"] = _ => HttpStatusCode.OK;
            }
            public TestModule(TestClock clock)
                : base("/test")
            {
                this.MetricForRequestTimeAndResponseSize("Action Request", "Get", "/");
                this.MetricForRequestSize("Request Size", "Put", "/");

                Get["/action"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 100);
                    return Response.AsText("response");
                };

                Get["/contentWithLength"] = _ =>
                {
                    clock.Advance(TimeUnit.Milliseconds, 100);
                    return Response.AsText("response").WithHeader("Content-Length", "100");
                };

                Put["/size"] = _ => HttpStatusCode.OK;
            }
Beispiel #15
0
        public void EDR_longPeriodsOfInactivityShouldNotCorruptSamplingState()
        {
            TestClock     clock     = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);

            ExponentiallyDecayingReservoir reservoir = new ExponentiallyDecayingReservoir(10, 0.015, clock, scheduler);

            // add 1000 values at a rate of 10 values/second
            for (int i = 0; i < 1000; i++)
            {
                reservoir.Update(1000 + i);
                clock.Advance(TimeUnit.Milliseconds, 100);
            }

            reservoir.Snapshot.Size.Should().Be(10);
            reservoir.Snapshot.Values.Should().OnlyContain(v => 1000 <= v && v < 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 scaling factor that will make all existing priorities equal to
            // zero after rescale.
            clock.Advance(TimeUnit.Hours, 15);
            reservoir.Update(2000);
            var snapshot = reservoir.Snapshot;

            snapshot.Size.Should().Be(2);
            snapshot.Values.Should().OnlyContain(v => 1000 <= v && v < 3000);

            // add 1000 values at a rate of 10 values/second
            for (int i = 0; i < 1000; i++)
            {
                reservoir.Update(3000 + i);
                clock.Advance(TimeUnit.Milliseconds, 100);
            }

            var finalSnapshot = reservoir.Snapshot;

            finalSnapshot.Size.Should().Be(10);
            // TODO: double check the Skip first value - sometimes first value is 2000 - which might or not be correct
            finalSnapshot.Values.Skip(1).Should().OnlyContain(v => 3000 <= v && v < 4000);
        }
        public void EDRlongPeriodsOfInactivityShouldNotCorruptSamplingState()
        {
            TestClock clock = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);

            ExponentiallyDecayingReservoir reservoir = new ExponentiallyDecayingReservoir(10, 0.015, clock, scheduler);

            // add 1000 values at a rate of 10 values/second
            for (int i = 0; i < 1000; i++)
            {
                reservoir.Update(1000 + i);
                clock.Advance(TimeUnit.Milliseconds, 100);
            }

            reservoir.Snapshot.Size.Should().Be(10);
            reservoir.Snapshot.Values.Should().OnlyContain(v => 1000 <= v && v < 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 scaling factor that will make all existing priorities equal to
            // zero after rescale.
            clock.Advance(TimeUnit.Hours, 15);
            reservoir.Update(2000);
            var snapshot = reservoir.Snapshot;
            snapshot.Size.Should().Be(2);
            snapshot.Values.Should().OnlyContain(v => 1000 <= v && v < 3000);

            // add 1000 values at a rate of 10 values/second
            for (int i = 0; i < 1000; i++)
            {
                reservoir.Update(3000 + i);
                clock.Advance(TimeUnit.Milliseconds, 100);
            }

            var finalSnapshot = reservoir.Snapshot;

            finalSnapshot.Size.Should().Be(10);
            // TODO: double check the Skip first value - sometimes first value is 2000 - which might or not be correct
            finalSnapshot.Values.Skip(1).Should().OnlyContain(v => 3000 <= v && v < 4000);
        }
Beispiel #17
0
        public void TimerCanTrackTime()
        {
            TestClock clock = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);

            TimerMetric timer = new TimerMetric(SamplingType.LongTerm, new MeterMetric(clock, scheduler), clock);
            using (timer.NewContext())
            {
                clock.Advance(TimeUnit.Milliseconds, 100);
            }
            timer.Value.Histogram.Count.Should().Be(1);
            timer.Value.Histogram.Max.Should().Be(TimeUnit.Milliseconds.ToNanoseconds(100));
        }
Beispiel #18
0
        public void MeterCanComputeRates()
        {
            TestClock clock = new TestClock();
            TestScheduler scheduler = new TestScheduler(clock);

            var meter = new MeterMetric(clock, scheduler);

            meter.Mark();
            clock.Advance(TimeUnit.Seconds, 10);
            meter.Mark(2);

            var value = meter.Value;

            value.MeanRate.Should().BeApproximately(0.3, 0.001);
            value.OneMinuteRate.Should().BeApproximately(0.1840, 0.001);
            value.FiveMinuteRate.Should().BeApproximately(0.1966, 0.001);
            value.FifteenMinuteRate.Should().BeApproximately(0.1988, 0.001);
        }