|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using Microsoft.AspNetCore.Http.Timeouts; |
| 6 | +using Microsoft.Extensions.Logging.Abstractions; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Http; |
| 10 | + |
| 11 | +public class RequestTimeoutsMiddlewareBenchmark |
| 12 | +{ |
| 13 | + RequestTimeoutsMiddleware _middlewareWithNoTimeout; |
| 14 | + RequestTimeoutsMiddleware _middleware; |
| 15 | + RequestTimeoutsMiddleware _middlewareWithThrow; |
| 16 | + |
| 17 | + [GlobalSetup] |
| 18 | + public void GlobalSetup() |
| 19 | + { |
| 20 | + _middlewareWithNoTimeout = new RequestTimeoutsMiddleware( |
| 21 | + async context => { await Task.Yield(); }, |
| 22 | + new CancellationTokenLinker(), |
| 23 | + NullLogger<RequestTimeoutsMiddleware>.Instance, |
| 24 | + Options.Create(new RequestTimeoutOptions())); |
| 25 | + |
| 26 | + _middleware = new RequestTimeoutsMiddleware( |
| 27 | + async context => { await Task.Yield(); }, |
| 28 | + new CancellationTokenLinker(), |
| 29 | + NullLogger<RequestTimeoutsMiddleware>.Instance, |
| 30 | + Options.Create(new RequestTimeoutOptions |
| 31 | + { |
| 32 | + DefaultPolicy = new RequestTimeoutPolicy |
| 33 | + { |
| 34 | + Timeout = TimeSpan.FromMilliseconds(200) |
| 35 | + }, |
| 36 | + Policies = |
| 37 | + { |
| 38 | + ["policy1"] = new RequestTimeoutPolicy { Timeout = TimeSpan.FromMilliseconds(200)} |
| 39 | + } |
| 40 | + })); |
| 41 | + |
| 42 | + _middlewareWithThrow = new RequestTimeoutsMiddleware( |
| 43 | + async context => |
| 44 | + { |
| 45 | + await Task.Delay(TimeSpan.FromMicroseconds(2)); |
| 46 | + context.RequestAborted.ThrowIfCancellationRequested(); |
| 47 | + }, |
| 48 | + new CancellationTokenLinker(), |
| 49 | + NullLogger<RequestTimeoutsMiddleware>.Instance, |
| 50 | + Options.Create(new RequestTimeoutOptions |
| 51 | + { |
| 52 | + DefaultPolicy = new RequestTimeoutPolicy |
| 53 | + { |
| 54 | + Timeout = TimeSpan.FromMicroseconds(1) |
| 55 | + } |
| 56 | + })); |
| 57 | + } |
| 58 | + |
| 59 | + [Benchmark] |
| 60 | + public async Task NoMetadataNoDefault() |
| 61 | + { |
| 62 | + var context = CreateHttpContext(new Endpoint(null, null, null)); |
| 63 | + await _middlewareWithNoTimeout.Invoke(context); |
| 64 | + } |
| 65 | + |
| 66 | + [Benchmark] |
| 67 | + public async Task DefaultTimeout() |
| 68 | + { |
| 69 | + var context = CreateHttpContext(new Endpoint(null, null, null)); |
| 70 | + |
| 71 | + await _middleware.Invoke(context); |
| 72 | + } |
| 73 | + |
| 74 | + [Benchmark] |
| 75 | + public async Task DefaultTimeoutOverridenByDisable() |
| 76 | + { |
| 77 | + var context = CreateHttpContext(new Endpoint( |
| 78 | + null, |
| 79 | + new EndpointMetadataCollection(new DisableRequestTimeoutAttribute()), |
| 80 | + null)); |
| 81 | + |
| 82 | + await _middleware.Invoke(context); |
| 83 | + } |
| 84 | + |
| 85 | + [Benchmark] |
| 86 | + public async Task TimeoutMetadata() |
| 87 | + { |
| 88 | + var context = CreateHttpContext(new Endpoint( |
| 89 | + null, |
| 90 | + new EndpointMetadataCollection(new RequestTimeoutAttribute(200)), |
| 91 | + null)); |
| 92 | + |
| 93 | + await _middleware.Invoke(context); |
| 94 | + } |
| 95 | + |
| 96 | + [Benchmark] |
| 97 | + public async Task NamedPolicyMetadata() |
| 98 | + { |
| 99 | + var context = CreateHttpContext(new Endpoint( |
| 100 | + null, |
| 101 | + new EndpointMetadataCollection(new RequestTimeoutAttribute("policy1")), |
| 102 | + null)); |
| 103 | + |
| 104 | + await _middleware.Invoke(context); |
| 105 | + } |
| 106 | + |
| 107 | + [Benchmark] |
| 108 | + public async Task TimeoutFires() |
| 109 | + { |
| 110 | + var context = CreateHttpContext(new Endpoint(null, null, null)); |
| 111 | + |
| 112 | + await _middlewareWithThrow.Invoke(context); |
| 113 | + } |
| 114 | + |
| 115 | + private HttpContext CreateHttpContext(Endpoint endpoint) |
| 116 | + { |
| 117 | + var context = new DefaultHttpContext(); |
| 118 | + context.SetEndpoint(endpoint); |
| 119 | + |
| 120 | + var cts = new CancellationTokenSource(); |
| 121 | + context.RequestAborted = cts.Token; |
| 122 | + |
| 123 | + return context; |
| 124 | + } |
| 125 | + |
| 126 | + private sealed class Options : IOptionsMonitor<RequestTimeoutOptions> |
| 127 | + { |
| 128 | + private readonly RequestTimeoutOptions _options; |
| 129 | + |
| 130 | + private Options(RequestTimeoutOptions options) |
| 131 | + { |
| 132 | + _options = options; |
| 133 | + } |
| 134 | + |
| 135 | + public static Options Create(RequestTimeoutOptions options) |
| 136 | + { |
| 137 | + return new Options(options); |
| 138 | + } |
| 139 | + |
| 140 | + public RequestTimeoutOptions CurrentValue => _options; |
| 141 | + |
| 142 | + public RequestTimeoutOptions Get(string name) => _options; |
| 143 | + |
| 144 | + public IDisposable OnChange(Action<RequestTimeoutOptions, string> listener) |
| 145 | + { |
| 146 | + return default; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments