|
| 1 | +// Copyright (c) Martin Costello, 2024. All rights reserved. |
| 2 | +// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using BenchmarkDotNet.Diagnosers; |
| 6 | +using Microsoft.AspNetCore.Builder; |
| 7 | +using Microsoft.AspNetCore.Hosting; |
| 8 | +using Microsoft.AspNetCore.Hosting.Server; |
| 9 | +using Microsoft.AspNetCore.Hosting.Server.Features; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | + |
| 13 | +namespace TodoApp; |
| 14 | + |
| 15 | +[EventPipeProfiler(EventPipeProfile.CpuSampling)] |
| 16 | +[MemoryDiagnoser] |
| 17 | +[ShortRunJob] |
| 18 | +public class OpenApiBenchmarks : IAsyncDisposable |
| 19 | +{ |
| 20 | + private WebApplication? _app; |
| 21 | + private HttpClient? _client; |
| 22 | + private bool _disposed; |
| 23 | + |
| 24 | + public OpenApiBenchmarks() |
| 25 | + { |
| 26 | + var builder = WebApplication.CreateBuilder([$"--contentRoot={GetContentRoot()}"]); |
| 27 | + |
| 28 | + builder.Logging.ClearProviders(); |
| 29 | + builder.WebHost.UseUrls("https://127.0.0.1:0"); |
| 30 | + |
| 31 | + builder.AddTodoApp(); |
| 32 | + |
| 33 | + _app = builder.Build(); |
| 34 | + _app.UseTodoApp(); |
| 35 | + } |
| 36 | + |
| 37 | + [GlobalSetup] |
| 38 | + public async Task StartServer() |
| 39 | + { |
| 40 | + if (_app is { } app) |
| 41 | + { |
| 42 | + await app.StartAsync(); |
| 43 | + |
| 44 | + var server = app.Services.GetRequiredService<IServer>(); |
| 45 | + var addresses = server.Features.Get<IServerAddressesFeature>(); |
| 46 | + |
| 47 | + var baseAddress = addresses!.Addresses |
| 48 | + .Select((p) => new Uri(p)) |
| 49 | + .Last(); |
| 50 | + |
| 51 | + var handler = new HttpClientHandler |
| 52 | + { |
| 53 | + ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator |
| 54 | + }; |
| 55 | + |
| 56 | +#pragma warning disable CA5400 |
| 57 | + _client = new(handler, disposeHandler: true) { BaseAddress = baseAddress }; |
| 58 | +#pragma warning restore CA5400 |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [GlobalCleanup] |
| 63 | + public async Task StopServer() |
| 64 | + { |
| 65 | + if (_app is { } app) |
| 66 | + { |
| 67 | + await app.StopAsync(); |
| 68 | + _app = null; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [Benchmark] |
| 73 | + public async Task<string> AspNetCore() => await _client!.GetStringAsync("/openapi/v1.json"); |
| 74 | + |
| 75 | + [Benchmark] |
| 76 | + public async Task<string> NSwag() => await _client!.GetStringAsync("/nswag/v1.json"); |
| 77 | + |
| 78 | + [Benchmark] |
| 79 | + public async Task<string> Swashbuckle() => await _client!.GetStringAsync("/swagger/v1/swagger.json"); |
| 80 | + |
| 81 | + public async ValueTask DisposeAsync() |
| 82 | + { |
| 83 | + GC.SuppressFinalize(this); |
| 84 | + |
| 85 | + if (!_disposed) |
| 86 | + { |
| 87 | + _client?.Dispose(); |
| 88 | + |
| 89 | + if (_app is not null) |
| 90 | + { |
| 91 | + await _app.DisposeAsync(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + _disposed = true; |
| 96 | + } |
| 97 | + |
| 98 | + private static string GetContentRoot() |
| 99 | + { |
| 100 | + string contentRoot = string.Empty; |
| 101 | + var directoryInfo = new DirectoryInfo(Path.GetDirectoryName(typeof(OpenApiBenchmarks).Assembly.Location)!); |
| 102 | + |
| 103 | + do |
| 104 | + { |
| 105 | + string? solutionPath = Directory.EnumerateFiles(directoryInfo.FullName, "TodoApp.sln").FirstOrDefault(); |
| 106 | + |
| 107 | + if (solutionPath is not null) |
| 108 | + { |
| 109 | + contentRoot = Path.GetFullPath(Path.Combine(directoryInfo.FullName, "src", "TodoApp")); |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + directoryInfo = directoryInfo.Parent; |
| 114 | + } |
| 115 | + while (directoryInfo is not null); |
| 116 | + |
| 117 | + return contentRoot; |
| 118 | + } |
| 119 | +} |
0 commit comments