public void TestAverageMeasure()
        {
            var sampleCutoff = TimeSpan.FromMilliseconds(5000);
            var sampleResolution = TimeSpan.FromMilliseconds(100);
            using (var averageMeasure = new AverageMeasure(sampleCutoff, sampleResolution))
            {
                // add samples
                var count = 5;
                var value = 10;
                for (var i = 0; i < count; i++)
                {
                    averageMeasure.Tick(value);
                }

                // wait half the cutoff time and verify average
                Thread.Sleep(new TimeSpan(sampleCutoff.Ticks / 2));
                Assert.AreEqual((int)value, (int)averageMeasure.GetAverage());

                // wait for the cutoff time to pass and verify the average dropped to 0
                Thread.Sleep(sampleCutoff);
                Assert.AreEqual(0, (int)averageMeasure.GetAverage());

                // add new samples
                value = 50;
                for (var i = 0; i < count; i++)
                {
                    averageMeasure.Tick(value);
                }

                // verify average
                Assert.AreEqual((int)value, (int)averageMeasure.GetAverage());
            }
        }
        public void TestAverageMeasure()
        {
            var sampleCutoff     = TimeSpan.FromMilliseconds(5000);
            var sampleResolution = TimeSpan.FromMilliseconds(100);

            using (var averageMeasure = new AverageMeasure(sampleCutoff, sampleResolution))
            {
                // add samples
                var count = 5;
                var value = 10;
                for (var i = 0; i < count; i++)
                {
                    averageMeasure.Tick(value);
                }

                // wait half the cutoff time and verify average
                Thread.Sleep(new TimeSpan(sampleCutoff.Ticks / 2));
                Assert.AreEqual((int)value, (int)averageMeasure.GetAverage());

                // wait for the cutoff time to pass and verify the average dropped to 0
                Thread.Sleep(sampleCutoff);
                Assert.AreEqual(0, (int)averageMeasure.GetAverage());

                // add new samples
                value = 50;
                for (var i = 0; i < count; i++)
                {
                    averageMeasure.Tick(value);
                }

                // verify average
                Assert.AreEqual((int)value, (int)averageMeasure.GetAverage());
            }
        }
Beispiel #3
0
        private async Task PruneBlock(PruningMode mode, Chain chain, ChainedHeader pruneBlock)
        {
            //TODO the replay information about blocks that have been rolled back also needs to be pruned (UnmintedTx)

            var txCount                 = 0;
            var totalStopwatch          = Stopwatch.StartNew();
            var pruneBlockTxesStopwatch = new Stopwatch();
            var pruneTxIndexStopwatch   = new Stopwatch();
            var pruneSpentTxesStopwatch = new Stopwatch();

            // retrieve the spent txes for this block
            BlockSpentTxes spentTxes;

            using (var handle = this.storageManager.OpenChainStateCursor())
            {
                var chainStateCursor = handle.Item;

                chainStateCursor.BeginTransaction(readOnly: true);
                chainStateCursor.TryGetBlockSpentTxes(pruneBlock.Height, out spentTxes);
            }

            if (spentTxes != null)
            {
                txCount = spentTxes.Count;

                pruneBlockTxesStopwatch.Start();
                pruneTxIndexStopwatch.Start();

                await Task.WhenAll(
                    // prune block txes (either merkle prune or delete)
                    PruneBlockTxesAsync(mode, chain, pruneBlock, spentTxes)
                    .ContinueWith(_ => pruneBlockTxesStopwatch.Stop()),
                    // prune tx index
                    PruneTxIndexAsync(mode, chain, pruneBlock, spentTxes)
                    .ContinueWith(_ => pruneTxIndexStopwatch.Stop())
                    );

                // remove block spent txes information
                //TODO should have a buffer on removing this, block txes pruning may need it again if flush doesn't happen
                var pruneSpentTxesTask = PruneBlockSpentTxes(mode, chain, pruneBlock);
                await pruneSpentTxesStopwatch.TimeAsync(pruneSpentTxesTask);
            }
            // if spent txes aren't available, block txes can still be deleted entirely for that pruning style
            else if (mode.HasFlag(PruningMode.BlockTxesDelete))
            {
                pruneBlockTxesStopwatch.Start();
                await PruneBlockTxesAsync(mode, chain, pruneBlock, null);

                pruneBlockTxesStopwatch.Stop();
            }
            else //if (pruneBlock.Height > 0)
            {
                //TODO can't throw an exception unless the pruned chain is persisted
                //logger.Info("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: {0:N0}".Format2(pruneBlock.Height));
                //throw new InvalidOperationException();
                txCount = 0;
            }

            // track stats
            txCountMeasure.Tick(txCount);
            txRateMeasure.Tick((float)(txCount / totalStopwatch.Elapsed.TotalSeconds));
            pruneBlockTxesDurationMeasure.Tick(pruneBlockTxesStopwatch.Elapsed);
            pruneTxIndexDurationMeasure.Tick(pruneTxIndexStopwatch.Elapsed);
            pruneSpentTxesDurationMeasure.Tick(pruneSpentTxesStopwatch.Elapsed);
            totalDurationMeasure.Tick(totalStopwatch.Elapsed);
        }