From 79cfc73293e3c1d86af8937f3747e1ee1d015873 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 6 Apr 2020 18:24:13 +0200 Subject: [PATCH] refactor WrapperBechmark --- ...Benchmark.cs => ParseMessagesBenchmark.cs} | 72 +++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) rename csharp/src/Google.Protobuf.Benchmarks/{WrapperBenchmark.cs => ParseMessagesBenchmark.cs} (59%) diff --git a/csharp/src/Google.Protobuf.Benchmarks/WrapperBenchmark.cs b/csharp/src/Google.Protobuf.Benchmarks/ParseMessagesBenchmark.cs similarity index 59% rename from csharp/src/Google.Protobuf.Benchmarks/WrapperBenchmark.cs rename to csharp/src/Google.Protobuf.Benchmarks/ParseMessagesBenchmark.cs index ae17c1819a..02310aac7e 100644 --- a/csharp/src/Google.Protobuf.Benchmarks/WrapperBenchmark.cs +++ b/csharp/src/Google.Protobuf.Benchmarks/ParseMessagesBenchmark.cs @@ -34,37 +34,87 @@ using BenchmarkDotNet.Attributes; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Buffers; +using Google.Protobuf.WellKnownTypes; namespace Google.Protobuf.Benchmarks { /// - /// Benchmark that tests serialization/deserialization of wrapper fields. + /// Benchmark that tests parsing performance for various messages. /// [MemoryDiagnoser] - public class WrapperBenchmark + public class ParseMessagesBenchmark { + const int MaxMessages = 100; + byte[] manyWrapperFieldsData; byte[] manyPrimitiveFieldsData; + byte[] manyWrapperFieldsMultipleMessagesData; + byte[] manyPrimitiveFieldsMultipleMessagesData; + + byte[] emptyData = new byte[0]; + + public IEnumerable MessageCountValues => new[] { 10, 100 }; + [GlobalSetup] public void GlobalSetup() { manyWrapperFieldsData = CreateManyWrapperFieldsMessage().ToByteArray(); manyPrimitiveFieldsData = CreateManyPrimitiveFieldsMessage().ToByteArray(); + manyWrapperFieldsMultipleMessagesData = CreateBufferWithMultipleMessages(CreateManyWrapperFieldsMessage(), MaxMessages); + manyPrimitiveFieldsMultipleMessagesData = CreateBufferWithMultipleMessages(CreateManyPrimitiveFieldsMessage(), MaxMessages); } [Benchmark] - public ManyWrapperFieldsMessage ParseWrapperFields() + public ManyWrapperFieldsMessage ManyWrapperFieldsMessage_ParseFromByteArray() { return ManyWrapperFieldsMessage.Parser.ParseFrom(manyWrapperFieldsData); } [Benchmark] - public ManyPrimitiveFieldsMessage ParsePrimitiveFields() + public ManyPrimitiveFieldsMessage ManyPrimitiveFieldsMessage_ParseFromByteArray() { return ManyPrimitiveFieldsMessage.Parser.ParseFrom(manyPrimitiveFieldsData); } + [Benchmark] + public Empty EmptyMessage_ParseFromByteArray() + { + return Empty.Parser.ParseFrom(emptyData); + } + + [Benchmark] + [ArgumentsSource(nameof(MessageCountValues))] + public long ManyWrapperFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount) + { + long sum = 0; + var input = new CodedInputStream(manyWrapperFieldsMultipleMessagesData); + for (int i = 0; i < messageCount; i++) + { + var msg = new ManyWrapperFieldsMessage(); + input.ReadMessage(msg); + sum += msg.Int64Field19.Value; + } + return sum; + } + + [Benchmark] + [ArgumentsSource(nameof(MessageCountValues))] + public long ManyPrimitiveFieldsMessage_ParseDelimitedMessagesFromByteArray(int messageCount) + { + long sum = 0; + var input = new CodedInputStream(manyPrimitiveFieldsMultipleMessagesData); + for (int i = 0; i < messageCount; i++) + { + var msg = new ManyPrimitiveFieldsMessage(); + input.ReadMessage(msg); + sum += msg.Int64Field19; + } + return sum; + } + + private static ManyWrapperFieldsMessage CreateManyWrapperFieldsMessage() { // Example data match data of an internal benchmarks @@ -98,5 +148,17 @@ namespace Google.Protobuf.Benchmarks DoubleField50 = 2.45 }; } + + private static byte[] CreateBufferWithMultipleMessages(IMessage msg, int msgCount) + { + var ms = new MemoryStream(); + var cos = new CodedOutputStream(ms); + for (int i = 0; i < msgCount; i++) + { + cos.WriteMessage(msg); + } + cos.Flush(); + return ms.ToArray(); + } } -} +} \ No newline at end of file