/
HistogramEncodingTestBase.cs
86 lines (75 loc) · 3.34 KB
/
HistogramEncodingTestBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using HdrHistogram.Encoding;
using HdrHistogram.Utilities;
using Xunit;
namespace HdrHistogram.UnitTests
{
public abstract class HistogramEncodingTestBase
{
protected const long DefautltLowestDiscernibleValue = 1;
protected const long DefaultHighestTrackableValue = 7716549600;//TimeStamp.Hours(1); // e.g. for 1 hr in system clock ticks (StopWatch.Frequency)
protected const int DefaultSignificantFigures = 3;
private static readonly HistogramEncoderV2 EncoderV2 = new Encoding.HistogramEncoderV2();
[Fact]
public void Given_a_populated_Histogram_When_encoded_and_decoded_Then_data_is_preserved()
{
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
Load(source);
var result = EncodeDecode(source);
HistogramAssert.AreValueEqual(source, result);
}
[Fact]
public void Given_a_populated_Histogram_When_encoded_and_decoded_with_compression_Then_data_is_preserved()
{
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
Load(source);
var result = CompressedEncodeDecode(source);
HistogramAssert.AreValueEqual(source, result);
}
[Fact]
public void Given_a_Histogram_populated_with_full_range_of_values_When_encoded_and_decoded_Then_data_is_preserved()
{
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
LoadFullRange(source);
var result = EncodeDecode(source);
HistogramAssert.AreValueEqual(source, result);
}
[Fact]
public void Given_a_Histogram_populated_with_full_range_of_values_When_encoded_and_decoded_with_compression_Then_data_is_preserved()
{
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
LoadFullRange(source);
var result = CompressedEncodeDecode(source);
HistogramAssert.AreValueEqual(source, result);
}
protected abstract HistogramBase Create(long highestTrackableValue, int numberOfSignificantDigits);
private static HistogramBase EncodeDecode(HistogramBase source)
{
var targetBuffer = ByteBuffer.Allocate(source.GetNeededByteBufferCapacity());
source.Encode(targetBuffer, EncoderV2);
targetBuffer.Position = 0;
return HistogramEncoding.DecodeFromByteBuffer(targetBuffer, 0);
}
private static HistogramBase CompressedEncodeDecode(HistogramBase source)
{
var targetBuffer = ByteBuffer.Allocate(source.GetNeededByteBufferCapacity());
source.EncodeIntoCompressedByteBuffer(targetBuffer);
targetBuffer.Position = 0;
return HistogramEncoding.DecodeFromCompressedByteBuffer(targetBuffer, 0);
}
private static void Load(IRecorder source)
{
for (long i = 0L; i < 10000L; i++)
{
source.RecordValue(1000L * i);
}
}
protected virtual void LoadFullRange(IRecorder source)
{
for (long i = 0L; i < DefaultHighestTrackableValue; i += 100L)
{
source.RecordValue(i);
}
source.RecordValue(DefaultHighestTrackableValue);
}
}
}