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 pathHubConnectionContextBenchmark.cs
99 lines (84 loc) · 3.58 KB
/
HubConnectionContextBenchmark.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
// 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 System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.SignalR.Internal;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.AspNetCore.SignalR.Microbenchmarks.Shared;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
{
public class HubConnectionContextBenchmark
{
private HubConnectionContext _hubConnectionContext;
private TestHubProtocolResolver _successHubProtocolResolver;
private TestHubProtocolResolver _failureHubProtocolResolver;
private TestUserIdProvider _userIdProvider;
private List<string> _supportedProtocols;
private TestDuplexPipe _pipe;
private ReadResult _handshakeRequestResult;
[GlobalSetup]
public void GlobalSetup()
{
var memoryBufferWriter = MemoryBufferWriter.Get();
try
{
HandshakeProtocol.WriteRequestMessage(new HandshakeRequestMessage("json", 1), memoryBufferWriter);
_handshakeRequestResult = new ReadResult(new ReadOnlySequence<byte>(memoryBufferWriter.ToArray()), false, false);
}
finally
{
MemoryBufferWriter.Return(memoryBufferWriter);
}
_pipe = new TestDuplexPipe();
var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), _pipe, _pipe);
_hubConnectionContext = new HubConnectionContext(connection, Timeout.InfiniteTimeSpan, NullLoggerFactory.Instance);
_successHubProtocolResolver = new TestHubProtocolResolver(new JsonHubProtocol());
_failureHubProtocolResolver = new TestHubProtocolResolver(null);
_userIdProvider = new TestUserIdProvider();
_supportedProtocols = new List<string> { "json" };
}
[Benchmark]
public async Task SuccessHandshakeAsync()
{
_pipe.AddReadResult(new ValueTask<ReadResult>(_handshakeRequestResult));
await _hubConnectionContext.HandshakeAsync(TimeSpan.FromSeconds(5), _supportedProtocols, _successHubProtocolResolver,
_userIdProvider, enableDetailedErrors: true);
}
[Benchmark]
public async Task ErrorHandshakeAsync()
{
_pipe.AddReadResult(new ValueTask<ReadResult>(_handshakeRequestResult));
await _hubConnectionContext.HandshakeAsync(TimeSpan.FromSeconds(5), _supportedProtocols, _failureHubProtocolResolver,
_userIdProvider, enableDetailedErrors: true);
}
}
public class TestUserIdProvider : IUserIdProvider
{
public string GetUserId(HubConnectionContext connection)
{
return "UserId!";
}
}
public class TestHubProtocolResolver : IHubProtocolResolver
{
private readonly IHubProtocol _instance;
public IReadOnlyList<IHubProtocol> AllProtocols { get; }
public TestHubProtocolResolver(IHubProtocol instance)
{
AllProtocols = new[] { instance };
_instance = instance;
}
public IHubProtocol GetProtocol(string protocolName, IReadOnlyList<string> supportedProtocols)
{
return _instance;
}
}
}