4
4
using System ;
5
5
using System . Buffers ;
6
6
using System . IO . Pipelines ;
7
+ using System . Runtime . CompilerServices ;
8
+ using System . Threading ;
9
+ using System . Threading . Tasks ;
7
10
using BenchmarkDotNet . Attributes ;
8
11
using Microsoft . AspNetCore . Http . Features ;
9
12
using Microsoft . AspNetCore . Server . Kestrel . Core ;
@@ -14,65 +17,63 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Performance
14
17
{
15
18
public class HttpProtocolFeatureCollection
16
19
{
17
- private readonly Http1Connection _http1Connection ;
18
- private IFeatureCollection _collection ;
19
-
20
- [ Benchmark ( Baseline = true ) ]
21
- public IHttpRequestFeature GetFirstViaFastFeature ( )
22
- {
23
- return ( IHttpRequestFeature ) GetFastFeature ( typeof ( IHttpRequestFeature ) ) ;
24
- }
25
-
26
- [ Benchmark ]
27
- public IHttpRequestFeature GetFirstViaType ( )
28
- {
29
- return ( IHttpRequestFeature ) Get ( typeof ( IHttpRequestFeature ) ) ;
30
- }
20
+ private readonly IFeatureCollection _collection ;
31
21
32
22
[ Benchmark ]
33
- public IHttpRequestFeature GetFirstViaExtension ( )
23
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
24
+ public IHttpRequestFeature GetViaTypeOf_First ( )
34
25
{
35
- return _collection . GetType < IHttpRequestFeature > ( ) ;
26
+ return ( IHttpRequestFeature ) _collection [ typeof ( IHttpRequestFeature ) ] ;
36
27
}
37
28
38
29
[ Benchmark ]
39
- public IHttpRequestFeature GetFirstViaGeneric ( )
30
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
31
+ public IHttpRequestFeature GetViaGeneric_First ( )
40
32
{
41
33
return _collection . Get < IHttpRequestFeature > ( ) ;
42
34
}
43
35
44
36
[ Benchmark ]
45
- public IHttpSendFileFeature GetLastViaFastFeature ( )
37
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
38
+ public IHttpSendFileFeature GetViaTypeOf_Last ( )
46
39
{
47
- return ( IHttpSendFileFeature ) GetFastFeature ( typeof ( IHttpSendFileFeature ) ) ;
40
+ return ( IHttpSendFileFeature ) _collection [ typeof ( IHttpSendFileFeature ) ] ;
48
41
}
49
42
50
43
[ Benchmark ]
51
- public IHttpSendFileFeature GetLastViaType ( )
44
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
45
+ public IHttpSendFileFeature GetViaGeneric_Last ( )
52
46
{
53
- return ( IHttpSendFileFeature ) Get ( typeof ( IHttpSendFileFeature ) ) ;
47
+ return _collection . Get < IHttpSendFileFeature > ( ) ;
54
48
}
55
49
56
50
[ Benchmark ]
57
- public IHttpSendFileFeature GetLastViaExtension ( )
51
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
52
+ public object GetViaTypeOf_Custom ( )
58
53
{
59
- return _collection . GetType < IHttpSendFileFeature > ( ) ;
54
+ return ( IHttpCustomFeature ) _collection [ typeof ( IHttpCustomFeature ) ] ;
60
55
}
61
56
62
57
[ Benchmark ]
63
- public IHttpSendFileFeature GetLastViaGeneric ( )
58
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
59
+ public object GetViaGeneric_Custom ( )
64
60
{
65
- return _collection . Get < IHttpSendFileFeature > ( ) ;
61
+ return _collection . Get < IHttpCustomFeature > ( ) ;
66
62
}
67
63
68
- private object Get ( Type type )
64
+
65
+ [ Benchmark ]
66
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
67
+ public object GetViaTypeOf_NotFound ( )
69
68
{
70
- return _collection [ type ] ;
69
+ return ( IHttpNotFoundFeature ) _collection [ typeof ( IHttpNotFoundFeature ) ] ;
71
70
}
72
71
73
- private object GetFastFeature ( Type type )
72
+ [ Benchmark ]
73
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
74
+ public object GetViaGeneric_NotFound ( )
74
75
{
75
- return _http1Connection . FastFeatureGet ( type ) ;
76
+ return _collection . Get < IHttpNotFoundFeature > ( ) ;
76
77
}
77
78
78
79
public HttpProtocolFeatureCollection ( )
@@ -99,21 +100,24 @@ public HttpProtocolFeatureCollection()
99
100
100
101
http1Connection . Reset ( ) ;
101
102
102
- _http1Connection = http1Connection ;
103
+ _collection = http1Connection ;
103
104
}
104
105
105
- [ IterationSetup ]
106
- public void Setup ( )
106
+ private class SendFileFeature : IHttpSendFileFeature
107
107
{
108
- _collection = _http1Connection ;
108
+ public Task SendFileAsync ( string path , long offset , long ? count , CancellationToken cancellation )
109
+ {
110
+ throw new NotImplementedException ( ) ;
111
+ }
109
112
}
110
113
111
- }
112
- public static class IFeatureCollectionExtensions
113
- {
114
- public static T GetType < T > ( this IFeatureCollection collection )
114
+ private interface IHttpCustomFeature
115
+ {
116
+ }
117
+
118
+ private interface IHttpNotFoundFeature
115
119
{
116
- return ( T ) collection [ typeof ( T ) ] ;
117
120
}
118
121
}
122
+
119
123
}
0 commit comments