Added examples, Added performance tests, Updated readme

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-11-03 16:51:37 +01:00
parent 3d259d363a
commit 9facb66ee8
44 changed files with 3295 additions and 32 deletions
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MessagePack" Version="3.1.12" />
<Compile Include="..\..\_PerfTesting.cs" Link="_PerfTesting.cs" />
</ItemGroup>
</Project>
+48
View File
@@ -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;
}
+111
View File
@@ -0,0 +1,111 @@
EnableExplicit
XIncludeFile "../../Includes/MessagePack.pbi"
XIncludeFile "../_PerfTesting.pbi"
XIncludeFile "_Workload.pbi"
; Writes and reads the shared workload, see `_Workload.pbi`.
; Each map's fields are written in their own writer, then copied in with `MsgPackWriteMapFrom`.
Global Dim Keys.s(#Perf_Entries - 1)
Global *Encoded
Procedure WriteMap(*Writer)
Protected *Fields = MsgPackCreate(128)
Protected i.i
If Not *Fields
PerfFail("Failed to create the fields writer !")
EndIf
For i = 0 To #Perf_Entries - 1
PerfCheckWrite(MsgPackWriteString(*Fields, Keys(i)))
PerfCheckWrite(MsgPackWriteInteger(*Fields, i))
Next
PerfCheckWrite(MsgPackWriteMapFrom(*Writer, *Fields))
MsgPackFree(*Fields, #True)
EndProcedure
; Returns the finalized buffer, which must be freed with `FreeMemory()`.
Procedure.i EncodeMaps()
Protected *Writer = MsgPackCreate(#Perf_EncodedSize)
Protected *Buffer
Protected i.i
If Not *Writer
PerfFail("Failed to create the writer !")
EndIf
For i = 1 To #Perf_Maps
WriteMap(*Writer)
Next
*Buffer = MsgPackFinalize(*Writer)
If Not *Buffer
PerfFail("Failed to finalize the writer !")
EndIf
ProcedureReturn *Buffer
EndProcedure
Procedure.q ScenarioWrite()
Protected *Buffer = EncodeMaps()
Protected Size.q = MemorySize(*Buffer)
FreeMemory(*Buffer)
ProcedureReturn Size
EndProcedure
Procedure.q ScenarioRead()
Protected *Reader = MsgPackWrapMsgBuffer(*Encoded, MemorySize(*Encoded))
Protected *Iterator
Protected Total.q
Protected i.i
If Not *Reader
PerfFail("Failed to wrap the buffer !")
EndIf
For i = 1 To #Perf_Maps
*Iterator = MsgPackExamine(*Reader)
If Not *Iterator
PerfFail("Failed to examine a map !")
EndIf
While MsgPackNextEntry(*Iterator)
Total + Len(MsgPackReadString(*Iterator))
Total + MsgPackReadInteger(*Iterator)
Wend
; `MsgPackNextEntry` also returns `#False` on errors, and failed reads break the next ones.
If MsgPackLastError(*Iterator) <> #MsgPack_Error_Success
PerfFail("A read failed !")
EndIf
MsgPackFinish(*Iterator)
Next
MsgPackFree(*Reader, #False)
ProcedureReturn Total
EndProcedure
OpenConsole()
Define i.i
For i = 0 To #Perf_Entries - 1
Keys(i) = "Key" + RSet(Str(i), 2, "0")
Next
*Encoded = EncodeMaps()
PerfRun("pb", "write", @ScenarioWrite(), #Perf_Maps)
PerfRun("pb", "read", @ScenarioRead(), #Perf_Maps)
FreeMemory(*Encoded)
End 0
+37
View File
@@ -0,0 +1,37 @@
# Writes and reads the workload shared with the PureBasic tests, see `_Workload.pbi`.
# The maps are packed from and unpacked to dicts, how they're usually handled in Python.
import sys
from pathlib import Path
import msgpack
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from _PerfTesting import run
MAPS = 100_000
MAP = {f"Key{i:02}": i for i in range(16)}
def write():
packer = msgpack.Packer(autoreset=False)
pack = packer.pack
for _ in range(MAPS):
pack(MAP)
return packer.bytes()
def read():
unpacker = msgpack.Unpacker(raw=False)
unpacker.feed(ENCODED)
unpack = unpacker.unpack
total = 0
for _ in range(MAPS):
for key, value in unpack().items():
total += len(key) + value
return total
ENCODED = write()
run("write", MAPS, lambda: len(write()))
run("read", MAPS, read)
+18
View File
@@ -0,0 +1,18 @@
$ErrorActionPreference = "Stop"
# Shows the error instead of closing the window right away.
trap {
Write-Host $_ -ForegroundColor Red
Pause
exit 1
}
. (Join-Path $PSScriptRoot "..\_PerfTesting.ps1")
# Requires Python with the packages from 'PerfTests\requirements.txt', and the .NET 10 SDK.
$Rows = @(Invoke-PureBasicPerfTests $PSScriptRoot)
$Rows += Invoke-PythonPerfTest (Join-Path $PSScriptRoot "PerfPython.py")
$Rows += Invoke-DotNetPerfTest (Join-Path $PSScriptRoot "DotNet")
Show-PerfResults $Rows $PSScriptRoot
Pause
+15
View File
@@ -0,0 +1,15 @@
; Workload shared by every implementation in this folder, keep `PerfPython.py` and `DotNet/Program.cs` in sync.
;
; `#Perf_Maps` top-level maps are written, then read, each holding `#Perf_Entries` entries:
; "Key00" to "Key15" (FixStr), mapped to 0 to 15 (FixInt), in that order.
; 16 entries make it a Map16, the smallest format code that fits.
; `NsPerOp` is the time per map.
;
; Write check: the encoded size.
; Read check: the sum of the keys' length and of the values.
#Perf_Maps = 100000
#Perf_Entries = 16
; 3 bytes of header, then 6 bytes per key and 1 per value.
#Perf_EncodedSize = #Perf_Maps * (3 + #Perf_Entries * 7)