11using System ;
2+ using System . IO ;
3+ using System . Threading ;
24using NUnit . Framework ;
35
46namespace 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}
0 commit comments