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 515
Add Multithread perf test #1222
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
192 changes: 192 additions & 0 deletions
192
test/Microsoft.AspNetCore.Server.Kestrel.Performance/MultiThreadedRequestParsing.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,192 @@ | ||
| // 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.Threading.Tasks; | ||
| using BenchmarkDotNet.Attributes; | ||
| using Microsoft.AspNetCore.Server.Kestrel.Internal.Http; | ||
| using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; | ||
| using Microsoft.AspNetCore.Testing; | ||
| using RequestLineStatus = Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.RequestLineStatus; | ||
|
|
||
| namespace Microsoft.AspNetCore.Server.Kestrel.Performance | ||
| { | ||
| [Config(typeof(CoreConfig))] | ||
| public class MultiThreadedRequestParsing | ||
| { | ||
| // Is divided by processor count so will be slightly off as the | ||
| // LCM of Intel's core counts (22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2) is 55440 | ||
| // which would be far too many iterations | ||
| private const int InnerLoopCount = 4096; | ||
|
|
||
| private static readonly int ThreadCount = Environment.ProcessorCount; | ||
| private static readonly int LoopCount = InnerLoopCount / ThreadCount; | ||
|
|
||
| private static MemoryPool[] MemoryPool; | ||
| private static SocketInput[] Input; | ||
| private static Frame<object>[] Frame; | ||
|
|
||
| [Benchmark(Baseline = true, OperationsPerInvoke = InnerLoopCount)] | ||
| public void ParsePlaintext() | ||
| { | ||
| Parallel.For(0, ThreadCount, new ParallelOptions() {MaxDegreeOfParallelism = ThreadCount}, (index) => | ||
| { | ||
| var socketInput = Input[index]; | ||
| var frame = Frame[index]; | ||
|
|
||
| for (var i = 0; i < LoopCount; i++) | ||
| { | ||
| InsertData(socketInput, Requests.PlaintextRequest); | ||
|
|
||
| ParseData(socketInput, frame); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Benchmark(OperationsPerInvoke = InnerLoopCount * Requests.Pipelining)] | ||
| public void ParsePipelinedPlaintext() | ||
| { | ||
| Parallel.For(0, ThreadCount, new ParallelOptions() { MaxDegreeOfParallelism = ThreadCount }, (index) => | ||
| { | ||
| var socketInput = Input[index]; | ||
| var frame = Frame[index]; | ||
|
|
||
| for (var i = 0; i < LoopCount; i++) | ||
| { | ||
| InsertData(socketInput, Requests.PlaintextPipelinedRequests); | ||
|
|
||
| ParseData(socketInput, frame); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Benchmark(OperationsPerInvoke = InnerLoopCount)] | ||
| public void ParseLiveAspNet() | ||
| { | ||
| Parallel.For(0, ThreadCount, new ParallelOptions() { MaxDegreeOfParallelism = ThreadCount }, (index) => | ||
| { | ||
| var socketInput = Input[index]; | ||
| var frame = Frame[index]; | ||
|
|
||
| for (var i = 0; i < LoopCount; i++) | ||
| { | ||
| InsertData(socketInput, Requests.LiveAspNetRequest); | ||
|
|
||
| ParseData(socketInput, frame); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Benchmark(OperationsPerInvoke = InnerLoopCount * Requests.Pipelining)] | ||
| public void ParsePipelinedLiveAspNet() | ||
| { | ||
| Parallel.For(0, ThreadCount, new ParallelOptions() { MaxDegreeOfParallelism = ThreadCount }, (index) => | ||
| { | ||
| var socketInput = Input[index]; | ||
| var frame = Frame[index]; | ||
|
|
||
| for (var i = 0; i < LoopCount; i++) | ||
| { | ||
| InsertData(socketInput, Requests.LiveAspNetPipelinedRequests); | ||
|
|
||
| ParseData(socketInput, frame); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Benchmark(OperationsPerInvoke = InnerLoopCount)] | ||
| public void ParseUnicode() | ||
| { | ||
| Parallel.For(0, ThreadCount, new ParallelOptions() { MaxDegreeOfParallelism = ThreadCount }, (index) => | ||
| { | ||
| var socketInput = Input[index]; | ||
| var frame = Frame[index]; | ||
|
|
||
| for (var i = 0; i < LoopCount; i++) | ||
| { | ||
| InsertData(socketInput, Requests.UnicodeRequest); | ||
|
|
||
| ParseData(socketInput, frame); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| [Benchmark(OperationsPerInvoke = InnerLoopCount * Requests.Pipelining)] | ||
| public void ParseUnicodePipelined() | ||
| { | ||
| Parallel.For(0, ThreadCount, new ParallelOptions() { MaxDegreeOfParallelism = ThreadCount }, (index) => | ||
| { | ||
| var socketInput = Input[index]; | ||
| var frame = Frame[index]; | ||
|
|
||
| for (var i = 0; i < LoopCount; i++) | ||
| { | ||
| InsertData(socketInput, Requests.UnicodePipelinedRequests); | ||
|
|
||
| ParseData(socketInput, frame); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static void InsertData(SocketInput socketInput, byte[] dataBytes) | ||
| { | ||
| socketInput.IncomingData(dataBytes, 0, dataBytes.Length); | ||
| } | ||
|
|
||
| private static void ParseData(SocketInput socketInput, Frame<object> frame) | ||
| { | ||
| while (socketInput.GetAwaiter().IsCompleted) | ||
| { | ||
| frame.Reset(); | ||
|
|
||
| if (frame.TakeStartLine(socketInput) != RequestLineStatus.Done) | ||
| { | ||
| ThrowInvalidStartLine(); | ||
| } | ||
|
|
||
| frame.InitializeHeaders(); | ||
|
|
||
| if (!frame.TakeMessageHeaders(socketInput, (FrameRequestHeaders)frame.RequestHeaders)) | ||
| { | ||
| ThrowInvalidMessageHeaders(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void ThrowInvalidStartLine() | ||
| { | ||
| throw new InvalidOperationException("Invalid StartLine"); | ||
| } | ||
|
|
||
| private static void ThrowInvalidMessageHeaders() | ||
| { | ||
| throw new InvalidOperationException("Invalid MessageHeaders"); | ||
| } | ||
|
|
||
| [Setup] | ||
| public void Setup() | ||
| { | ||
| var threadCount = ThreadCount; | ||
|
|
||
| MemoryPool = new MemoryPool[threadCount]; | ||
| Input = new SocketInput[threadCount]; | ||
| Frame = new Frame<object>[threadCount]; | ||
|
|
||
| for (var i = 0; i < threadCount; i++) | ||
| { | ||
| Requests.SetupFrameObjects(out MemoryPool[i], out Input[i], out Frame[i]); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| [Cleanup] | ||
| public void Cleanup() | ||
| { | ||
| var threadCount = ThreadCount; | ||
| for (var i = 0; i < threadCount; i++) | ||
| { | ||
| Requests.CleanUpFrameObjects(ref MemoryPool[i], ref Input[i], ref Frame[i]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
For convenience, can we print this?
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.
Added to Program.cs