Skip to content

Commit 0f0fbdb

Browse files
committed
Concurrent Long and Int histograms
1 parent c81852a commit 0f0fbdb

14 files changed

Lines changed: 773 additions & 0 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace HdrHistogram.UnitTests
7+
{
8+
public abstract class ConcurrentHistogramTestBase : HistogramTestBase
9+
{
10+
[Test]
11+
public void Can_support_multiple_concurrent_recorders()
12+
{
13+
var target = Create(1, long.MaxValue - 1, 3);
14+
const int loopcount = 10 * 1000 * 1000;
15+
var concurrency = Environment.ProcessorCount;
16+
var expected = loopcount * concurrency;
17+
Action foo = () =>
18+
{
19+
for (var i = 0; i < loopcount; i++)
20+
target.RecordValue(i);
21+
};
22+
23+
var actions = Enumerable.Range(1, concurrency)
24+
.Select(_ => foo)
25+
.ToArray();
26+
Parallel.Invoke(actions);
27+
28+
Assert.AreEqual(expected, target.TotalCount);
29+
}
30+
}
31+
}

src/HdrHistogram.UnitTests/HdrHistogram.UnitTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@
4949
<Reference Include="System.Xml" />
5050
</ItemGroup>
5151
<ItemGroup>
52+
<Compile Include="ConcurrentHistogramTestBase.cs" />
53+
<Compile Include="IntConcurrentHistogramTests.cs" />
54+
<Compile Include="LongConcurrentHistogramTests.cs" />
5255
<Compile Include="HistogramAssert.cs" />
5356
<Compile Include="HistogramEncodingTest.cs" />
5457
<Compile Include="HistogramIterationValueComparer.cs" />
58+
<Compile Include="Persistence\LongConcurrentHistogramLogReaderWriterTests.cs" />
5559
<Compile Include="Persistence\HistogramLogExtensions.cs" />
5660
<Compile Include="Persistence\HistogramLogReaderWriterTestBase.cs" />
61+
<Compile Include="Persistence\IntConcurrentHistogramLogReaderWriterTests.cs" />
5762
<Compile Include="Persistence\IntHistogramLogReaderWriterTests.cs" />
5863
<Compile Include="HistogramTestBase.cs" />
5964
<Compile Include="IntHistogramTests.cs" />
@@ -65,6 +70,7 @@
6570
<Compile Include="Persistence\TestCaseGenerator.cs" />
6671
<Compile Include="Properties\AssemblyInfo.cs" />
6772
<Compile Include="Recording\RecorderTestsBase.cs" />
73+
<Compile Include="Recording\RecorderTestWithConcurrentHistogram.cs" />
6874
<Compile Include="Recording\RecorderTestWithIntHistogram.cs" />
6975
<Compile Include="Recording\RecorderTestWithLongHistogram.cs" />
7076
<Compile Include="Recording\RecorderTestWithLShortHistogram.cs" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests
4+
{
5+
[TestFixture]
6+
public class IntConcurrentHistogramTests : ConcurrentHistogramTestBase
7+
{
8+
protected override int WordSize => sizeof(int);
9+
10+
protected override HistogramBase Create(long highestTrackableValue, int numberOfSignificantValueDigits)
11+
{
12+
return new IntConcurrentHistogram(1, highestTrackableValue, numberOfSignificantValueDigits);
13+
}
14+
15+
protected override HistogramBase Create(long lowestTrackableValue, long highestTrackableValue, int numberOfSignificantValueDigits)
16+
{
17+
return new IntConcurrentHistogram(lowestTrackableValue, highestTrackableValue, numberOfSignificantValueDigits);
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests
4+
{
5+
[TestFixture]
6+
public class LongConcurrentHistogramTests : ConcurrentHistogramTestBase
7+
{
8+
protected override int WordSize => sizeof(long);
9+
10+
protected override HistogramBase Create(long highestTrackableValue, int numberOfSignificantValueDigits)
11+
{
12+
return new LongConcurrentHistogram(1, highestTrackableValue, numberOfSignificantValueDigits);
13+
}
14+
15+
protected override HistogramBase Create(long lowestTrackableValue, long highestTrackableValue, int numberOfSignificantValueDigits)
16+
{
17+
return new LongConcurrentHistogram(lowestTrackableValue, highestTrackableValue, numberOfSignificantValueDigits);
18+
}
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests.Persistence
4+
{
5+
[TestFixture]
6+
public sealed class IntConcurrentHistogramLogReaderWriterTests : HistogramLogReaderWriterTestBase
7+
{
8+
protected override HistogramBase Create(long highestTrackableValue, int numberOfSignificantValueDigits)
9+
{
10+
return new IntConcurrentHistogram(1, highestTrackableValue, numberOfSignificantValueDigits);
11+
}
12+
13+
[Test, TestCaseSource(typeof(TestCaseGenerator), nameof(TestCaseGenerator.PowersOfTwo), new object[] { 31 })]
14+
public void CanRoundTripSingleHistogramsWithFullRangesOfCountsAndValues(long count)
15+
{
16+
RoundTripSingleHistogramsWithFullRangesOfCountsAndValues(count);
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests.Persistence
4+
{
5+
[TestFixture]
6+
public sealed class LongConcurrentHistogramLogReaderWriterTests : HistogramLogReaderWriterTestBase
7+
{
8+
protected override HistogramBase Create(long highestTrackableValue, int numberOfSignificantValueDigits)
9+
{
10+
return new LongConcurrentHistogram(1, highestTrackableValue, numberOfSignificantValueDigits);
11+
}
12+
13+
[Test, TestCaseSource(typeof(TestCaseGenerator), nameof(TestCaseGenerator.PowersOfTwo), new object[] { 63 })]
14+
public void CanRoundTripSingleHistogramsWithFullRangesOfCountsAndValues(long count)
15+
{
16+
RoundTripSingleHistogramsWithFullRangesOfCountsAndValues(count);
17+
}
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests.Recording
4+
{
5+
[TestFixture]
6+
public sealed class RecorderTestWithConcurrentHistogram : RecorderTestsBase
7+
{
8+
protected override HistogramBase Create(long id, long min, long max, int sf)
9+
{
10+
return new LongConcurrentHistogram(id, min, max, sf);
11+
}
12+
}
13+
}

src/HdrHistogram/HdrHistogram.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
<Reference Include="System.Xml" />
4747
</ItemGroup>
4848
<ItemGroup>
49+
<Compile Include="IntConcurrentHistogram.cs" />
50+
<Compile Include="LongConcurrentHistogram.cs" />
4951
<Compile Include="HistogramEncoding.cs" />
5052
<Compile Include="Encoding\HistogramEncoderV2.cs" />
5153
<Compile Include="Encoding\IEncoder.cs" />
@@ -85,6 +87,8 @@
8587
<Compile Include="ShortHistogram.cs" />
8688
<Compile Include="TimeStamp.cs" />
8789
<Compile Include="Utilities\ArrayExtensions.cs" />
90+
<Compile Include="Utilities\AtomicIntArray.cs" />
91+
<Compile Include="Utilities\AtomicLongArray.cs" />
8892
<Compile Include="Utilities\Bitwise.cs" />
8993
<Compile Include="LongHistogram.cs" />
9094
<Compile Include="Properties\AssemblyInfo.cs" />

src/HdrHistogram/HistogramBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public abstract class HistogramBase : IRecorder
4646
private long _maxValue;
4747
private long _minNonZeroValue;
4848

49+
/// <summary>
50+
/// An identifier for the Histogram. Maybe generated by the Recorder if used.
51+
/// </summary>
4952
public long InstanceId { get; }
5053

5154
/// <summary>

0 commit comments

Comments
 (0)