Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 5847bfc

Browse files
committed
ResponseHeaders benchmark
1 parent 99c9012 commit 5847bfc

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

test/Microsoft.AspNetCore.Server.Kestrel.Performance/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ private static void RunSelectedBenchmarks(BenchmarkType type)
3636
{
3737
BenchmarkRunner.Run<Writing>();
3838
}
39+
if (type.HasFlag(BenchmarkType.ResponseHeaders))
40+
{
41+
BenchmarkRunner.Run<ResponseHeaders>();
42+
}
3943
}
4044
}
4145

@@ -44,6 +48,7 @@ public enum BenchmarkType : uint
4448
{
4549
RequestParsing = 1,
4650
Writing = 2,
51+
ResponseHeaders = 4,
4752
// add new ones in powers of two - e.g. 2,4,8,16...
4853

4954
All = uint.MaxValue
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using BenchmarkDotNet.Attributes;
5+
using Microsoft.AspNetCore.Server.Kestrel.Internal.Http;
6+
using Microsoft.AspNetCore.Testing;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Http.Internal;
9+
using System.Runtime.CompilerServices;
10+
using System.Text;
11+
12+
namespace Microsoft.AspNetCore.Server.Kestrel.Performance
13+
{
14+
[Config(typeof(CoreConfig))]
15+
public class ResponseHeaders
16+
{
17+
private const int InnerLoopCount = 512;
18+
19+
private static readonly byte[] _bytesServer = Encoding.ASCII.GetBytes("\r\nServer: Kestrel");
20+
private static DateHeaderValueManager _dateHeaderValueManager = new DateHeaderValueManager();
21+
private FrameResponseHeaders _responseHeadersDirect;
22+
private HttpResponse _response;
23+
24+
[Params("ContentLengthNumeric", "ContentLengthString", "Plaintext", "Common", "Unknown")]
25+
public string Type { get; set; }
26+
27+
[Benchmark(OperationsPerInvoke = InnerLoopCount)]
28+
public void SetHeaders()
29+
{
30+
switch (Type)
31+
{
32+
case "ContentLengthNumeric":
33+
ContentLengthNumeric();
34+
break;
35+
case "ContentLengthString":
36+
ContentLengthString();
37+
break;
38+
case "Plaintext":
39+
Plaintext();
40+
break;
41+
case "Common":
42+
Common();
43+
break;
44+
case "Unknown":
45+
Unknown();
46+
break;
47+
48+
}
49+
}
50+
51+
[MethodImpl(MethodImplOptions.NoInlining)]
52+
private void ContentLengthNumeric()
53+
{
54+
for (var i = 0; i < InnerLoopCount; i++)
55+
{
56+
_responseHeadersDirect.Reset();
57+
58+
_response.ContentLength = 0;
59+
}
60+
}
61+
62+
[MethodImpl(MethodImplOptions.NoInlining)]
63+
private void ContentLengthString()
64+
{
65+
for (var i = 0; i < InnerLoopCount; i++)
66+
{
67+
_responseHeadersDirect.Reset();
68+
69+
_response.Headers["Content-Length"] = "0";
70+
}
71+
}
72+
73+
[MethodImpl(MethodImplOptions.NoInlining)]
74+
private void Plaintext()
75+
{
76+
for (var i = 0; i < InnerLoopCount; i++)
77+
{
78+
_responseHeadersDirect.Reset();
79+
80+
_response.StatusCode = 200;
81+
_response.ContentType = "text/plain";
82+
_response.ContentLength = 13;
83+
84+
var dateHeaderValues = _dateHeaderValueManager.GetDateHeaderValues();
85+
_responseHeadersDirect.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes);
86+
_responseHeadersDirect.SetRawServer("Kestrel", _bytesServer);
87+
}
88+
}
89+
90+
[MethodImpl(MethodImplOptions.NoInlining)]
91+
private void Common()
92+
{
93+
for (var i = 0; i < InnerLoopCount; i++)
94+
{
95+
_responseHeadersDirect.Reset();
96+
97+
_response.StatusCode = 200;
98+
_response.ContentType = "text/plain";
99+
_response.ContentLength = 13;
100+
101+
var headers = _response.Headers;
102+
103+
headers["Connection"] = "Close";
104+
headers["Cache-Control"] = "Test Value";
105+
headers["Keep-Alive"] = "Test Value";
106+
headers["Pragma"] = "Test Value";
107+
headers["Trailer"] = "Test Value";
108+
headers["Transfer-Encoding"] = "Test Value";
109+
headers["Upgrade"] = "Test Value";
110+
headers["Via"] = "Test Value";
111+
headers["Warning"] = "Test Value";
112+
headers["Allow"] = "Test Value";
113+
headers["Content-Encoding"] = "Test Value";
114+
headers["Content-Language"] = "Test Value";
115+
headers["Content-Location"] = "Test Value";
116+
headers["Content-MD5"] = "Test Value";
117+
headers["Content-Range"] = "Test Value";
118+
headers["Expires"] = "Test Value";
119+
headers["Last-Modified"] = "Test Value";
120+
121+
var dateHeaderValues = _dateHeaderValueManager.GetDateHeaderValues();
122+
_responseHeadersDirect.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes);
123+
_responseHeadersDirect.SetRawServer("Kestrel", _bytesServer);
124+
}
125+
}
126+
127+
[MethodImpl(MethodImplOptions.NoInlining)]
128+
private void Unknown()
129+
{
130+
for (var i = 0; i < InnerLoopCount; i++)
131+
{
132+
_responseHeadersDirect.Reset();
133+
134+
_response.StatusCode = 200;
135+
_response.ContentType = "text/plain";
136+
_response.ContentLength = 13;
137+
138+
var headers = _response.Headers;
139+
140+
headers["Unknown"] = "Unknown";
141+
headers["IUnknown"] = "Unknown";
142+
headers["X-Unknown"] = "Unknown";
143+
144+
var dateHeaderValues = _dateHeaderValueManager.GetDateHeaderValues();
145+
_responseHeadersDirect.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes);
146+
_responseHeadersDirect.SetRawServer("Kestrel", _bytesServer);
147+
}
148+
}
149+
150+
[Setup]
151+
public void Setup()
152+
{
153+
var connectionContext = new MockConnection(new KestrelServerOptions());
154+
var frame = new Frame<object>(application: null, context: connectionContext);
155+
frame.Reset();
156+
frame.InitializeHeaders();
157+
_responseHeadersDirect = (FrameResponseHeaders)frame.ResponseHeaders;
158+
var context = new DefaultHttpContext(frame);
159+
_response = new DefaultHttpResponse(context);
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)