Skip to content

Commit 7334692

Browse files
committed
Unit test Writer.Append(). Clean up
1 parent 2d18fbb commit 7334692

8 files changed

Lines changed: 115 additions & 10 deletions

File tree

src/HdrHistogram.Examples/RecorderExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace HdrHistogram.Examples
1414
/// </summary>
1515
static class RecorderExample
1616
{
17-
private static readonly Recorder Recorder = new Recorder(1, TimeSpan.TicksPerHour, 3, (id, low, high, sf) => new LongHistogram(id, low, high, sf));
17+
private static readonly Recorder Recorder = new Recorder(1, TimeStamp.Hours(1), 3, (id, low, high, sf) => new LongHistogram(id, low, high, sf));
1818
private static readonly Lazy<AddressFamily> AddressFamily = new Lazy<AddressFamily>(() => GetAddressFamily("google.com"));
1919
private static readonly TimeSpan RunPeriod = TimeSpan.FromSeconds(10);
20-
private static readonly LongHistogram AccumulatingHistogram = new LongHistogram(TimeSpan.TicksPerHour, 3);
20+
private static readonly LongHistogram AccumulatingHistogram = new LongHistogram(TimeStamp.Hours(1), 3);
2121
private const string LogPath = "DatagramSocket.histogram.log";
2222
private static HistogramLogWriter _logWriter;
2323
private static FileStream _outputStream;

src/HdrHistogram.Examples/SimpleHistogramExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static void RecordMeasurements()
4949
}
5050

5151
/// <summary>
52-
/// Write to the console the Memory footprint of the histogram instance and
52+
/// Write to the console the memory footprint of the histogram instance and
5353
/// the percentile distribution of all the recorded values.
5454
/// </summary>
5555
private static void OutputMeasurements()

src/HdrHistogram.UnitTests/Persistence/HistogramLogReaderWriterTestBase.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ public void CanRoundTripSingleHistogramsWithSparseValues()
7878
HistogramAssert.AreValueEqual(histogram, actualHistograms.Single());
7979
}
8080

81+
[Test]
82+
public void CanAppendHistogram()
83+
{
84+
var histogram1 = Create(highestTrackableValue: long.MaxValue - 1, numberOfSignificantValueDigits: 3);
85+
histogram1.RecordValue(1);
86+
histogram1.RecordValue((long.MaxValue / 2) + 1);
87+
histogram1.SetTimes();
88+
var histogram2 = Create(highestTrackableValue: long.MaxValue - 1, numberOfSignificantValueDigits: 3);
89+
histogram2.RecordValue(2);
90+
histogram2.SetTimes();
91+
92+
byte[] data;
93+
using (var writerStream = new MemoryStream())
94+
using(var log = new HistogramLogWriter(writerStream))
95+
{
96+
log.Append(histogram1);
97+
log.Append(histogram2);
98+
data = writerStream.ToArray();
99+
}
100+
var actualHistograms = data.ReadHistograms();
101+
102+
Assert.AreEqual(2, actualHistograms.Length);
103+
HistogramAssert.AreValueEqual(histogram1, actualHistograms.First());
104+
HistogramAssert.AreValueEqual(histogram2, actualHistograms.Skip(1).First());
105+
}
106+
81107
[TestCase("jHiccup-2.0.7S.logV2.hlog")]
82108
public void CanReadv2Logs(string logPath)
83109
{

src/HdrHistogram/HistogramExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static void OutputPercentileDistribution(this HistogramBase histogram,
169169
/// conversion at time of recording.
170170
/// Instead conversion (scaling) can be done at time of output to microseconds, milliseconds,
171171
/// seconds or other appropriate unit.
172-
/// These units are sometimes refered to as ticks, but should not not to be confused with
172+
/// These units are sometimes referred to as ticks, but should not not to be confused with
173173
/// ticks used in <seealso cref="DateTime"/> or <seealso cref="TimeSpan"/>.
174174
/// </para>
175175
/// <para>

src/HdrHistogram/HistogramLogWriter.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public sealed class HistogramLogWriter : IDisposable
1313
private const string HistogramLogFormatVersion = "1.2";
1414

1515
private readonly TextWriter _log;
16+
private bool _hasHeaderWritten = false;
1617

1718
/// <summary>
1819
/// Writes the provided histograms to the underlying <see cref="Stream"/> with a given overall start time.
@@ -27,12 +28,7 @@ public static void Write(Stream outputStream, DateTime startTime, params Histogr
2728
writer.Write(startTime, histograms);
2829
}
2930
}
30-
31-
public void Append(HistogramBase histogram)
32-
{
33-
WriteHistogram(histogram);
34-
}
35-
31+
3632
/// <summary>
3733
/// Creates a <see cref="HistogramLogWriter"/> that writes to an underlying <see cref="Stream"/>.
3834
/// </summary>
@@ -58,12 +54,29 @@ public void Write(DateTime startTime, params HistogramBase[] histograms)
5854
WriteLogFormatVersion();
5955
WriteStartTime(startTime);
6056
WriteLegend();
57+
_hasHeaderWritten = true;
6158
foreach (var histogram in histograms)
6259
{
6360
WriteHistogram(histogram);
6461
}
6562
}
6663

64+
/// <summary>
65+
/// Appends a Histogram to the log.
66+
/// </summary>
67+
/// <param name="histogram">The histogram to write to the log.</param>
68+
public void Append(HistogramBase histogram)
69+
{
70+
if (!_hasHeaderWritten)
71+
{
72+
Write(histogram.StartTimeStamp.ToDateFromMillisecondsSinceEpoch(), histogram);
73+
}
74+
else
75+
{
76+
WriteHistogram(histogram);
77+
}
78+
}
79+
6780
/// <summary>
6881
/// Output a log format version to the log.
6982
/// </summary>

