Skip to content

Commit 7746fd1

Browse files
committed
Fixes and missing tests
1 parent 7a6de5d commit 7746fd1

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/HdrHistogram.UnitTests/HistogramTestBase.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.IO;
3+
using System.Threading;
24
using NUnit.Framework;
35

46
namespace HdrHistogram.UnitTests
@@ -21,7 +23,7 @@ public void ConstructorShouldRejectInvalidParameters(
2123
Assert.AreEqual(errorParamName, ex.ParamName);
2224
StringAssert.StartsWith(errorMessage, ex.Message);
2325
}
24-
26+
2527

2628
[TestCase(2, 2)]
2729
[TestCase(HighestTrackableValue, NumberOfSignificantValueDigits)]
@@ -123,6 +125,23 @@ public void RecordAction_increments_TotalCount()
123125
Assert.AreEqual(1, longHistogram.TotalCount);
124126
}
125127

128+
[Test]
129+
public void RecordAction_records_in_correct_units()
130+
{
131+
var pause = TimeSpan.FromSeconds(1);
132+
var longHistogram = Create(HighestTrackableValue, NumberOfSignificantValueDigits);
133+
134+
longHistogram.Record(() => Thread.Sleep(pause));
135+
136+
var stringWriter = new StringWriter();
137+
longHistogram.OutputPercentileDistribution(stringWriter, 5, OutputScalingFactor.TimeStampToMilliseconds, true);
138+
//First column of second row.
139+
var recordedMilliseconds = GetCellValue(stringWriter.ToString(), 0, 1);
140+
var actual = double.Parse(recordedMilliseconds);
141+
var expected = pause.TotalMilliseconds;
142+
var delta = expected * 0.1; //10% Variance to allow for slack in transitioning from Thread.Sleep
143+
Assert.AreEqual(expected, actual, delta);
144+
}
126145

127146
[Test]
128147
public void Reset_sets_counts_to_zero()
@@ -265,5 +284,10 @@ private static int GetBucketsNeededToCoverValue(int subBucketSize, long value)
265284
protected abstract int WordSize { get; }
266285
protected abstract HistogramBase Create(long highestTrackableValue, int numberOfSignificantValueDigits);
267286
protected abstract HistogramBase Create(long lowestTrackableValue, long highestTrackableValue, int numberOfSignificantValueDigits);
287+
288+
private static string GetCellValue(string csvData, int col, int row)
289+
{
290+
return csvData.Split('\n')[row].Split(',')[col];
291+
}
268292
}
269293
}

src/HdrHistogram/HistogramExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,18 @@ public static long HighestEquivalentValue(this HistogramBase histogram, long val
7272
return histogram.NextNonEquivalentValue(value) - 1;
7373
}
7474

75-
75+
/// <summary>
76+
/// Copy this histogram into the target histogram, overwriting it's contents.
77+
/// </summary>
78+
/// <param name="source">The source histogram</param>
79+
/// <param name="targetHistogram">the histogram to copy into</param>
80+
public static void CopyInto(this HistogramBase source, HistogramBase targetHistogram)
81+
{
82+
targetHistogram.Reset();
83+
targetHistogram.Add(source);
84+
targetHistogram.StartTimeStamp = source.StartTimeStamp;
85+
targetHistogram.EndTimeStamp = source.EndTimeStamp;
86+
}
7687

7788
/// <summary>
7889
/// Provide a means of iterating through histogram values according to percentile levels.
@@ -193,7 +204,7 @@ public static void Record(this IRecorder recorder, Action action)
193204
var start = Stopwatch.GetTimestamp();
194205
action();
195206
var elapsed = Stopwatch.GetTimestamp() - start;
196-
histogram.RecordValue(elapsed);
207+
recorder.RecordValue(elapsed);
197208
}
198209
}
199210
}

0 commit comments

Comments
 (0)