Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions HdrHistogram.Benchmarking/ByteBuffer/ByteBufferBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using BenchmarkDotNet.Attributes;
using HdrHistogram.Utilities;

namespace HdrHistogram.Benchmarking.ByteBuffer
{
[MemoryDiagnoser]
public class ByteBufferBenchmark
{
private Utilities.ByteBuffer _writeBuffer = null!;
private Utilities.ByteBuffer _readBuffer = null!;
private const int Iterations = 1000;

[GlobalSetup]
public void Setup()
{
_writeBuffer = Utilities.ByteBuffer.Allocate(Iterations * sizeof(long));
_readBuffer = Utilities.ByteBuffer.Allocate(Iterations * sizeof(long));
for (int i = 0; i < Iterations; i++)
{
_readBuffer.PutLong(i * 12345678L);
}
}

[Benchmark]
public void PutLong()
{
_writeBuffer.Position = 0;
for (int i = 0; i < Iterations; i++)
{
_writeBuffer.PutLong(i * 12345678L);
}
}

[Benchmark]
public long GetLong()
{
_readBuffer.Position = 0;
long last = 0;
for (int i = 0; i < Iterations; i++)
{
last = _readBuffer.GetLong();
}
return last;
}
}
}
2 changes: 2 additions & 0 deletions HdrHistogram.Benchmarking/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace HdrHistogram.Benchmarking
{
class Program

Check warning on line 9 in HdrHistogram.Benchmarking/Program.cs

View workflow job for this annotation

GitHub Actions / build

Type 'Program' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
{
static void Main(string[] args)
{
Expand All @@ -24,6 +24,8 @@
typeof(LeadingZeroCount.LeadingZeroCount64BitBenchmark),
typeof(LeadingZeroCount.LeadingZeroCount32BitBenchmark),
typeof(Recording.Recording32BitBenchmark),
typeof(ByteBuffer.ByteBufferBenchmark),
typeof(Serialisation.SerialisationBenchmark),
});
switcher.Run(args, config);
}
Expand Down
61 changes: 61 additions & 0 deletions HdrHistogram.Benchmarking/Serialisation/SerialisationBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using BenchmarkDotNet.Attributes;
using HdrHistogram.Encoding;

namespace HdrHistogram.Benchmarking.Serialisation
{
[MemoryDiagnoser]
public class SerialisationBenchmark
{
private LongHistogram _source = null!;
private Utilities.ByteBuffer _encodeBuffer = null!;
private Utilities.ByteBuffer _decodeBuffer = null!;
private Utilities.ByteBuffer _compressedDecodeBuffer = null!;

[GlobalSetup]
public void Setup()
{
_source = new LongHistogram(3600_000_000L, 3);
for (long i = 0; i < 10_000; i++)
{
_source.RecordValue(1000L * i);
}

var capacity = _source.GetNeededByteBufferCapacity();
_encodeBuffer = Utilities.ByteBuffer.Allocate(capacity);

// Pre-encode for uncompressed decode benchmark
_source.Encode(_decodeBuffer = Utilities.ByteBuffer.Allocate(capacity), HistogramEncoderV2.Instance);

// Pre-encode for compressed decode benchmark
_source.EncodeIntoCompressedByteBuffer(_compressedDecodeBuffer = Utilities.ByteBuffer.Allocate(capacity));
}

[Benchmark]
public int Encode()
{
_encodeBuffer.Position = 0;
return _source.Encode(_encodeBuffer, HistogramEncoderV2.Instance);
}

[Benchmark]
public HistogramBase Decode()
{
_decodeBuffer.Position = 0;
return HistogramEncoding.DecodeFromByteBuffer(_decodeBuffer, 0);
}

[Benchmark]
public int EncodeCompressed()
{
_encodeBuffer.Position = 0;
return _source.EncodeIntoCompressedByteBuffer(_encodeBuffer);
}

[Benchmark]
public HistogramBase DecodeCompressed()
{
_compressedDecodeBuffer.Position = 0;
return HistogramEncoding.DecodeFromCompressedByteBuffer(_compressedDecodeBuffer, 0);
}
}
}
99 changes: 99 additions & 0 deletions HdrHistogram.UnitTests/Utilities/ByteBufferTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using FluentAssertions;
using HdrHistogram.Utilities;
using Xunit;

