Merge branch 'codex/parser-span-retention'

This commit is contained in:
Joseph Doherty
2026-03-13 10:11:42 -04:00
12 changed files with 594 additions and 81 deletions

View File

@@ -23,4 +23,8 @@
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\NATS.Server\NATS.Server.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,143 @@
using System.Buffers;
using System.Diagnostics;
using System.Text;
using NATS.Server.Protocol;
using Xunit.Abstractions;
namespace NATS.Server.Benchmark.Tests.Protocol;
public class ParserHotPathBenchmarks(ITestOutputHelper output)
{
[Fact]
[Trait("Category", "Benchmark")]
public void Parser_PING_Throughput()
{
var payload = "PING\r\n"u8.ToArray();
MeasureSingleChunk("Parser PING", payload, iterations: 500_000);
}
[Fact]
[Trait("Category", "Benchmark")]
public void Parser_PUB_Throughput()
{
var payload = "PUB bench.subject 16\r\n0123456789ABCDEF\r\n"u8.ToArray();
MeasureSingleChunk("Parser PUB", payload, iterations: 250_000);
}
[Fact]
[Trait("Category", "Benchmark")]
public void Parser_HPUB_Throughput()
{
var payload = "HPUB bench.subject 12 28\r\nNATS/1.0\r\n\r\n0123456789ABCDEF\r\n"u8.ToArray();
MeasureSingleChunk("Parser HPUB", payload, iterations: 200_000);
}
[Fact]
[Trait("Category", "Benchmark")]
public void Parser_PUB_SplitPayload_Throughput()
{
var firstChunk = "PUB bench.subject 16\r\n01234567"u8.ToArray();
var secondChunk = "89ABCDEF\r\n"u8.ToArray();
MeasureSplitPayload("Parser PUB split payload", firstChunk, secondChunk, iterations: 200_000);
}
private void MeasureSingleChunk(string name, byte[] commandBytes, int iterations)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var parser = new NatsParser();
var totalBytes = (long)commandBytes.Length * iterations;
var beforeAlloc = GC.GetAllocatedBytesForCurrentThread();
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
ReadOnlySequence<byte> buffer = new(commandBytes);
if (!parser.TryParseView(ref buffer, out var command))
throw new InvalidOperationException($"{name} did not produce a parsed command.");
if (command.Type is CommandType.Pub or CommandType.HPub)
{
var payload = command.GetPayloadMemory();
if (payload.IsEmpty)
throw new InvalidOperationException($"{name} produced an empty payload unexpectedly.");
}
}
stopwatch.Stop();
var allocatedBytes = GC.GetAllocatedBytesForCurrentThread() - beforeAlloc;
WriteResult(name, iterations, totalBytes, stopwatch.Elapsed, allocatedBytes);
}
private void MeasureSplitPayload(string name, byte[] firstChunkBytes, byte[] secondChunkBytes, int iterations)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var parser = new NatsParser();
var totalBytes = (long)(firstChunkBytes.Length + secondChunkBytes.Length) * iterations;
var beforeAlloc = GC.GetAllocatedBytesForCurrentThread();
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
ReadOnlySequence<byte> firstChunk = new(firstChunkBytes);
if (parser.TryParseView(ref firstChunk, out _))
throw new InvalidOperationException($"{name} should wait for the second payload chunk.");
ReadOnlySequence<byte> secondChunk = CreateSequence(firstChunk.First, secondChunkBytes);
if (!parser.TryParseView(ref secondChunk, out var command))
throw new InvalidOperationException($"{name} did not complete after the second payload chunk.");
if (command.GetPayloadMemory().Length != 16)
throw new InvalidOperationException($"{name} produced the wrong payload length.");
}
stopwatch.Stop();
var allocatedBytes = GC.GetAllocatedBytesForCurrentThread() - beforeAlloc;
WriteResult(name, iterations, totalBytes, stopwatch.Elapsed, allocatedBytes);
}
private void WriteResult(string name, int iterations, long totalBytes, TimeSpan elapsed, long allocatedBytes)
{
var operationsPerSecond = iterations / elapsed.TotalSeconds;
var megabytesPerSecond = totalBytes / elapsed.TotalSeconds / (1024.0 * 1024.0);
var bytesPerOperation = allocatedBytes / (double)iterations;
output.WriteLine($"=== {name} ===");
output.WriteLine($"Ops: {operationsPerSecond:N0} ops/s");
output.WriteLine($"Data: {megabytesPerSecond:F1} MB/s");
output.WriteLine($"Alloc: {bytesPerOperation:F1} B/op");
output.WriteLine($"Elapsed: {elapsed.TotalMilliseconds:F0} ms");
output.WriteLine("");
}
private static ReadOnlySequence<byte> CreateSequence(ReadOnlyMemory<byte> remainingBytes, byte[] secondChunk)
{
var first = new BufferSegment(remainingBytes);
var second = first.Append(secondChunk);
return new ReadOnlySequence<byte>(first, 0, second, second.Memory.Length);
}
private sealed class BufferSegment : ReadOnlySequenceSegment<byte>
{
public BufferSegment(ReadOnlyMemory<byte> memory)
{
Memory = memory;
}
public BufferSegment Append(ReadOnlyMemory<byte> memory)
{
var next = new BufferSegment(memory)
{
RunningIndex = RunningIndex + Memory.Length,
};
Next = next;
return next;
}
}
}

