ParseMessageBenchmark: parsing from ReadOnlySequence

This commit is contained in:
Jan Tattermusch 2020-04-07 12:24:18 +02:00
parent 5f836193c7
commit 42eff9d640

View File

@ -65,18 +65,36 @@ namespace Google.Protobuf.Benchmarks
return manyWrapperFieldsTest.ParseFromByteArray(); return manyWrapperFieldsTest.ParseFromByteArray();
} }
[Benchmark]
public IMessage ManyWrapperFieldsMessage_ParseFromReadOnlySequence()
{
return manyWrapperFieldsTest.ParseFromReadOnlySequence();
}
[Benchmark] [Benchmark]
public IMessage ManyPrimitiveFieldsMessage_ParseFromByteArray() public IMessage ManyPrimitiveFieldsMessage_ParseFromByteArray()
{ {
return manyPrimitiveFieldsTest.ParseFromByteArray(); return manyPrimitiveFieldsTest.ParseFromByteArray();
} }
[Benchmark]
public IMessage ManyPrimitiveFieldsMessage_ParseFromReadOnlySequence()
{
return manyPrimitiveFieldsTest.ParseFromReadOnlySequence();
}
[Benchmark] [Benchmark]
public IMessage EmptyMessage_ParseFromByteArray() public IMessage EmptyMessage_ParseFromByteArray()
{ {
return emptyMessageTest.ParseFromByteArray(); return emptyMessageTest.ParseFromByteArray();
} }
[Benchmark]
public IMessage EmptyMessage_ParseFromReadOnlySequence()
{
return emptyMessageTest.ParseFromReadOnlySequence();
}
[Benchmark] [Benchmark]
[ArgumentsSource(nameof(MessageCountValues))] [ArgumentsSource(nameof(MessageCountValues))]
public void ManyWrapperFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount) public void ManyWrapperFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount)
@ -84,6 +102,13 @@ namespace Google.Protobuf.Benchmarks
manyWrapperFieldsTest.ParseDelimitedMessagesFromByteArray(messageCount); manyWrapperFieldsTest.ParseDelimitedMessagesFromByteArray(messageCount);
} }
[Benchmark]
[ArgumentsSource(nameof(MessageCountValues))]
public void ManyWrapperFieldsMessage_ParseDelimitedMessagesFromReadOnlySequence(int messageCount)
{
manyWrapperFieldsTest.ParseDelimitedMessagesFromReadOnlySequence(messageCount);
}
[Benchmark] [Benchmark]
[ArgumentsSource(nameof(MessageCountValues))] [ArgumentsSource(nameof(MessageCountValues))]
public void ManyPrimitiveFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount) public void ManyPrimitiveFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount)
@ -91,6 +116,13 @@ namespace Google.Protobuf.Benchmarks
manyPrimitiveFieldsTest.ParseDelimitedMessagesFromByteArray(messageCount); manyPrimitiveFieldsTest.ParseDelimitedMessagesFromByteArray(messageCount);
} }
[Benchmark]
[ArgumentsSource(nameof(MessageCountValues))]
public void ManyPrimitiveFieldsMessage_ParseDelimitedMessagesFromReadOnlySequence(int messageCount)
{
manyPrimitiveFieldsTest.ParseDelimitedMessagesFromReadOnlySequence(messageCount);
}
private static ManyWrapperFieldsMessage CreateManyWrapperFieldsMessage() private static ManyWrapperFieldsMessage CreateManyWrapperFieldsMessage()
{ {
// Example data match data of an internal benchmarks // Example data match data of an internal benchmarks
@ -133,6 +165,9 @@ namespace Google.Protobuf.Benchmarks
private readonly byte[] data; private readonly byte[] data;
private readonly byte[] multipleMessagesData; private readonly byte[] multipleMessagesData;
private ReadOnlySequence<byte> dataSequence;
private ReadOnlySequence<byte> multipleMessagesDataSequence;
public SubTest(IMessage message, MessageParser parser, Func<IMessage> factory, int maxMessageCount) public SubTest(IMessage message, MessageParser parser, Func<IMessage> factory, int maxMessageCount)
{ {
this.message = message; this.message = message;
@ -140,10 +175,14 @@ namespace Google.Protobuf.Benchmarks
this.factory = factory; this.factory = factory;
this.data = message.ToByteArray(); this.data = message.ToByteArray();
this.multipleMessagesData = CreateBufferWithMultipleMessages(message, maxMessageCount); this.multipleMessagesData = CreateBufferWithMultipleMessages(message, maxMessageCount);
this.dataSequence = new ReadOnlySequence<byte>(this.data);
this.multipleMessagesDataSequence = new ReadOnlySequence<byte>(this.multipleMessagesData);
} }
public IMessage ParseFromByteArray() => parser.ParseFrom(data); public IMessage ParseFromByteArray() => parser.ParseFrom(data);
public IMessage ParseFromReadOnlySequence() => parser.ParseFrom(dataSequence);
public void ParseDelimitedMessagesFromByteArray(int messageCount) public void ParseDelimitedMessagesFromByteArray(int messageCount)
{ {
var input = new CodedInputStream(multipleMessagesData); var input = new CodedInputStream(multipleMessagesData);
@ -154,6 +193,16 @@ namespace Google.Protobuf.Benchmarks
} }
} }
public void ParseDelimitedMessagesFromReadOnlySequence(int messageCount)
{
var ctx = new ParseContext(multipleMessagesDataSequence);
for (int i = 0; i < messageCount; i++)
{
var msg = factory();
ctx.ReadMessage(msg);
}
}
private static byte[] CreateBufferWithMultipleMessages(IMessage msg, int msgCount) private static byte[] CreateBufferWithMultipleMessages(IMessage msg, int msgCount)
{ {
var ms = new MemoryStream(); var ms = new MemoryStream();