src/HdrHistogram/IntHistogram.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ public IntHistogram(long lowestTrackableValue, long highestTrackableValue, int n
6666
{
6767
_counts = new int[CountsArrayLength];
6868
}
69+
70+
/// <summary>
71+
/// Construct a <see cref="IntHistogram"/> given the lowest and highest values to be tracked and a number of significant decimal digits.
72+
/// </summary>
73+
/// <param name="instanceId">An identifier for this instance.</param>
74+
/// <param name="lowestTrackableValue">The lowest value that can be tracked (distinguished from 0) by the histogram.
75+
/// Must be a positive integer that is &gt;= 1.
76+
/// May be internally rounded down to nearest power of 2.
77+
/// </param>
78+
/// <param name="highestTrackableValue">The highest value to be tracked by the histogram.
79+
/// Must be a positive integer that is &gt;= (2 * lowestTrackableValue).
80+
/// </param>
81+
/// <param name="numberOfSignificantValueDigits">
82+
/// The number of significant decimal digits to which the histogram will maintain value resolution and separation.
83+
/// Must be a non-negative integer between 0 and 5.
84+
/// </param>
85+
/// <remarks>
86+
/// Providing a lowestTrackableValue is useful in situations where the units used for the histogram's values are much
87+
/// smaller that the minimal accuracy required.
88+
/// For example when tracking time values stated in ticks (100 nanoseconds), where the minimal accuracy required is a
89+
/// microsecond, the proper value for lowestTrackableValue would be 10.
90+
/// </remarks>
6991
public IntHistogram(long instanceId, long lowestTrackableValue, long highestTrackableValue,
7092
int numberOfSignificantValueDigits)
7193
: base(instanceId, lowestTrackableValue, highestTrackableValue, numberOfSignificantValueDigits)

src/HdrHistogram/LongHistogram.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ public LongHistogram(long lowestTrackableValue, long highestTrackableValue,
6868
{
6969
_counts = new long[CountsArrayLength];
7070
}
71+
72+
/// <summary>
73+
/// Construct a <see cref="LongHistogram"/> given the lowest and highest values to be tracked and a number of significant decimal digits.
74+
/// </summary>
75+
/// <param name="instanceId">An identifier for this instance.</param>
76+
/// <param name="lowestTrackableValue">The lowest value that can be tracked (distinguished from 0) by the histogram.
77+
/// Must be a positive integer that is &gt;= 1.
78+
/// May be internally rounded down to nearest power of 2.
79+
/// </param>
80+
/// <param name="highestTrackableValue">The highest value to be tracked by the histogram.
81+
/// Must be a positive integer that is &gt;= (2 * lowestTrackableValue).
82+
/// </param>
83+
/// <param name="numberOfSignificantValueDigits">
84+
/// The number of significant decimal digits to which the histogram will maintain value resolution and separation.
85+
/// Must be a non-negative integer between 0 and 5.
86+
/// </param>
87+
/// <remarks>
88+
/// Providing a lowestTrackableValue is useful in situations where the units used for the histogram's values are much
89+
/// smaller that the minimal accuracy required.
90+
/// For example when tracking time values stated in ticks (100 nanoseconds), where the minimal accuracy required is a
91+
/// microsecond, the proper value for lowestTrackableValue would be 10.
92+
/// </remarks>
7193
public LongHistogram(long instanceId, long lowestTrackableValue, long highestTrackableValue,
7294
int numberOfSignificantValueDigits)
7395
: base(instanceId, lowestTrackableValue, highestTrackableValue, numberOfSignificantValueDigits)

src/HdrHistogram/ShortHistogram.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ public ShortHistogram(long lowestTrackableValue, long highestTrackableValue, int
6868
{
6969
_counts = new short[CountsArrayLength];
7070
}
71+
72+
/// <summary>
73+
/// Construct a <see cref="ShortHistogram"/> given the lowest and highest values to be tracked and a number of significant decimal digits.
74+
/// </summary>
75+
/// <param name="instanceId">An identifier for this instance.</param>
76+
/// <param name="lowestTrackableValue">The lowest value that can be tracked (distinguished from 0) by the histogram.
77+
/// Must be a positive integer that is &gt;= 1.
78+
/// May be internally rounded down to nearest power of 2.
79+
/// </param>
80+
/// <param name="highestTrackableValue">The highest value to be tracked by the histogram.
81+
/// Must be a positive integer that is &gt;= (2 * lowestTrackableValue).
82+
/// </param>
83+
/// <param name="numberOfSignificantValueDigits">
84+
/// The number of significant decimal digits to which the histogram will maintain value resolution and separation.
85+
/// Must be a non-negative integer between 0 and 5.
86+
/// </param>
87+
/// <remarks>
88+
/// Providing a lowestTrackableValue is useful in situations where the units used for the histogram's values are much
89+
/// smaller that the minimal accuracy required.
90+
/// For example when tracking time values stated in ticks (100 nanoseconds), where the minimal accuracy required is a
91+
/// microsecond, the proper value for lowestTrackableValue would be 10.
92+
/// </remarks>
7193
public ShortHistogram(long instanceId, long lowestTrackableValue, long highestTrackableValue,
7294
int numberOfSignificantValueDigits)
7395
: base(instanceId, lowestTrackableValue, highestTrackableValue, numberOfSignificantValueDigits)

0 commit comments

Comments
 (0)