Skip to content
Draft
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
1 change: 1 addition & 0 deletions LiteDB.Benchmarks/Benchmarks/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal static class Categories
internal const string QUERIES = nameof(QUERIES);
internal const string INSERTION = nameof(INSERTION);
internal const string DELETION = nameof(DELETION);
internal const string SERIALIZATION = nameof(SERIALIZATION);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using LiteDB.Benchmarks.Benchmarks;
using LiteDB.Engine;
using System;

namespace LiteDB.Benchmarks.Benchmarks.Serialization
{
[BenchmarkCategory(Constants.Categories.SERIALIZATION)]
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class BufferSerializationBenchmark
{
private BufferSlice _contiguousRead;
private BufferSlice[] _splitRead;
private BufferSlice _contiguousWrite;
private BufferSlice[] _splitWrite;

[Params(128, 4096)]
public int ValueCount { get; set; }

[GlobalSetup]
public void Setup()
{
var byteCount = ValueCount * sizeof(int);

var contiguous = new byte[byteCount];
using (var writer = new BufferWriter(contiguous))
{
for (var i = 0; i < ValueCount; i++)
{
writer.Write(i);
}
}

_contiguousRead = new BufferSlice(contiguous, 0, contiguous.Length);

var firstLength = Math.Max(1, byteCount / 2);
var secondLength = byteCount - firstLength;

if (secondLength == 0)
{
secondLength = 1;
firstLength = Math.Max(1, byteCount - secondLength);
}

var segmentA = new BufferSlice(new byte[firstLength], 0, firstLength);
var segmentB = new BufferSlice(new byte[secondLength], 0, secondLength);

using (var writer = new BufferWriter(new[] { segmentA, segmentB }))
{
for (var i = 0; i < ValueCount; i++)
{
writer.Write(i);
}
}

_splitRead = new[] { segmentA, segmentB };

_contiguousWrite = new BufferSlice(new byte[byteCount], 0, byteCount);
_splitWrite = new[]
{
new BufferSlice(new byte[firstLength], 0, firstLength),
new BufferSlice(new byte[secondLength], 0, secondLength)
};
}

[Benchmark]
public int ReadInt32Contiguous()
{
var sum = 0;

using (var reader = new BufferReader(_contiguousRead))
{
for (var i = 0; i < ValueCount; i++)
{
sum += reader.ReadInt32();
}
}

return sum;
}

[Benchmark]
public int ReadInt32Split()
{
var sum = 0;

using (var reader = new BufferReader(_splitRead))
{
for (var i = 0; i < ValueCount; i++)
{
sum += reader.ReadInt32();
}
}

return sum;
}

[Benchmark]
public int WriteInt32Contiguous()
{
using (var writer = new BufferWriter(_contiguousWrite))
{
for (var i = 0; i < ValueCount; i++)
{
writer.Write(i);
}

return writer.Position;
}
}

[Benchmark]
public int WriteInt32Split()
{
using (var writer = new BufferWriter(_splitWrite))
{
for (var i = 0; i < ValueCount; i++)
{
writer.Write(i);
}

return writer.Position;
}
}
}
}
Loading
Loading