From 82bda4a9c8dcb565ef94084ceae029c9ba1e88c6 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 1 Apr 2018 09:19:07 -0700 Subject: [PATCH] Added micro benchmarks for SSE parsing and Writing (#1818) --- ....AspNetCore.SignalR.Microbenchmarks.csproj | 9 ++- .../ServerSentEventsBenchmark.cs | 75 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 1 + 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/ServerSentEventsBenchmark.cs diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks.csproj b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks.csproj index a742b96718..4ff4eb83a8 100644 --- a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks.csproj +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks.csproj @@ -6,14 +6,15 @@ - - - - + + + + + diff --git a/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/ServerSentEventsBenchmark.cs b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/ServerSentEventsBenchmark.cs new file mode 100644 index 0000000000..d6bd719e27 --- /dev/null +++ b/benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/ServerSentEventsBenchmark.cs @@ -0,0 +1,75 @@ +using System; +using System.Buffers; +using System.IO; +using BenchmarkDotNet.Attributes; +using Microsoft.AspNetCore.Http.Connections.Client.Internal; +using Microsoft.AspNetCore.Http.Connections.Internal; +using Microsoft.AspNetCore.SignalR.Internal.Protocol; + +namespace Microsoft.AspNetCore.SignalR.Microbenchmarks +{ + public class ServerSentEventsBenchmark + { + private ServerSentEventsMessageParser _parser; + private byte[] _sseFormattedData; + private byte[] _rawData; + + [Params(Message.NoArguments, Message.FewArguments, Message.ManyArguments, Message.LargeArguments)] + public Message Input { get; set; } + + [GlobalSetup] + public void GlobalSetup() + { + var hubProtocol = new JsonHubProtocol(); + HubMessage hubMessage = null; + switch (Input) + { + case Message.NoArguments: + hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null); + break; + case Message.FewArguments: + hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "Foo", 2.0f); + break; + case Message.ManyArguments: + hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "string", 2.0f, true, (byte)9, new int[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L); + break; + case Message.LargeArguments: + hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, new string('F', 10240), new string('B', 10240)); + break; + } + + _parser = new ServerSentEventsMessageParser(); + _rawData = hubProtocol.WriteToArray(hubMessage); + var ms = new MemoryStream(); + ServerSentEventsMessageFormatter.WriteMessage(_rawData, ms); + _sseFormattedData = ms.ToArray(); + } + + [Benchmark] + public void ReadSingleMessage() + { + var buffer = new ReadOnlySequence(_sseFormattedData); + + if (_parser.ParseMessage(buffer, out _, out _, out _) != ServerSentEventsMessageParser.ParseResult.Completed) + { + throw new InvalidOperationException("Parse failed!"); + } + + _parser.Reset(); + } + + [Benchmark] + public void WriteSingleMessage() + { + ServerSentEventsMessageFormatter.WriteMessage(_rawData, Stream.Null); + } + + public enum Message + { + NoArguments = 0, + FewArguments = 1, + ManyArguments = 2, + LargeArguments = 3 + } + } +} diff --git a/src/Microsoft.AspNetCore.Http.Connections.Client/Properties/AssemblyInfo.cs b/src/Microsoft.AspNetCore.Http.Connections.Client/Properties/AssemblyInfo.cs index 8bc7094d90..53146da3b6 100644 --- a/src/Microsoft.AspNetCore.Http.Connections.Client/Properties/AssemblyInfo.cs +++ b/src/Microsoft.AspNetCore.Http.Connections.Client/Properties/AssemblyInfo.cs @@ -4,3 +4,4 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("Microsoft.AspNetCore.SignalR.Client.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] +[assembly: InternalsVisibleTo("Microsoft.AspNetCore.SignalR.Microbenchmarks, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] \ No newline at end of file