55using System . Buffers ;
66using System . IO . Pipelines ;
77using System . Runtime . CompilerServices ;
8- using System . Threading ;
9- using System . Threading . Tasks ;
8+
109using BenchmarkDotNet . Attributes ;
10+
1111using Microsoft . AspNetCore . Http . Features ;
12+ using Microsoft . AspNetCore . Http . Features . Authentication ;
1213using Microsoft . AspNetCore . Server . Kestrel . Core ;
13- using Microsoft . AspNetCore . Server . Kestrel . Core . Internal ;
14+ using Microsoft . AspNetCore . Server . Kestrel . Core . Features ;
1415using Microsoft . AspNetCore . Server . Kestrel . Core . Internal . Http ;
1516using Microsoft . AspNetCore . Testing ;
1617
@@ -20,45 +21,205 @@ public class HttpProtocolFeatureCollection
2021 {
2122 private readonly IFeatureCollection _collection ;
2223
23- [ Benchmark ]
24+ [ Benchmark ( Description = "Get<IHttpRequestFeature>*" ) ]
2425 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
25- public IHttpRequestFeature GetViaTypeOf_First ( )
26+ public IHttpRequestFeature Get_IHttpRequestFeature ( )
2627 {
27- return ( IHttpRequestFeature ) _collection [ typeof ( IHttpRequestFeature ) ] ;
28+ return _collection . Get < IHttpRequestFeature > ( ) ;
2829 }
2930
30- [ Benchmark ]
31+ [ Benchmark ( Description = "Get<IHttpResponseFeature>*" ) ]
3132 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
32- public IHttpRequestFeature GetViaGeneric_First ( )
33+ public IHttpResponseFeature Get_IHttpResponseFeature ( )
3334 {
34- return _collection . Get < IHttpRequestFeature > ( ) ;
35+ return _collection . Get < IHttpResponseFeature > ( ) ;
36+ }
37+
38+ [ Benchmark ( Description = "Get<IHttpResponseBodyFeature>*" ) ]
39+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
40+ public IHttpResponseBodyFeature Get_IHttpResponseBodyFeature ( )
41+ {
42+ return _collection . Get < IHttpResponseBodyFeature > ( ) ;
43+ }
44+
45+ [ Benchmark ( Description = "Get<IRouteValuesFeature>*" ) ]
46+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
47+ public IRouteValuesFeature Get_IRouteValuesFeature ( )
48+ {
49+ return _collection . Get < IRouteValuesFeature > ( ) ;
50+ }
51+
52+ [ Benchmark ( Description = "Get<IEndpointFeature>*" ) ]
53+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
54+ public IEndpointFeature Get_IEndpointFeature ( )
55+ {
56+ return _collection . Get < IEndpointFeature > ( ) ;
57+ }
58+
59+ [ Benchmark ( Description = "Get<IServiceProvidersFeature>" ) ]
60+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
61+ public IServiceProvidersFeature Get_IServiceProvidersFeature ( )
62+ {
63+ return _collection . Get < IServiceProvidersFeature > ( ) ;
64+ }
65+
66+ [ Benchmark ( Description = "Get<IItemsFeature>" ) ]
67+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
68+ public IItemsFeature Get_IItemsFeature ( )
69+ {
70+ return _collection . Get < IItemsFeature > ( ) ;
71+ }
72+
73+ [ Benchmark ( Description = "Get<IQueryFeature>" ) ]
74+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
75+ public IQueryFeature Get_IQueryFeature ( )
76+ {
77+ return _collection . Get < IQueryFeature > ( ) ;
78+ }
79+
80+ [ Benchmark ( Description = "Get<IRequestBodyPipeFeature>*" ) ]
81+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
82+ public IRequestBodyPipeFeature Get_IRequestBodyPipeFeature ( )
83+ {
84+ return _collection . Get < IRequestBodyPipeFeature > ( ) ;
85+ }
86+
87+ [ Benchmark ( Description = "Get<IFormFeature>" ) ]
88+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
89+ public IFormFeature Get_IFormFeature ( )
90+ {
91+ return _collection . Get < IFormFeature > ( ) ;
92+ }
93+
94+ [ Benchmark ( Description = "Get<IHttpAuthenticationFeature>" ) ]
95+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
96+ public IHttpAuthenticationFeature Get_IHttpAuthenticationFeature ( )
97+ {
98+ return _collection . Get < IHttpAuthenticationFeature > ( ) ;
99+ }
100+
101+ [ Benchmark ( Description = "Get<IHttpRequestIdentifierFeature>*" ) ]
102+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
103+ public IHttpRequestIdentifierFeature Get_IHttpRequestIdentifierFeature ( )
104+ {
105+ return _collection . Get < IHttpRequestIdentifierFeature > ( ) ;
106+ }
107+
108+ [ Benchmark ( Description = "Get<IHttpConnectionFeature>*" ) ]
109+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
110+ public IHttpConnectionFeature Get_IHttpConnectionFeature ( )
111+ {
112+ return _collection . Get < IHttpConnectionFeature > ( ) ;
113+ }
114+
115+ [ Benchmark ( Description = "Get<ISessionFeature>" ) ]
116+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
117+ public ISessionFeature Get_ISessionFeature ( )
118+ {
119+ return _collection . Get < ISessionFeature > ( ) ;
35120 }
36121
37- [ Benchmark ]
122+ [ Benchmark ( Description = "Get<IResponseCookiesFeature>" ) ]
38123 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
39- public object GetViaTypeOf_Custom ( )
124+ public IResponseCookiesFeature Get_IResponseCookiesFeature ( )
40125 {
41- return ( IHttpCustomFeature ) _collection [ typeof ( IHttpCustomFeature ) ] ;
126+ return _collection . Get < IResponseCookiesFeature > ( ) ;
42127 }
43128
44- [ Benchmark ]
129+ [ Benchmark ( Description = "Get<IHttpRequestTrailersFeature>*" ) ]
45130 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
46- public object GetViaGeneric_Custom ( )
131+ public IHttpRequestTrailersFeature Get_IHttpRequestTrailersFeature ( )
47132 {
48- return _collection . Get < IHttpCustomFeature > ( ) ;
133+ return _collection . Get < IHttpRequestTrailersFeature > ( ) ;
49134 }
50135
136+ [ Benchmark ( Description = "Get<IHttpResponseTrailersFeature>" ) ]
137+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
138+ public IHttpResponseTrailersFeature Get_IHttpResponseTrailersFeature ( )
139+ {
140+ return _collection . Get < IHttpResponseTrailersFeature > ( ) ;
141+ }
51142
52- [ Benchmark ]
143+ [ Benchmark ( Description = "Get<ITlsConnectionFeature>" ) ]
53144 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
54- public object GetViaTypeOf_NotFound ( )
145+ public ITlsConnectionFeature Get_ITlsConnectionFeature ( )
55146 {
56- return ( IHttpNotFoundFeature ) _collection [ typeof ( IHttpNotFoundFeature ) ] ;
147+ return _collection . Get < ITlsConnectionFeature > ( ) ;
57148 }
58149
59- [ Benchmark ]
150+ [ Benchmark ( Description = "Get<IHttpUpgradeFeature>*" ) ]
60151 [ MethodImpl ( MethodImplOptions . NoInlining ) ]
61- public object GetViaGeneric_NotFound ( )
152+ public IHttpUpgradeFeature Get_IHttpUpgradeFeature ( )
153+ {
154+ return _collection . Get < IHttpUpgradeFeature > ( ) ;
155+ }
156+
157+ [ Benchmark ( Description = "Get<IHttpWebSocketFeature>" ) ]
158+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
159+ public IHttpWebSocketFeature Get_IHttpWebSocketFeature ( )
160+ {
161+ return _collection . Get < IHttpWebSocketFeature > ( ) ;
162+ }
163+
164+ [ Benchmark ( Description = "Get<IHttp2StreamIdFeature>" ) ]
165+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
166+ public IHttp2StreamIdFeature Get_IHttp2StreamIdFeature ( )
167+ {
168+ return _collection . Get < IHttp2StreamIdFeature > ( ) ;
169+ }
170+
171+ [ Benchmark ( Description = "Get<IHttpRequestLifetimeFeature>*" ) ]
172+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
173+ public IHttpRequestLifetimeFeature Get_IHttpRequestLifetimeFeature ( )
174+ {
175+ return _collection . Get < IHttpRequestLifetimeFeature > ( ) ;
176+ }
177+
178+ [ Benchmark ( Description = "Get<IHttpMaxRequestBodySizeFeature>*" ) ]
179+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
180+ public IHttpMaxRequestBodySizeFeature Get_IHttpMaxRequestBodySizeFeature ( )
181+ {
182+ return _collection . Get < IHttpMaxRequestBodySizeFeature > ( ) ;
183+ }
184+
185+ [ Benchmark ( Description = "Get<IHttpMinRequestBodyDataRateFeature>*" ) ]
186+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
187+ public IHttpMinRequestBodyDataRateFeature Get_IHttpMinRequestBodyDataRateFeature ( )
188+ {
189+ return _collection . Get < IHttpMinRequestBodyDataRateFeature > ( ) ;
190+ }
191+
192+ [ Benchmark ( Description = "Get<IHttpMinResponseDataRateFeature>*" ) ]
193+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
194+ public IHttpMinResponseDataRateFeature Get_IHttpMinResponseDataRateFeature ( )
195+ {
196+ return _collection . Get < IHttpMinResponseDataRateFeature > ( ) ;
197+ }
198+
199+ [ Benchmark ( Description = "Get<IHttpBodyControlFeature>*" ) ]
200+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
201+ public IHttpBodyControlFeature Get_IHttpBodyControlFeature ( )
202+ {
203+ return _collection . Get < IHttpBodyControlFeature > ( ) ;
204+ }
205+
206+ [ Benchmark ( Description = "Get<IHttpRequestBodyDetectionFeature>*" ) ]
207+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
208+ public IHttpRequestBodyDetectionFeature Get_IHttpRequestBodyDetectionFeature ( )
209+ {
210+ return _collection . Get < IHttpRequestBodyDetectionFeature > ( ) ;
211+ }
212+
213+ [ Benchmark ( Description = "Get<IHttpResetFeature>" ) ]
214+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
215+ public IHttpResetFeature Get_IHttpResetFeature ( )
216+ {
217+ return _collection . Get < IHttpResetFeature > ( ) ;
218+ }
219+
220+ [ Benchmark ( Description = "Get<IHttpNotFoundFeature>" ) ]
221+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
222+ public IHttpNotFoundFeature Get_IHttpNotFoundFeature ( )
62223 {
63224 return _collection . Get < IHttpNotFoundFeature > ( ) ;
64225 }
@@ -89,11 +250,7 @@ public HttpProtocolFeatureCollection()
89250 _collection = http1Connection ;
90251 }
91252
92- private interface IHttpCustomFeature
93- {
94- }
95-
96- private interface IHttpNotFoundFeature
253+ public interface IHttpNotFoundFeature
97254 {
98255 }
99256 }
0 commit comments