Expand Down Expand Up @@ -69,4 +70,102 @@ public override void Flush() { }
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}
}

public class ByteBufferReadWriteTests
{
[Theory]
[InlineData(42)]
[InlineData(-1)]
[InlineData(int.MaxValue)]
[InlineData(int.MinValue)]
public void PutInt_and_GetInt_roundtrip(int value)
{
var buffer = ByteBuffer.Allocate(sizeof(int));
buffer.PutInt(value);
buffer.Position = 0;
var result = buffer.GetInt();
Assert.Equal(value, result);
Assert.Equal(sizeof(int), buffer.Position);
}

[Theory]
[InlineData(4, 12345)]
[InlineData(8, -99999)]
public void PutInt_at_index_and_GetInt_roundtrip(int index, int value)
{
var buffer = ByteBuffer.Allocate(index + sizeof(int));
buffer.Position = index;
int positionBefore = buffer.Position;
buffer.PutInt(index, value);
Assert.Equal(positionBefore, buffer.Position);
buffer.Position = index;
var result = buffer.GetInt();
Assert.Equal(value, result);
}

[Theory]
[InlineData(100L)]
[InlineData(-1L)]
[InlineData(long.MaxValue)]
[InlineData(long.MinValue)]
public void PutLong_and_GetLong_roundtrip(long value)
{
var buffer = ByteBuffer.Allocate(sizeof(long));
buffer.PutLong(value);
buffer.Position = 0;
var result = buffer.GetLong();
Assert.Equal(value, result);
Assert.Equal(sizeof(long), buffer.Position);
}

[Theory]
[InlineData(0.0)]
[InlineData(double.PositiveInfinity)]
[InlineData(3.14159265358979)]
public void PutDouble_and_GetDouble_roundtrip(double value)
{
var buffer = ByteBuffer.Allocate(sizeof(double));
buffer.PutDouble(value);
buffer.Position = 0;
var result = buffer.GetDouble();
Assert.Equal(BitConverter.DoubleToInt64Bits(value), BitConverter.DoubleToInt64Bits(result));
Assert.Equal(sizeof(double), buffer.Position);
}

[Fact]
public void PutDouble_and_GetDouble_roundtrip_NaN()
{
var buffer = ByteBuffer.Allocate(sizeof(double));
buffer.PutDouble(double.NaN);
buffer.Position = 0;
var result = buffer.GetDouble();
Assert.Equal(BitConverter.DoubleToInt64Bits(double.NaN), BitConverter.DoubleToInt64Bits(result));
}

[Theory]
[InlineData(new byte[] { 0x01, 0x00 }, (short)256)]
[InlineData(new byte[] { 0x00, 0x7F }, (short)127)]
public void GetShort_returns_big_endian_value(byte[] bytes, short expected)
{
var buffer = ByteBuffer.Allocate(bytes.Length);
Buffer.BlockCopy(bytes, 0, ByteBufferTestHelper.GetInternalBuffer(buffer), 0, bytes.Length);
buffer.Position = 0;
var result = buffer.GetShort();
Assert.Equal(expected, result);
buffer.Position.Should().Be(sizeof(short));
}
}

/// <summary>
/// Test helper to access internal buffer via reflection for test setup.
/// </summary>
internal static class ByteBufferTestHelper
{
public static byte[] GetInternalBuffer(ByteBuffer buffer)
{
var field = typeof(ByteBuffer).GetField("_internalBuffer",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
return (byte[])field!.GetValue(buffer)!;
}
}
}
4 changes: 4 additions & 0 deletions HdrHistogram/HdrHistogram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<None Include="../README.md" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.*" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0|AnyCPU'">
<DocumentationFile>bin\Release\net8.0\HdrHistogram.xml</DocumentationFile>
</PropertyGroup>
Expand Down
Loading
Loading