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 pathRedisProtocolBenchmark.cs
156 lines (129 loc) · 4.96 KB
/
RedisProtocolBenchmark.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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 System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.AspNetCore.SignalR.StackExchangeRedis.Internal;
namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
{
public class RedisProtocolBenchmark
{
private RedisProtocol _protocol;
private RedisGroupCommand _groupCommand;
private object[] _args;
private string _methodName;
private IReadOnlyList<string> _excludedConnectionIdsSmall;
private IReadOnlyList<string> _excludedConnectionIdsLarge;
private byte[] _writtenAck;
private byte[] _writtenGroupCommand;
private byte[] _writtenInvocationNoExclusions;
private byte[] _writtenInvocationSmallExclusions;
private byte[] _writtenInvocationLargeExclusions;
[GlobalSetup]
public void GlobalSetup()
{
_protocol = new RedisProtocol(new [] {
new DummyProtocol("protocol1"),
new DummyProtocol("protocol2")
});
_groupCommand = new RedisGroupCommand(id: 42, serverName: "Server", GroupAction.Add, groupName: "group", connectionId: "connection");
// Because of the DummyProtocol, the args don't really matter
_args = Array.Empty<object>();
_methodName = "Method";
_excludedConnectionIdsSmall = GenerateIds(2);
_excludedConnectionIdsLarge = GenerateIds(20);
_writtenAck = _protocol.WriteAck(42);
_writtenGroupCommand = _protocol.WriteGroupCommand(_groupCommand);
_writtenInvocationNoExclusions = _protocol.WriteInvocation(_methodName, _args, null);
_writtenInvocationSmallExclusions = _protocol.WriteInvocation(_methodName, _args, _excludedConnectionIdsSmall);
_writtenInvocationLargeExclusions = _protocol.WriteInvocation(_methodName, _args, _excludedConnectionIdsLarge);
}
[Benchmark]
public void WriteAck()
{
_protocol.WriteAck(42);
}
[Benchmark]
public void WriteGroupCommand()
{
_protocol.WriteGroupCommand(_groupCommand);
}
[Benchmark]
public void WriteInvocationNoExclusions()
{
_protocol.WriteInvocation(_methodName, _args);
}
[Benchmark]
public void WriteInvocationSmallExclusions()
{
_protocol.WriteInvocation(_methodName, _args, _excludedConnectionIdsSmall);
}
[Benchmark]
public void WriteInvocationLargeExclusions()
{
_protocol.WriteInvocation(_methodName, _args, _excludedConnectionIdsLarge);
}
[Benchmark]
public void ReadAck()
{
_protocol.ReadAck(_writtenAck);
}
[Benchmark]
public void ReadGroupCommand()
{
_protocol.ReadGroupCommand(_writtenGroupCommand);
}
[Benchmark]
public void ReadInvocationNoExclusions()
{
_protocol.ReadInvocation(_writtenInvocationNoExclusions);
}
[Benchmark]
public void ReadInvocationSmallExclusions()
{
_protocol.ReadInvocation(_writtenInvocationSmallExclusions);
}
[Benchmark]
public void ReadInvocationLargeExclusions()
{
_protocol.ReadInvocation(_writtenInvocationLargeExclusions);
}
private static IReadOnlyList<string> GenerateIds(int count)
{
var ids = new string[count];
for(var i = 0; i < count; i++)
{
ids[i] = Guid.NewGuid().ToString("N");
}
return ids;
}
private class DummyProtocol: IHubProtocol
{
private static readonly byte[] _fixedOutput = new byte[] { 0x68, 0x68, 0x6C, 0x6C, 0x6F };
public string Name { get; }
public int Version => 1;
public int MinorVersion => 0;
public TransferFormat TransferFormat => TransferFormat.Text;
public DummyProtocol(string name)
{
Name = name;
}
public bool IsVersionSupported(int version) => true;
public bool TryParseMessage(ref ReadOnlySequence<byte> input, IInvocationBinder binder, out HubMessage message)
{
throw new NotSupportedException();
}
public void WriteMessage(HubMessage message, IBufferWriter<byte> output)
{
output.Write(_fixedOutput);
}
public ReadOnlyMemory<byte> GetMessageBytes(HubMessage message)
{
return HubProtocolExtensions.GetMessageBytes(this, message);
}
}
}
}