public void TestRateMeasure() { // start timing var stopwatch = Stopwatch.StartNew(); var sampleCutoff = TimeSpan.FromSeconds(1); var sampleResolution = TimeSpan.FromMilliseconds(10); using (var rateMeasure = new RateMeasure(sampleCutoff, sampleResolution)) { // tick once per duration, count times var count = 10; var duration = TimeSpan.FromMilliseconds(20); for (var i = 0; i < count; i++) { Thread.Sleep(duration); rateMeasure.Tick(); } // stop timing stopwatch.Stop(); // the average rate, per the amount of the time the test has run for, should be as close as possible to count Assert.AreEqual(count, rateMeasure.GetAverage(stopwatch.Elapsed), 0.5); // wait for the cutoff time to pass and verify the rate dropped to 0 Thread.Sleep(duration + rateMeasure.SampleCutoff); Assert.AreEqual(0, rateMeasure.GetAverage(duration)); } }
public override string ToString() { var statString = new StringBuilder(); TimeSpan duration; lock (durationStopwatch) duration = durationStopwatch.Elapsed; var durationFormatted = $"{Math.Floor(duration.TotalHours):#,#00}:{duration:mm':'ss}"; statString.AppendLine($"Chain State Builder Stats"); statString.AppendLine($"-------------------------"); statString.AppendLine($"Height: {Height,15:N0}"); statString.AppendLine($"Duration: {durationFormatted,15}"); statString.AppendLine($"-------------------------"); statString.AppendLine($"Blocks Rate: {blockRateMeasure.GetAverage(),15:N0}/s"); statString.AppendLine($"Tx Rate: {txRateMeasure.GetAverage(),15:N0}/s"); statString.AppendLine($"Input Rate: {inputRateMeasure.GetAverage(),15:N0}/s"); statString.AppendLine($"-------------------------"); statString.AppendLine($"Txes per block: {txesPerBlockMeasure.GetAverage(),15:N0}"); statString.AppendLine($"Inputs per block: {inputsPerBlockMeasure.GetAverage(),15:N0}"); statString.AppendLine($"-------------------------"); statString.AppendLine($"Processed Txes: {TotalTxCount,15:N0}"); statString.AppendLine($"Processed Inputs: {TotalInputCount,15:N0}"); statString.AppendLine($"Utx Size: {UnspentTxCount,15:N0}"); statString.AppendLine($"Utxo Size: {UnspentOutputCount,15:N0}"); statString.AppendLine($"-------------------------"); var txesReadDuration = txesReadDurationMeasure.GetAverage(); var txesDecodeDuration = txesDecodeDurationMeasure.GetAverage(); var lookAheadDuration = lookAheadDurationMeasure.GetAverage(); var calculateUtxoDuration = calculateUtxoDurationMeasure.GetAverage(); var applyUtxoDuration = applyUtxoDurationMeasure.GetAverage(); var validateDuration = validateDurationMeasure.GetAverage(); var commitUtxoDuration = commitUtxoDurationMeasure.GetAverage(); var addBlockDuration = addBlockDurationMeasure.GetAverage(); statString.AppendLine(GetPipelineStat("Block Txes Read", txesReadDuration, TimeSpan.Zero)); statString.AppendLine(GetPipelineStat("Block Txes Decode", txesDecodeDuration, txesReadDuration)); statString.AppendLine(GetPipelineStat("UTXO Look-ahead", lookAheadDuration, txesDecodeDuration)); statString.AppendLine(GetPipelineStat("UTXO Calculation", calculateUtxoDuration, lookAheadDuration)); statString.AppendLine(GetPipelineStat("UTXO Application", applyUtxoDuration, calculateUtxoDuration)); statString.AppendLine(GetPipelineStat("Block Validation", validateDuration, calculateUtxoDuration)); statString.AppendLine(GetPipelineStat("UTXO Commit", commitUtxoDuration, TimeSpan.FromTicks(Math.Max(applyUtxoDuration.Ticks, validateDuration.Ticks)))); statString.Append(GetPipelineStat("AddBlock Total", addBlockDuration, null)); return(statString.ToString()); }
public void TestRateMeasure() { var sampleCutoff = TimeSpan.FromSeconds(1); var sampleResolution = TimeSpan.FromMilliseconds(10); using (var rateMeasure = new RateMeasure(sampleCutoff, sampleResolution)) { // tick once per duration, count times var count = 10; var duration = TimeSpan.FromMilliseconds(20); for (var i = 0; i < count; i++) { Thread.Sleep(duration); rateMeasure.Tick(); } // the average rate per duration unit time should be as close as possible to 1 Assert.AreEqual(1, rateMeasure.GetAverage(duration), 0.1); // wait for the cutoff time to pass and verify the rate dropped to 0 Thread.Sleep(duration + rateMeasure.SampleCutoff); Assert.AreEqual(0, rateMeasure.GetAverage(duration)); } }