This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathHubProtocolBenchmark.cs
91 lines (80 loc) · 2.98 KB
/
HubProtocolBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.SignalR.Protocol;
namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
{
public class HubProtocolBenchmark
{
private IHubProtocol _hubProtocol;
private ReadOnlyMemory<byte> _binaryInput;
private TestBinder _binder;
private HubMessage _hubMessage;
[Params(Message.NoArguments, Message.FewArguments, Message.ManyArguments, Message.LargeArguments)]
public Message Input { get; set; }
[Params(Protocol.MsgPack, Protocol.Json)]
public Protocol HubProtocol { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
switch (HubProtocol)
{
case Protocol.MsgPack:
_hubProtocol = new MessagePackHubProtocol();
break;
case Protocol.Json:
_hubProtocol = new JsonHubProtocol();
break;
}
switch (Input)
{
case Message.NoArguments:
_hubMessage = new InvocationMessage("Target", Array.Empty<object>());
break;
case Message.FewArguments:
_hubMessage = new InvocationMessage("Target", new object[] { 1, "Foo", 2.0f });
break;
case Message.ManyArguments:
_hubMessage = new InvocationMessage("Target", new object[] { 1, "string", 2.0f, true, (byte)9, new byte[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L });
break;
case Message.LargeArguments:
_hubMessage = new InvocationMessage("Target", new object[] { new string('F', 10240), new byte[10240] });
break;
}
_binaryInput = _hubProtocol.GetMessageBytes(_hubMessage);
_binder = new TestBinder(_hubMessage);
}
[Benchmark]
public void ReadSingleMessage()
{
var data = new ReadOnlySequence<byte>(_binaryInput);
if (!_hubProtocol.TryParseMessage(ref data, _binder, out _))
{
throw new InvalidOperationException("Failed to read message");
}
}
[Benchmark]
public void WriteSingleMessage()
{
var bytes = _hubProtocol.GetMessageBytes(_hubMessage);
if (bytes.Length != _binaryInput.Length)
{
throw new InvalidOperationException("Failed to write message");
}
}
public enum Protocol
{
MsgPack = 0,
Json = 1
}
public enum Message
{
NoArguments = 0,
FewArguments = 1,
ManyArguments = 2,
LargeArguments = 3
}
}
}