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>
@@ -0,0 +1,62 @@
// Writes, reads and skips 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 Records = 125_000;
const int Fields = Records * 8;
byte[] encoded = Write();
Perf.Run("write", Fields, () => Write().Length);
Perf.Run("read", Fields, () => Read(encoded));
Perf.Run("skip", Fields, () => Skip(encoded));
return 0;
static byte[] Write()
{
var buffer = new ArrayBufferWriter<byte>();
var writer = new MessagePackWriter(buffer);
for (int i = 0; i < Records; i++)
{
writer.Write(42);
writer.Write(-5);
writer.Write(200);
writer.Write(-1000);
writer.Write(70000);
writer.Write(-1099511627776L);
writer.Write(3.14);
writer.Write("Hello");
}
writer.Flush();
return buffer.WrittenSpan.ToArray();
}
static long Read(byte[] data)
{
var reader = new MessagePackReader(data);
long total = 0;
for (int i = 0; i < Records; i++)
{
total += reader.ReadInt64();
total += reader.ReadInt64();
total += reader.ReadInt64();
total += reader.ReadInt64();
total += reader.ReadInt64();
total += reader.ReadInt64();
total += (long)reader.ReadDouble();
total += reader.ReadString()!.Length;
}
return total;
}
static long Skip(byte[] data)
{
var reader = new MessagePackReader(data);
for (int i = 0; i < Fields; i++)
{
reader.Skip();
}
return Fields;
}
+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`, forcing each field's format code.
Global *Encoded
Procedure WriteRecords(*Writer)
Protected i.i
For i = 1 To #Perf_Records
PerfCheckWrite(MsgPackWriteFixInt(*Writer, 42))
PerfCheckWrite(MsgPackWriteFixInt(*Writer, -5))
PerfCheckWrite(MsgPackWriteUInt8(*Writer, 200))
PerfCheckWrite(MsgPackWriteInt16(*Writer, -1000))
PerfCheckWrite(MsgPackWriteUInt32(*Writer, 70000))
PerfCheckWrite(MsgPackWriteInt64(*Writer, -1099511627776))
PerfCheckWrite(MsgPackWriteFloat64(*Writer, 3.14))
PerfCheckWrite(MsgPackWriteFixStr(*Writer, "Hello"))
Next
EndProcedure
; Returns the finalized buffer, which must be freed with `FreeMemory()`.
Procedure.i Encode(InitialSize.i, GrowthIncrement.i)
Protected *Writer = MsgPackCreate(InitialSize, GrowthIncrement)
Protected *Buffer
If Not *Writer
PerfFail("Failed to create the writer !")
EndIf
WriteRecords(*Writer)
*Buffer = MsgPackFinalize(*Writer)
If Not *Buffer
PerfFail("Failed to finalize the writer !")
EndIf
ProcedureReturn *Buffer
EndProcedure
Procedure.q EncodeAndFree(InitialSize.i, GrowthIncrement.i)
Protected *Buffer = Encode(InitialSize, GrowthIncrement)
Protected Size.q = MemorySize(*Buffer)
FreeMemory(*Buffer)
ProcedureReturn Size
EndProcedure
Procedure.q ScenarioWrite()
ProcedureReturn EncodeAndFree(64, 1)
EndProcedure
Procedure.q ScenarioWriteGrow64K()
ProcedureReturn EncodeAndFree(64, 65536)
EndProcedure
Procedure.q ScenarioWritePrealloc()
ProcedureReturn EncodeAndFree(#Perf_EncodedSize, 1)
EndProcedure
Procedure.q ScenarioRead()
Protected *Reader = MsgPackWrapMsgBuffer(*Encoded, MemorySize(*Encoded))
Protected Total.q
Protected i.i
If Not *Reader
PerfFail("Failed to wrap the buffer !")
EndIf
For i = 1 To #Perf_Records
; Fixints don't have an explicitly typed getter.
Total + MsgPackReadInteger(*Reader)
Total + MsgPackReadInteger(*Reader)
Total + MsgPackReadUInt8(*Reader)
Total + MsgPackReadInt16(*Reader)
Total + (MsgPackReadUInt32(*Reader) & $FFFFFFFF)
Total + MsgPackReadInt64(*Reader)
Total + Int(MsgPackReadFloat64(*Reader))
Total + Len(MsgPackReadString(*Reader))
Next
; Failed reads don't move the offset, so a failure breaks the next reads too.
If MsgPackLastError(*Reader) <> #MsgPack_Error_Success
PerfFail("A read failed !")
EndIf
MsgPackFree(*Reader, #False)
ProcedureReturn Total
EndProcedure
OpenConsole()
*Encoded = Encode(#Perf_EncodedSize, 1)
PerfRun("pb-explicit", "read", @ScenarioRead(), #Perf_Fields)
PerfRun("pb-explicit", "write-prealloc", @ScenarioWritePrealloc(), #Perf_Fields)
PerfRun("pb-explicit", "write-grow64k", @ScenarioWriteGrow64K(), #Perf_Fields)
; Last since it reallocates the buffer on every write, which can be very slow.
PerfRun("pb-explicit", "write", @ScenarioWrite(), #Perf_Fields)
FreeMemory(*Encoded)
End 0
+132
View File
@@ -0,0 +1,132 @@
EnableExplicit
XIncludeFile "../../Includes/MessagePack.pbi"
XIncludeFile "../_PerfTesting.pbi"
XIncludeFile "_Workload.pbi"
; Writes and reads the shared workload, see `_Workload.pbi`, letting the library pick each field's format code.
Global *Encoded
Procedure WriteRecords(*Writer)
Protected i.i
For i = 1 To #Perf_Records
PerfCheckWrite(MsgPackWriteInteger(*Writer, 42))
PerfCheckWrite(MsgPackWriteInteger(*Writer, -5))
PerfCheckWrite(MsgPackWriteInteger(*Writer, 200))
PerfCheckWrite(MsgPackWriteInteger(*Writer, -1000))
PerfCheckWrite(MsgPackWriteInteger(*Writer, 70000))
PerfCheckWrite(MsgPackWriteInteger(*Writer, -1099511627776))
; Floats don't have an implicitly typed setter.
PerfCheckWrite(MsgPackWriteFloat64(*Writer, 3.14))
PerfCheckWrite(MsgPackWriteString(*Writer, "Hello"))
Next
EndProcedure
; Returns the finalized buffer, which must be freed with `FreeMemory()`.
Procedure.i Encode(InitialSize.i, GrowthIncrement.i)
Protected *Writer = MsgPackCreate(InitialSize, GrowthIncrement)
Protected *Buffer
If Not *Writer
PerfFail("Failed to create the writer !")
EndIf
WriteRecords(*Writer)
*Buffer = MsgPackFinalize(*Writer)
If Not *Buffer
PerfFail("Failed to finalize the writer !")
EndIf
ProcedureReturn *Buffer
EndProcedure
Procedure.q EncodeAndFree(InitialSize.i, GrowthIncrement.i)
Protected *Buffer = Encode(InitialSize, GrowthIncrement)
Protected Size.q = MemorySize(*Buffer)
FreeMemory(*Buffer)
ProcedureReturn Size
EndProcedure
Procedure.q ScenarioWrite()
ProcedureReturn EncodeAndFree(64, 1)
EndProcedure
Procedure.q ScenarioWriteGrow64K()
ProcedureReturn EncodeAndFree(64, 65536)
EndProcedure
Procedure.q ScenarioWritePrealloc()
ProcedureReturn EncodeAndFree(#Perf_EncodedSize, 1)
EndProcedure
Procedure.q ScenarioRead()
Protected *Reader = MsgPackWrapMsgBuffer(*Encoded, MemorySize(*Encoded))
Protected Total.q
Protected i.i
If Not *Reader
PerfFail("Failed to wrap the buffer !")
EndIf
For i = 1 To #Perf_Records
Total + MsgPackReadInteger(*Reader)
Total + MsgPackReadInteger(*Reader)
Total + MsgPackReadInteger(*Reader)
Total + MsgPackReadInteger(*Reader)
Total + MsgPackReadInteger(*Reader)
Total + MsgPackReadInteger(*Reader)
Total + Int(MsgPackReadFloat(*Reader))
Total + Len(MsgPackReadString(*Reader))
Next
; Failed reads don't move the offset, so a failure breaks the next reads too.
If MsgPackLastError(*Reader) <> #MsgPack_Error_Success
PerfFail("A read failed !")
EndIf
MsgPackFree(*Reader, #False)
ProcedureReturn Total
EndProcedure
; Doesn't depend on the typing, so it's only measured here.
; Skips each field separately, like the Python and .NET implementations.
Procedure.q ScenarioSkip()
Protected *Reader = MsgPackWrapMsgBuffer(*Encoded, MemorySize(*Encoded))
Protected i.i
If Not *Reader
PerfFail("Failed to wrap the buffer !")
EndIf
For i = 1 To #Perf_Fields
If MsgPackSkip(*Reader) <> #MsgPack_Error_Success
PerfFail("A skip failed !")
EndIf
Next
MsgPackFree(*Reader, #False)
ProcedureReturn #Perf_Fields
EndProcedure
OpenConsole()
*Encoded = Encode(#Perf_EncodedSize, 1)
PerfRun("pb-implicit", "read", @ScenarioRead(), #Perf_Fields)
PerfRun("pb-implicit", "skip", @ScenarioSkip(), #Perf_Fields)
PerfRun("pb-implicit", "write-prealloc", @ScenarioWritePrealloc(), #Perf_Fields)
PerfRun("pb-implicit", "write-grow64k", @ScenarioWriteGrow64K(), #Perf_Fields)
; Last since it reallocates the buffer on every write, which can be very slow.
PerfRun("pb-implicit", "write", @ScenarioWrite(), #Perf_Fields)
FreeMemory(*Encoded)
End 0
+54
View File
@@ -0,0 +1,54 @@
# Writes, reads and skips the workload shared with the PureBasic tests, see `_Workload.pbi`.
import sys
from pathlib import Path
import msgpack
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from _PerfTesting import run
RECORDS = 125_000
FIELDS = RECORDS * 8
def write():
packer = msgpack.Packer(autoreset=False)
pack = packer.pack
for _ in range(RECORDS):
pack(42)
pack(-5)
pack(200)
pack(-1000)
pack(70000)
pack(-1099511627776)
pack(3.14)
pack("Hello")
return packer.bytes()
def read():
unpacker = msgpack.Unpacker(raw=False)
unpacker.feed(ENCODED)
unpack = unpacker.unpack
total = 0
for _ in range(RECORDS):
total += unpack() + unpack() + unpack() + unpack() + unpack() + unpack()
total += int(unpack())
total += len(unpack())
return total
def skip():
unpacker = msgpack.Unpacker(raw=False)
unpacker.feed(ENCODED)
skip_field = unpacker.skip
for _ in range(FIELDS):
skip_field()
return FIELDS
ENCODED = write()
run("write", FIELDS, lambda: len(write()))
run("read", FIELDS, read)
run("skip", FIELDS, skip)
@@ -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_Records` records of 8 fields, 1M fields in total, each record being:
; 42 (FixInt), -5 (FixInt), 200 (UInt8), -1000 (Int16), 70000 (UInt32),
; -1099511627776 (Int64), 3.14 (Float64), "Hello" (FixStr)
;
; Write check: the encoded size.
; Read check: the sum of the integers, of `Int()` of the float, and of the string's length.
; Skip check: the number of fields skipped.
#Perf_Records = 125000
#Perf_Fields = #Perf_Records * 8
; 1 + 1 + 2 + 3 + 5 + 9 + 9 + 6 bytes per record.
#Perf_EncodedSize = #Perf_Records * 36