This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 523
Add benchmarks for ResponseHeaders sets #1256
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
test/Microsoft.AspNetCore.Server.Kestrel.Performance/ResponseHeaders.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.AspNetCore.Hosting.Internal; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.Server.Kestrel.Internal; | ||
using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; | ||
using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; | ||
using Microsoft.AspNetCore.Testing; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.ObjectPool; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Performance | ||
{ | ||
[Config(typeof(CoreConfig))] | ||
public class ResponseHeaders | ||
{ | ||
private const int InnerLoopCount = 512; | ||
|
||
private static FrameResponseHeaders HeadersDirect; | ||
private static DateHeaderValueManager DateHeaderValueManager = new DateHeaderValueManager(); | ||
|
||
private MemoryPool MemoryPool; | ||
private SocketInput SocketInput; | ||
private HostingApplication Application; | ||
private HostingApplication.Context Context; | ||
|
||
[Params("ContentLengthNumeric", "ContentLengthString", "Plaintext", "Primary", "Common", "Unknown")] | ||
public string Type { get; set; } | ||
|
||
private RequestDelegate GetRequestDelegate(string type) | ||
{ | ||
switch (type) | ||
{ | ||
case "Plaintext": | ||
return Plaintext; | ||
case "ContentLengthString": | ||
return ContentLengthString; | ||
case "ContentLengthNumeric": | ||
return ContentLengthNumeric; | ||
case "Primary": | ||
return Primary; | ||
case "Common": | ||
return Common; | ||
case "Unknown": | ||
return Unknown; | ||
} | ||
|
||
return Plaintext; | ||
} | ||
|
||
[Benchmark(OperationsPerInvoke = InnerLoopCount)] | ||
public async Task BenchmarkAsync() | ||
{ | ||
for (int i = 0; i < InnerLoopCount; i++) | ||
{ | ||
await Application.ProcessRequestAsync(Context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why benchmark via app instead of just instantiating |
||
} | ||
} | ||
|
||
private static readonly RequestDelegate Plaintext = (context) => | ||
{ | ||
HeadersDirect.Reset(); | ||
|
||
var response = context.Response; | ||
response.StatusCode = 200; | ||
response.ContentType = "text/plain"; | ||
response.Headers["Content-Length"] = "13"; | ||
|
||
var dateHeaderValues = DateHeaderValueManager.GetDateHeaderValues(); | ||
HeadersDirect.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes); | ||
return Task.CompletedTask; | ||
}; | ||
|
||
private static readonly RequestDelegate Primary = (context) => | ||
{ | ||
HeadersDirect.Reset(); | ||
|
||
var response = context.Response; | ||
response.StatusCode = 200; | ||
response.ContentType = "text/plain"; | ||
response.Headers["Connection"] = "Close"; | ||
response.Headers["Content-Length"] = "13"; | ||
response.Headers["Server"] = "Kestrel"; | ||
|
||
var dateHeaderValues = DateHeaderValueManager.GetDateHeaderValues(); | ||
HeadersDirect.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes); | ||
return Task.CompletedTask; | ||
}; | ||
|
||
private static readonly RequestDelegate Common = (context) => | ||
{ | ||
HeadersDirect.Reset(); | ||
|
||
var response = context.Response; | ||
response.StatusCode = 200; | ||
response.ContentType = "text/plain"; | ||
response.Headers["Connection"] = "Close"; | ||
response.Headers["Content-Length"] = "13"; | ||
response.Headers["Server"] = "Kestrel"; | ||
response.Headers["Cache-Control"] = "Test Value"; | ||
response.Headers["Keep-Alive"] = "Test Value"; | ||
response.Headers["Pragma"] = "Test Value"; | ||
response.Headers["Trailer"] = "Test Value"; | ||
response.Headers["Transfer-Encoding"] = "Test Value"; | ||
response.Headers["Upgrade"] = "Test Value"; | ||
response.Headers["Via"] = "Test Value"; | ||
response.Headers["Warning"] = "Test Value"; | ||
response.Headers["Allow"] = "Test Value"; | ||
response.Headers["Content-Encoding"] = "Test Value"; | ||
response.Headers["Content-Language"] = "Test Value"; | ||
response.Headers["Content-Location"] = "Test Value"; | ||
response.Headers["Content-MD5"] = "Test Value"; | ||
response.Headers["Content-Range"] = "Test Value"; | ||
response.Headers["Expires"] = "Test Value"; | ||
response.Headers["Last-Modified"] = "Test Value"; | ||
var dateHeaderValues = DateHeaderValueManager.GetDateHeaderValues(); | ||
HeadersDirect.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes); | ||
return Task.CompletedTask; | ||
}; | ||
|
||
private static readonly RequestDelegate Unknown = (context) => | ||
{ | ||
HeadersDirect.Reset(); | ||
|
||
var response = context.Response; | ||
response.StatusCode = 200; | ||
response.ContentType = "text/plain"; | ||
response.Headers["Content-Length"] = "13"; | ||
|
||
response.Headers["Unknown"] = "Unknown"; | ||
response.Headers["IUnknown"] = "Unknown"; | ||
response.Headers["X-Unknown"] = "Unknown"; | ||
|
||
var dateHeaderValues = DateHeaderValueManager.GetDateHeaderValues(); | ||
HeadersDirect.SetRawDate(dateHeaderValues.String, dateHeaderValues.Bytes); | ||
return Task.CompletedTask; | ||
}; | ||
|
||
private static readonly RequestDelegate ContentLengthString = (context) => | ||
{ | ||
var response = context.Response; | ||
response.Headers["Content-Length"] = "13"; | ||
return Task.CompletedTask; | ||
}; | ||
|
||
private static readonly RequestDelegate ContentLengthNumeric = (context) => | ||
{ | ||
var response = context.Response; | ||
response.ContentLength = 13; | ||
return Task.CompletedTask; | ||
}; | ||
|
||
[Setup] | ||
public void Setup() | ||
{ | ||
var trace = new KestrelTrace(new TestKestrelTrace()); | ||
var threadPool = new LoggingThreadPool(trace); | ||
|
||
MemoryPool = new MemoryPool(); | ||
SocketInput = new SocketInput(MemoryPool, threadPool); | ||
|
||
var connectionContext = new MockConnection(new KestrelServerOptions()); | ||
connectionContext.Input = SocketInput; | ||
|
||
var httpContextFactory = new HttpContextFactory(new DefaultObjectPoolProvider(), Options.Create(new FormOptions())); | ||
|
||
var testType = GetRequestDelegate(Type); | ||
|
||
Application = new HostingApplication(testType, new NullScopeLogger(), new NoopDiagnosticSource(), httpContextFactory); | ||
|
||
var frame = new Frame<HostingApplication.Context>(application: Application, context: connectionContext); | ||
frame.Reset(); | ||
frame.InitializeHeaders(); | ||
HeadersDirect = (FrameResponseHeaders)frame.ResponseHeaders; | ||
Context = Application.CreateContext(frame); | ||
} | ||
|
||
[Cleanup] | ||
public void Cleanup() | ||
{ | ||
Application.DisposeContext(Context, null); | ||
SocketInput.IncomingFin(); | ||
SocketInput.Dispose(); | ||
MemoryPool.Dispose(); | ||
} | ||
|
||
private class NullScopeLogger : ILogger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
public IDisposable BeginScope<TState>(TState state) => null; | ||
|
||
public bool IsEnabled(LogLevel logLevel) => false; | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
} | ||
} | ||
|
||
private class NoopDiagnosticSource : DiagnosticSource | ||
{ | ||
public override bool IsEnabled(string name) => false; | ||
|
||
public override void Write(string name, object value) | ||
{ | ||
} | ||
} | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
test/Microsoft.AspNetCore.Server.Kestrel.Performance/project.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
_memoryPool
, same for other fields. Or make them properties.