Skip to content

Commit 82bda4a

Browse files
authored
Added micro benchmarks for SSE parsing and Writing (#1818)
1 parent ca161e9 commit 82bda4a

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

benchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks/Microsoft.AspNetCore.SignalR.Microbenchmarks.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<Compile Include="..\..\src\Common\DuplexPipe.cs" Link="DuplexPipe.cs" />
10-
</ItemGroup>
11-
12-
<ItemGroup>
9+
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Http.Connections.Client\Microsoft.AspNetCore.Http.Connections.Client.csproj" />
10+
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Http.Connections\Microsoft.AspNetCore.Http.Connections.csproj" />
1311
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Core\Microsoft.AspNetCore.SignalR.Core.csproj" />
1412
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Common\Microsoft.AspNetCore.SignalR.Common.csproj" />
1513
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Client.Core\Microsoft.AspNetCore.SignalR.Client.Core.csproj" />
1614
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Protocols.MsgPack\Microsoft.AspNetCore.SignalR.Protocols.MsgPack.csproj" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
1718
<PackageReference Include="BenchmarkDotNet" Version="$(BenchmarkDotNetPackageVersion)" />
1819
<PackageReference Include="Microsoft.AspNetCore.BenchmarkRunner.Sources" Version="$(MicrosoftAspNetCoreBenchmarkRunnerSourcesPackageVersion)" />
1920
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsDependencyInjectionPackageVersion)" />
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Buffers;
3+
using System.IO;
4+
using BenchmarkDotNet.Attributes;
5+
using Microsoft.AspNetCore.Http.Connections.Client.Internal;
6+
using Microsoft.AspNetCore.Http.Connections.Internal;
7+
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
8+
9+
namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
10+
{
11+
public class ServerSentEventsBenchmark
12+
{
13+
private ServerSentEventsMessageParser _parser;
14+
private byte[] _sseFormattedData;
15+
private byte[] _rawData;
16+
17+
[Params(Message.NoArguments, Message.FewArguments, Message.ManyArguments, Message.LargeArguments)]
18+
public Message Input { get; set; }
19+
20+
[GlobalSetup]
21+
public void GlobalSetup()
22+
{
23+
var hubProtocol = new JsonHubProtocol();
24+
HubMessage hubMessage = null;
25+
switch (Input)
26+
{
27+
case Message.NoArguments:
28+
hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null);
29+
break;
30+
case Message.FewArguments:
31+
hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "Foo", 2.0f);
32+
break;
33+
case Message.ManyArguments:
34+
hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "string", 2.0f, true, (byte)9, new int[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L);
35+
break;
36+
case Message.LargeArguments:
37+
hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, new string('F', 10240), new string('B', 10240));
38+
break;
39+
}
40+
41+
_parser = new ServerSentEventsMessageParser();
42+
_rawData = hubProtocol.WriteToArray(hubMessage);
43+
var ms = new MemoryStream();
44+
ServerSentEventsMessageFormatter.WriteMessage(_rawData, ms);
45+
_sseFormattedData = ms.ToArray();
46+
}
47+
48+
[Benchmark]
49+
public void ReadSingleMessage()
50+
{
51+
var buffer = new ReadOnlySequence<byte>(_sseFormattedData);
52+
53+
if (_parser.ParseMessage(buffer, out _, out _, out _) != ServerSentEventsMessageParser.ParseResult.Completed)
54+
{
55+
throw new InvalidOperationException("Parse failed!");
56+
}
57+
58+
_parser.Reset();
59+
}
60+
61+
[Benchmark]
62+
public void WriteSingleMessage()
63+
{
64+
ServerSentEventsMessageFormatter.WriteMessage(_rawData, Stream.Null);
65+
}
66+
67+
public enum Message
68+
{
69+
NoArguments = 0,
70+
FewArguments = 1,
71+
ManyArguments = 2,
72+
LargeArguments = 3
73+
}
74+
}
75+
}

src/Microsoft.AspNetCore.Http.Connections.Client/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
using System.Runtime.CompilerServices;
55

66
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.SignalR.Client.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
7+
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.SignalR.Microbenchmarks, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

0 commit comments

Comments
 (0)