public void Should_EmptySnapshotBeThreadSafe()
        {
            var target = new HdrHistogramReservoir(1, 1000, 3, 10000);

            Parallel.ForEach(Enumerable.Range(1, 100000), i => AssertEmptySnapshot(target.GetSnapshot(false)));
            target.Reset();
            Parallel.ForEach(Enumerable.Range(1, 100000), i => AssertEmptySnapshot(target.GetSnapshot(false)));
            Parallel.ForEach(Enumerable.Range(1, 1000), i => AssertEmptySnapshot(target.GetSnapshot(true)));
        }
        public async Task Should_ReturnEmptyHistogram_When_ResetIsCalled()
        {
            var target = new HdrHistogramReservoir(1, 1000000000000, 3, 2000);

            var tcs = new CancellationTokenSource();

            AssertEmptySnapshot(target.GetSnapshot(false));

            Parallel.ForEach(Enumerable.Range(1, 100), i => target.Update(i));

            AssertEmptySnapshot(target.GetSnapshot(false));
            await Task.Yield();

            AssertEmptySnapshot(target.GetSnapshot(false));

            TestHelper.RetryAssert(
                () =>
            {
                AssertFirstSnapshot(target.GetSnapshot(false));
            },
                200,
                20);

            var tasks = Enumerable.Range(0, 4).Select(i => Task.Run(async() =>
            {
                var current = i * 100000;
                while (!tcs.IsCancellationRequested)
                {
                    if (current == int.MaxValue)
                    {
                        current = 0;
                    }

                    target.Update(++current);
                    await Task.Yield();
                }
            })).ToList();

            try
            {
                AssertFirstSnapshot(target.GetSnapshot(false));
                await Task.Yield();

                AssertFirstSnapshot(target.GetSnapshot(false));

                // reset
                AssertFirstSnapshot(target.GetSnapshot(true));

                AssertEmptySnapshot(target.GetSnapshot(false));
                await Task.Yield();

                AssertEmptySnapshot(target.GetSnapshot(false));
            }
            finally
            {
                tcs.Cancel();
                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
        }
        public void Should_RefreshEntireHistogramAfterRefreshInterval()
        {
            var target = new HdrHistogramReservoir(1, 30000, 3, 1000);

            Parallel.ForEach(Enumerable.Range(1, 100), i => target.Update(i));

            AssertEmptySnapshot(target.GetSnapshot(false));
            AssertEmptySnapshot(target.GetSnapshot(false));

            TestHelper.RetryAssert(
                () =>
            {
                AssertFirstSnapshot(target.GetSnapshot(false));
            },
                200,
                15);

            Parallel.ForEach(Enumerable.Range(201, 100), i => target.Update(i));

            AssertFirstSnapshot(target.GetSnapshot(false));
            AssertFirstSnapshot(target.GetSnapshot(false));

            TestHelper.RetryAssert(
                () =>
            {
                AssertSecondSnapshot(target.GetSnapshot(false));
            },
                200,
                15);

            AssertSecondSnapshot(target.GetSnapshot(false));
            AssertSecondSnapshot(target.GetSnapshot(false));
        }
Exemple #4
0
        public void can_records_user_value()
        {
            var reservoir = new HdrHistogramReservoir(new Recorder(2));

            reservoir.Update(2L, "B");
            reservoir.Update(1L, "A");

            var snapshot = reservoir.GetSnapshot();

            snapshot.MinUserValue.Should().Be("A");
            snapshot.Min.Should().Be(1L);
            snapshot.MaxUserValue.Should().Be("B");
            snapshot.Max.Should().Be(2L);
        }
Exemple #5
0
        public void snap_shot_reflects_what_was_recorded()
        {
            var reservoir = new HdrHistogramReservoir(new Recorder(2));

            for (var i = 0; i < 1000; i++)
            {
                reservoir.Update(i);
            }

            var snapshot = reservoir.GetSnapshot();

            snapshot.Count.Should().Be(1000);
            snapshot.Median.Should().Be(499);
            snapshot.Mean.Should().BeApproximately(499, 1);
            snapshot.Min.Should().Be(0);
            snapshot.Max.Should().Be(999);
            snapshot.Size.Should().Be(4608);
            snapshot.Percentile75.Should().Be(751);
            snapshot.Percentile95.Should().Be(951);
            snapshot.Percentile98.Should().Be(979);
            snapshot.Percentile99.Should().Be(991);
            snapshot.Percentile999.Should().Be(999);
        }