View File

@@ -61,6 +61,16 @@ public class ParserTests
Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldContain("verbose");
}
[Fact]
public async Task Parse_CONNECT_preserves_json_payload_bytes()
{
const string json = "{\"verbose\":false,\"echo\":true}";
var cmds = await ParseAsync($"CONNECT {json}\r\n");
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Connect);
Encoding.ASCII.GetString(cmds[0].Payload.Span).ShouldBe(json);
}
[Fact]
public async Task Parse_SUB_without_queue()
{
@@ -144,6 +154,31 @@ public class ParserTests
cmds[0].Payload.ToArray().ShouldBeEmpty();
}
[Fact]
public async Task Parse_split_PUB_payload_across_reads()
{
var pipe = new Pipe();
var parser = new NatsParser(maxPayload: NatsProtocol.MaxPayloadSize);
await pipe.Writer.WriteAsync(Encoding.ASCII.GetBytes("PUB foo 5\r\nHe"));
var first = await pipe.Reader.ReadAsync();
var firstBuffer = first.Buffer;
parser.TryParse(ref firstBuffer, out _).ShouldBeFalse();
pipe.Reader.AdvanceTo(firstBuffer.Start, firstBuffer.End);
await pipe.Writer.WriteAsync(Encoding.ASCII.GetBytes("llo\r\n"));
pipe.Writer.Complete();
var second = await pipe.Reader.ReadAsync();
var secondBuffer = second.Buffer;
parser.TryParse(ref secondBuffer, out var cmd).ShouldBeTrue();
cmd.Type.ShouldBe(CommandType.Pub);
cmd.Subject.ShouldBe("foo");
Encoding.ASCII.GetString(cmd.Payload.Span).ShouldBe("Hello");
pipe.Reader.AdvanceTo(secondBuffer.Start, secondBuffer.End);
}
[Fact]
public async Task Parse_case_insensitive()
{
@@ -173,6 +208,7 @@ public class ParserTests
var cmds = await ParseAsync("INFO {\"server_id\":\"test\"}\r\n");
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Info);
Encoding.ASCII.GetString(cmds[0].Payload.Span).ShouldBe("{\"server_id\":\"test\"}");
}
// Mirrors Go TestParsePubArg: verifies subject, optional reply, and payload size

View File

