|  | 
|  | 1 | +namespace Benchmarks.HealthChecks.Benchmarks; | 
|  | 2 | + | 
|  | 3 | +using System; | 
|  | 4 | +using System.Threading.Tasks; | 
|  | 5 | +using BenchmarkDotNet.Attributes; | 
|  | 6 | +using global::Benchmarks.HealthChecks.Internals; | 
|  | 7 | +using Microsoft.Extensions.Configuration; | 
|  | 8 | +using Microsoft.Extensions.DependencyInjection; | 
|  | 9 | +using Microsoft.Extensions.Logging.Abstractions; | 
|  | 10 | +using NetEvolve.HealthChecks.SqlServer; | 
|  | 11 | +using NetEvolve.HealthChecks.SqlServer.Legacy; | 
|  | 12 | +using Testcontainers.MsSql; | 
|  | 13 | + | 
|  | 14 | +public class SqlServer | 
|  | 15 | +{ | 
|  | 16 | +    private readonly MsSqlContainer _databaseOne = new MsSqlBuilder().WithLogger(NullLogger.Instance).Build(); | 
|  | 17 | +    private readonly MsSqlContainer _databaseTwo = new MsSqlBuilder().WithLogger(NullLogger.Instance).Build(); | 
|  | 18 | + | 
|  | 19 | +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. | 
|  | 20 | +    private BenchmarkHealthCheckService _netEvolveHealthCheckExecutor; | 
|  | 21 | +    private BenchmarkHealthCheckService _netEvolveLegacyHealthCheckExecutor; | 
|  | 22 | +    private BenchmarkHealthCheckService _anotherHealthCheckExecutor; | 
|  | 23 | +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. | 
|  | 24 | + | 
|  | 25 | +    [GlobalSetup] | 
|  | 26 | +    public async Task GlobalSetupAsync() | 
|  | 27 | +    { | 
|  | 28 | +        await _databaseOne.StartAsync().ConfigureAwait(false); | 
|  | 29 | +        await _databaseTwo.StartAsync().ConfigureAwait(false); | 
|  | 30 | + | 
|  | 31 | +        var configuration = new ConfigurationBuilder().Build(); | 
|  | 32 | + | 
|  | 33 | +        var netEvolveServices = new ServiceCollection() | 
|  | 34 | +            .AddSingleton<IConfiguration>(configuration) | 
|  | 35 | +            .AddSingleton<BenchmarkHealthCheckService>(); | 
|  | 36 | +        _ = netEvolveServices | 
|  | 37 | +            .AddHealthChecks() | 
|  | 38 | +            .AddSqlServer( | 
|  | 39 | +                nameof(_databaseOne), | 
|  | 40 | +                options => options.ConnectionString = _databaseOne.GetConnectionString() | 
|  | 41 | +            ) | 
|  | 42 | +            .AddSqlServer( | 
|  | 43 | +                nameof(_databaseTwo), | 
|  | 44 | +                options => options.ConnectionString = _databaseTwo.GetConnectionString() | 
|  | 45 | +            ); | 
|  | 46 | +        var netEvolveServiceProvider = netEvolveServices.BuildServiceProvider(); | 
|  | 47 | +        _netEvolveHealthCheckExecutor = netEvolveServiceProvider.GetRequiredService<BenchmarkHealthCheckService>(); | 
|  | 48 | + | 
|  | 49 | +        var netEvolveLegacyServices = new ServiceCollection() | 
|  | 50 | +            .AddSingleton<IConfiguration>(configuration) | 
|  | 51 | +            .AddSingleton<BenchmarkHealthCheckService>(); | 
|  | 52 | +        _ = netEvolveLegacyServices | 
|  | 53 | +            .AddHealthChecks() | 
|  | 54 | +            .AddSqlServerLegacy(nameof(_databaseOne), options => _databaseOne.GetConnectionString()) | 
|  | 55 | +            .AddSqlServerLegacy(nameof(_databaseTwo), options => _databaseTwo.GetConnectionString()); | 
|  | 56 | +        var netEvolveLegacyServiceProvider = netEvolveLegacyServices.BuildServiceProvider(); | 
|  | 57 | +        _netEvolveLegacyHealthCheckExecutor = | 
|  | 58 | +            netEvolveLegacyServiceProvider.GetRequiredService<BenchmarkHealthCheckService>(); | 
|  | 59 | + | 
|  | 60 | +        var aspnetcoreServices = new ServiceCollection() | 
|  | 61 | +            .AddSingleton<IConfiguration>(configuration) | 
|  | 62 | +            .AddSingleton<BenchmarkHealthCheckService>(); | 
|  | 63 | +        _ = aspnetcoreServices | 
|  | 64 | +            .AddHealthChecks() | 
|  | 65 | +            .AddSqlServer(_databaseOne.GetConnectionString(), name: nameof(_databaseOne)) | 
|  | 66 | +            .AddSqlServer(_databaseTwo.GetConnectionString(), name: nameof(_databaseTwo)); | 
|  | 67 | +        var anotherServiceProvider = aspnetcoreServices.BuildServiceProvider(); | 
|  | 68 | +        _anotherHealthCheckExecutor = anotherServiceProvider.GetRequiredService<BenchmarkHealthCheckService>(); | 
|  | 69 | +    } | 
|  | 70 | + | 
|  | 71 | +    [GlobalCleanup] | 
|  | 72 | +    public async Task GlobalCleanupAsync() | 
|  | 73 | +    { | 
|  | 74 | +        if (_databaseOne is not null) | 
|  | 75 | +        { | 
|  | 76 | +            await _databaseOne.StopAsync().ConfigureAwait(false); | 
|  | 77 | +            await _databaseOne.DisposeAsync().ConfigureAwait(false); | 
|  | 78 | +        } | 
|  | 79 | + | 
|  | 80 | +        if (_databaseTwo is not null) | 
|  | 81 | +        { | 
|  | 82 | +            await _databaseTwo.StopAsync().ConfigureAwait(false); | 
|  | 83 | +            await _databaseTwo.DisposeAsync().ConfigureAwait(false); | 
|  | 84 | +        } | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    [Benchmark(Baseline = true, Description = "AspNetCore.HealthChecks.SqlServer")] | 
|  | 88 | +    public Task BenchmarkAspNetCoreAsync() => _ = _anotherHealthCheckExecutor.CheckHealthAsync(); | 
|  | 89 | + | 
|  | 90 | +    [Benchmark(Description = "NetEvolve.HealthChecks.SqlServer")] | 
|  | 91 | +    public Task BenchmarkNetEvolveAsync() => _ = _netEvolveHealthCheckExecutor.CheckHealthAsync(); | 
|  | 92 | + | 
|  | 93 | +    [Benchmark(Description = "NetEvolve.HealthChecks.SqlServer.Legacy")] | 
|  | 94 | +    public Task BenchmarkNetEvolveLegacyAsync() => _ = _netEvolveLegacyHealthCheckExecutor.CheckHealthAsync(); | 
|  | 95 | +} | 
0 commit comments