|
| 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