forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhead Benchmark for Request Throttling (dotnet#10907)
* initial implementation; no tests * benchmark project * Better benchmarks * overhead test for congested queue * Addressed feedback
- Loading branch information
1 parent
af812f2
commit 844530d
Showing
15 changed files
with
265 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 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
1 change: 1 addition & 0 deletions
1
src/Middleware/RequestThrottling/perf/Microbenchmarks/AssemblyInfo.cs
This file contains 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 @@ | ||
[assembly: BenchmarkDotNet.Attributes.AspNetCoreBenchmark] |
14 changes: 14 additions & 0 deletions
14
...ttling/perf/Microbenchmarks/Microsoft.AspNetCore.RequestThrottling.Microbenchmarks.csproj
This file contains 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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
<!--<StartupObject>Microsoft.AspNetCore.RequestThrottling.Microbenchmarks.Test</StartupObject>--> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="BenchmarkDotNet" /> | ||
<Reference Include="Microsoft.AspNetCore.BenchmarkRunner.Sources" /> | ||
<Reference Include="Microsoft.AspNetCore.RequestThrottling" /> | ||
</ItemGroup> | ||
</Project> |
71 changes: 71 additions & 0 deletions
71
src/Middleware/RequestThrottling/perf/Microbenchmarks/QueueEmptyOverhead.cs
This file contains 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,71 @@ | ||
// 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; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Running; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.RequestThrottling.Microbenchmarks | ||
{ | ||
public class QueueEmptyOverhead | ||
{ | ||
private const int _numRequests = 20000; | ||
|
||
private RequestThrottlingMiddleware _middleware; | ||
private RequestDelegate _restOfServer; | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
_restOfServer = YieldsThreadInternally ? (RequestDelegate)YieldsThread : (RequestDelegate)CompletesImmediately; | ||
|
||
var options = new RequestThrottlingOptions | ||
{ | ||
MaxConcurrentRequests = 8, | ||
RequestQueueLimit = _numRequests | ||
}; | ||
|
||
_middleware = new RequestThrottlingMiddleware( | ||
next: _restOfServer, | ||
loggerFactory: NullLoggerFactory.Instance, | ||
options: Options.Create(options) | ||
); | ||
} | ||
|
||
[Params(false, true)] | ||
public bool YieldsThreadInternally; | ||
|
||
[Benchmark(OperationsPerInvoke = _numRequests)] | ||
public async Task Baseline() | ||
{ | ||
for (int i = 0; i < _numRequests; i++) | ||
{ | ||
await _restOfServer(null); | ||
} | ||
} | ||
|
||
[Benchmark(OperationsPerInvoke = _numRequests)] | ||
public async Task WithEmptyQueueOverhead() | ||
{ | ||
for (int i = 0; i < _numRequests; i++) | ||
{ | ||
await _middleware.Invoke(null); | ||
} | ||
} | ||
|
||
private static async Task YieldsThread(HttpContext context) | ||
{ | ||
await Task.Yield(); | ||
} | ||
|
||
private static Task CompletesImmediately(HttpContext context) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/Middleware/RequestThrottling/perf/Microbenchmarks/QueueFullOverhead.cs
This file contains 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,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.RequestThrottling.Microbenchmarks | ||
{ | ||
public class QueueFullOverhead | ||
{ | ||
private const int _numRequests = 2000; | ||
private int _requestCount = 0; | ||
private ManualResetEventSlim _mres = new ManualResetEventSlim(); | ||
|
||
private RequestThrottlingMiddleware _middleware; | ||
|
||
[Params(8)] | ||
public int MaxConcurrentRequests; | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
var options = new RequestThrottlingOptions | ||
{ | ||
MaxConcurrentRequests = MaxConcurrentRequests, | ||
RequestQueueLimit = _numRequests | ||
}; | ||
|
||
_middleware = new RequestThrottlingMiddleware( | ||
next: (RequestDelegate)_incrementAndCheck, | ||
loggerFactory: NullLoggerFactory.Instance, | ||
options: Options.Create(options) | ||
); | ||
} | ||
|
||
[IterationSetup] | ||
public void Setup() | ||
{ | ||
_requestCount = 0; | ||
_mres.Reset(); | ||
} | ||
|
||
private async Task _incrementAndCheck(HttpContext context) | ||
{ | ||
if (Interlocked.Increment(ref _requestCount) == _numRequests) | ||
{ | ||
_mres.Set(); | ||
} | ||
|
||
await Task.Yield(); | ||
} | ||
|
||
[Benchmark(OperationsPerInvoke = _numRequests)] | ||
public void Baseline() | ||
{ | ||
for (int i = 0; i < _numRequests; i++) | ||
{ | ||
_ = _incrementAndCheck(null); | ||
} | ||
|
||
_mres.Wait(); | ||
} | ||
|
||
[Benchmark(OperationsPerInvoke = _numRequests)] | ||
public void QueueingAll() | ||
{ | ||
for (int i = 0; i < _numRequests; i++) | ||
{ | ||
_ = _middleware.Invoke(null); | ||
} | ||
|
||
_mres.Wait(); | ||
} | ||
} | ||
} |
This file contains 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
11 changes: 9 additions & 2 deletions
11
src/Middleware/RequestThrottling/sample/RequestThrottlingSample.csproj
This file contains 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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ItemGroup Condition="'$(BenchmarksTargetFramework)' == ''"> | ||
<Reference Include="Microsoft.Extensions.Logging.Console" /> | ||
<Reference Include="Microsoft.AspNetCore.RequestThrottling" /> | ||
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(BenchmarksTargetFramework)' != ''"> | ||
<PackageReference Include="Microsoft.AspNetCore.RequestThrottling" Version="$(MicrosoftAspNetCoreAppPackageVersion)" /> | ||
|
||
<FrameworkReference Update="Microsoft.AspNetCore.App" RuntimeFrameworkVersion="$(MicrosoftAspNetCoreAppPackageVersion)" /> | ||
<FrameworkReference Update="Microsoft.NETCore.App" RuntimeFrameworkVersion="$(MicrosoftNETCoreAppPackageVersion)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
17 changes: 17 additions & 0 deletions
17
src/Middleware/RequestThrottling/src/Internal/IRequestQueue.cs
This file contains 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,17 @@ | ||
// 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; | ||
|
||
namespace Microsoft.AspNetCore.RequestThrottling.Internal | ||
{ | ||
interface IRequestQueue : IDisposable | ||
{ | ||
int TotalRequests { get; } | ||
|
||
Task<bool> TryEnterQueueAsync(); | ||
|
||
void Release(); | ||
} | ||
} |
This file contains 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
3 changes: 2 additions & 1 deletion
3
src/Middleware/RequestThrottling/src/Microsoft.AspNetCore.RequestThrottling.csproj
This file contains 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 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 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.