-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathProgram.cs
More file actions
199 lines (167 loc) · 5.09 KB
/
Program.cs
File metadata and controls
199 lines (167 loc) · 5.09 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Running;
using EdjCase.JsonRpc.Router;
using EdjCase.JsonRpc.Router.Abstractions;
using EdjCase.JsonRpc.Router.Defaults;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Benchmarks
{
public class Program
{
public static void Main()
{
//var tester = new RequestMatcherTester();
//tester.GlobalSetup();
//tester.SimpleIterationSetup();
//Type type = typeof(Program);
//for (int i = 0; i < 100_000; i++)
//{
// tester.SimpleParamsNoReturn();
//}
_ = BenchmarkRunner.Run<RequestMatcherTester>();
}
#pragma warning disable CA1822 // Mark members as static
public void Test()
#pragma warning restore CA1822 // Mark members as static
{
}
}
[MValueColumn]
[MemoryDiagnoser]
[SimpleJob(RunStrategy.Throughput, invocationCount: 100_000)]
public class RequestMatcherTester
{
private IRpcRequestMatcher? requestMatcher;
[GlobalSetup]
public void GlobalSetup()
{
var logger = new FakeLogger<DefaultRequestMatcher>();
var methodProvider = new FakeMethodProvider();
var fakeRpcContextAccessor = new FakeRpcContextAccessor();
var options = Options.Create(new RpcServerConfiguration
{
JsonSerializerSettings = null
});
var rpcParameterConverter = new DefaultRpcParameterConverter(options, new FakeLogger<DefaultRpcParameterConverter>());
this.requestMatcher = new DefaultRequestMatcher(
logger,
methodProvider,
fakeRpcContextAccessor,
rpcParameterConverter,
new RequestMatcherCache(Options.Create(new RequestCacheOptions()))
);
}
private RpcRequestSignature? requestsignature;
[IterationSetup(Target = nameof(NoParamsNoReturn))]
public void IterationSetup()
{
this.requestsignature = RpcRequestSignature.Create(nameof(MethodClass.NoParamsNoReturn));
}
[Benchmark]
public void NoParamsNoReturn()
{
_ = this.requestMatcher!.GetMatchingMethod(this.requestsignature!);
}
[IterationSetup(Target = nameof(ComplexParamNoReturn))]
public void ComplexIterationSetup()
{
this.requestsignature = RpcRequestSignature.Create(nameof(MethodClass.ComplexParamNoReturn), new[] { RpcParameterType.Object });
}
[Benchmark]
public void ComplexParamNoReturn()
{
_ = this.requestMatcher!.GetMatchingMethod(this.requestsignature!);
}
[IterationSetup(Target = nameof(SimpleParamsNoReturn))]
public void SimpleIterationSetup()
{
var parameters = new Dictionary<string, RpcParameterType>
{
{"a", RpcParameterType.Number },
{"b", RpcParameterType.Boolean },
{"c", RpcParameterType.String }
};
this.requestsignature = RpcRequestSignature.Create(nameof(MethodClass.SimpleParamsNoReturn), parameters);
}
[Benchmark]
public void SimpleParamsNoReturn()
{
_ = this.requestMatcher!.GetMatchingMethod(this.requestsignature!);
}
#pragma warning disable IDE0060 // Remove unused parameter
internal class MethodClass
{
public void NoParamsNoReturn()
{
}
public void ComplexParamNoReturn(ComplexParam complex)
{
}
public void SimpleParamsNoReturn(int a, bool b, string c)
{
}
public class ComplexParam
{
public string? A { get; set; }
public bool B { get; set; }
public ComplexParam? C { get; set; }
}
}
#pragma warning restore IDE0060 // Remove unused parameter
}
public class FakeRpcContextAccessor : IRpcContextAccessor
{
public RpcContext Get()
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
return new RpcContext(null, "/api/v1/controller_name");
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}
public void Set(RpcContext context)
{
throw new NotImplementedException();
}
}
internal class FakeMethodProvider : IRpcMethodProvider
{
private static readonly List<DefaultRpcMethodInfo> methods = typeof(RequestMatcherTester.MethodClass)
.GetTypeInfo()
.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.DeclaringType != typeof(object))
.Select(DefaultRpcMethodInfo.FromMethodInfo)
.ToList();
public RpcRouteMetaData Get()
{
return new RpcRouteMetaData(FakeMethodProvider.methods, new Dictionary<RpcPath, IReadOnlyList<IRpcMethodInfo>>());
}
}
public class FakeLogger<T> : ILogger<T>
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return new FakeDisposable();
}
public bool IsEnabled(LogLevel logLevel)
{
return false;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
}
private class FakeDisposable : IDisposable
{
public void Dispose()
{
}
}
}
}