@@ -232,6 +232,35 @@ public class ClientProtocolGoParityTests
}
}
[Fact]
public async Task Split_pub_payload_is_delivered_across_client_reads()
{
var (server, port, cts) = await StartServerAsync();
try
{
using var sub = await ConnectAndPingAsync(port);
using var pub = await ConnectAndPingAsync(port);
await sub.SendAsync(Encoding.ASCII.GetBytes("SUB foo 1\r\nPING\r\n"));
await SocketTestHelper.ReadUntilAsync(sub, "PONG\r\n");
await pub.SendAsync(Encoding.ASCII.GetBytes("PUB foo 5\r\nHe"));
await Task.Delay(25);
await pub.SendAsync(Encoding.ASCII.GetBytes("llo\r\nPING\r\n"));
await SocketTestHelper.ReadUntilAsync(pub, "PONG\r\n");
await sub.SendAsync(Encoding.ASCII.GetBytes("PING\r\n"));
var response = await SocketTestHelper.ReadUntilAsync(sub, "PONG\r\n");
response.ShouldContain("MSG foo 1 5\r\nHello\r\n");
}
finally
{
await cts.CancelAsync();
server.Dispose();
}
}
// =========================================================================
// TestTraceMsg — client_test.go:1700
// Tests that trace message formatting truncates correctly.

View File

