mirror of
https://github.com/aziascreations/PB-MessagePack.git
synced 2026-11-24 18:28:44 +01:00
Added examples, Added performance tests, Updated readme
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// Writes and reads the workload shared with the PureBasic tests, see `../_Workload.pbi`.
|
||||
// Uses the low-level MessagePackWriter/MessagePackReader from MessagePack-CSharp, pinned in the .csproj.
|
||||
using System.Buffers;
|
||||
using MessagePack;
|
||||
|
||||
const int Maps = 100_000;
|
||||
const int Entries = 16;
|
||||
|
||||
string[] keys = Enumerable.Range(0, Entries).Select(i => $"Key{i:D2}").ToArray();
|
||||
byte[] encoded = Write(keys);
|
||||
|
||||
Perf.Run("write", Maps, () => Write(keys).Length);
|
||||
Perf.Run("read", Maps, () => Read(encoded));
|
||||
return 0;
|
||||
|
||||
|
||||
static byte[] Write(string[] keys)
|
||||
{
|
||||
var buffer = new ArrayBufferWriter<byte>();
|
||||
var writer = new MessagePackWriter(buffer);
|
||||
for (int i = 0; i < Maps; i++)
|
||||
{
|
||||
writer.WriteMapHeader(Entries);
|
||||
for (int j = 0; j < Entries; j++)
|
||||
{
|
||||
writer.Write(keys[j]);
|
||||
writer.Write(j);
|
||||
}
|
||||
}
|
||||
writer.Flush();
|
||||
return buffer.WrittenSpan.ToArray();
|
||||
}
|
||||
|
||||
static long Read(byte[] encoded)
|
||||
{
|
||||
var reader = new MessagePackReader(encoded);
|
||||
long total = 0;
|
||||
for (int i = 0; i < Maps; i++)
|
||||
{
|
||||
int count = reader.ReadMapHeader();
|
||||
for (int j = 0; j < count; j++)
|
||||
{
|
||||
total += reader.ReadString()!.Length;
|
||||
total += reader.ReadInt64();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
Reference in New Issue
Block a user