@@ -0,0 +1,155 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Reflection;
using System.Text;
using NATS.Server.Protocol;
namespace NATS.Server.Core.Tests.ProtocolParity;
public class ParserSpanRetentionTests
{
[Fact]
public void TryParseView_exposes_PUB_fields_as_byte_views()
{
var parser = new NatsParser();
ReadOnlySequence<byte> buffer = new(Encoding.ASCII.GetBytes("PUB foo reply 5\r\nHello\r\n"));
var parsed = TryParseView(parser, ref buffer, out var view);
parsed.ShouldBeTrue();
GetCommandType(view).ShouldBe(CommandType.Pub);
GetAscii(view, "Subject").ShouldBe("foo");
GetAscii(view, "ReplyTo").ShouldBe("reply");
GetAscii(view, "Payload").ShouldBe("Hello");
GetPropertyType(view, "Subject").ShouldNotBe(typeof(string));
GetPropertyType(view, "ReplyTo").ShouldNotBe(typeof(string));
}
[Fact]
public void TryParseView_exposes_HPUB_fields_as_byte_views()
{
const string header = "NATS/1.0\r\n\r\n";
const string payload = "Hello";
var total = header.Length + payload.Length;
var parser = new NatsParser();
ReadOnlySequence<byte> buffer = new(Encoding.ASCII.GetBytes(
$"HPUB foo reply {header.Length} {total}\r\n{header}{payload}\r\n"));
var parsed = TryParseView(parser, ref buffer, out var view);
parsed.ShouldBeTrue();
GetCommandType(view).ShouldBe(CommandType.HPub);
GetAscii(view, "Subject").ShouldBe("foo");
GetAscii(view, "ReplyTo").ShouldBe("reply");
GetAscii(view, "Payload").ShouldBe(header + payload);
GetInt(view, "HeaderSize").ShouldBe(header.Length);
GetPropertyType(view, "Payload").ShouldNotBe(typeof(byte[]));
}
[Fact]
public void TryParseView_exposes_CONNECT_payload_as_byte_view()
{
const string json = "{\"verbose\":false,\"echo\":true}";
var parser = new NatsParser();
ReadOnlySequence<byte> buffer = new(Encoding.ASCII.GetBytes($"CONNECT {json}\r\n"));
var parsed = TryParseView(parser, ref buffer, out var view);
parsed.ShouldBeTrue();
GetCommandType(view).ShouldBe(CommandType.Connect);
GetAscii(view, "Payload").ShouldBe(json);
GetPropertyType(view, "Payload").ShouldNotBe(typeof(byte[]));
}
[Fact]
public void TryParseView_exposes_INFO_payload_as_byte_view()
{
const string json = "{\"server_id\":\"test\"}";
var parser = new NatsParser();
ReadOnlySequence<byte> buffer = new(Encoding.ASCII.GetBytes($"INFO {json}\r\n"));
var parsed = TryParseView(parser, ref buffer, out var view);
parsed.ShouldBeTrue();
GetCommandType(view).ShouldBe(CommandType.Info);
GetAscii(view, "Payload").ShouldBe(json);
GetPropertyType(view, "Payload").ShouldNotBe(typeof(byte[]));
}
[Fact]
public async Task TryParseView_preserves_split_payload_state_across_reads()
{
var parser = new NatsParser();
var pipe = new Pipe();
await pipe.Writer.WriteAsync(Encoding.ASCII.GetBytes("PUB foo 5\r\nHe"));
var first = await pipe.Reader.ReadAsync();
var firstBuffer = first.Buffer;
TryParseView(parser, ref firstBuffer, out _).ShouldBeFalse();
pipe.Reader.AdvanceTo(firstBuffer.Start, firstBuffer.End);
await pipe.Writer.WriteAsync(Encoding.ASCII.GetBytes("llo\r\n"));
pipe.Writer.Complete();
var second = await pipe.Reader.ReadAsync();
var secondBuffer = second.Buffer;
TryParseView(parser, ref secondBuffer, out var view).ShouldBeTrue();
GetCommandType(view).ShouldBe(CommandType.Pub);
GetAscii(view, "Subject").ShouldBe("foo");
GetAscii(view, "Payload").ShouldBe("Hello");
pipe.Reader.AdvanceTo(secondBuffer.Start, secondBuffer.End);
}
private static bool TryParseView(NatsParser parser, ref ReadOnlySequence<byte> buffer, out object view)
{
var method = typeof(NatsParser).GetMethod(
"TryParseView",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
method.ShouldNotBeNull("NatsParser should expose a byte-first TryParseView API.");
object?[] args =
[
buffer,
null,
];
var parsed = (bool)method!.Invoke(parser, args)!;
buffer = (ReadOnlySequence<byte>)args[0]!;
view = args[1]!;
return parsed;
}
private static CommandType GetCommandType(object view) =>
(CommandType)GetRequiredProperty(view, "Type").GetValue(view)!;
private static int GetInt(object view, string propertyName) =>
(int)GetRequiredProperty(view, propertyName).GetValue(view)!;
private static Type GetPropertyType(object view, string propertyName) =>
GetRequiredProperty(view, propertyName).PropertyType;
private static string GetAscii(object view, string propertyName)
{
var property = GetRequiredProperty(view, propertyName);
var value = property.GetValue(view);
return value switch
{
ReadOnlyMemory<byte> memory => Encoding.ASCII.GetString(memory.Span),
ReadOnlySequence<byte> sequence => Encoding.ASCII.GetString(sequence.ToArray()),
byte[] bytes => Encoding.ASCII.GetString(bytes),
null => string.Empty,
_ => throw new InvalidOperationException(
$"Unsupported property type for {propertyName}: {property.PropertyType}"),
};
}
private static PropertyInfo GetRequiredProperty(object view, string propertyName)
{
var property = view.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
property.ShouldNotBeNull($"Expected property {propertyName} on {view.GetType().Name}.");
return property!;
}
}

View File

@@ -42,4 +42,15 @@ public class ProtocolParserSnippetGapParityTests
ex.Message.ShouldContain("Maximum control line exceeded");
ex.Message.ShouldContain("snip=");
}
[Fact]
public void Parse_invalid_payload_trailer_preserves_existing_error_message()
{
var parser = new NatsParser();
var input = Encoding.ASCII.GetBytes("PUB foo 5\r\nHelloXX");
ReadOnlySequence<byte> buffer = new(input);
var ex = Should.Throw<ProtocolViolationException>(() => parser.TryParse(ref buffer, out _));
ex.Message.ShouldBe("Expected \\r\\n after payload");
}
}