using System.Configuration; using Azure.Identity; using BenchmarkDotNet.Attributes; using LCU.API.Internal.Document.PerformanceTest.API; using LCU.API.Internal.DocumentLib.Configurations; using LCU.API.Internal.DocumentLib.Containers; using LCU.Library.Internal.DocumentModels.Requests.Search; using Microsoft.Extensions.Configuration; using MongoDB.Driver; namespace LCU.API.Internal.Document.PerformanceTest; public enum Indexing { Default, FieldSpecific, Wildcard } //todo it would be great to get the RU cost output [RPlotExporter] [ShortRunJob] public class IndexingStrategyPerformanceTest { [Params(Indexing.Default, Indexing.FieldSpecific, Indexing.Wildcard)] public Indexing Strategy { get; set; } private readonly IDocumentApi _api = ApiFactory.Api; private readonly DatabaseConfiguration _config = new(); public IndexingStrategyPerformanceTest() { var configBuilder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddJsonFile("appsettings.Development.json"); var baseConfig = configBuilder.Build(); var clientSecretCredentials = new ClientSecretCredential ( baseConfig["TenantId"], baseConfig["ClientId"], baseConfig["ClientSecret"] ); var vaultLocation = new Uri($"https://{baseConfig["AzureKeyVaultUrl"]}.vault.azure.net/"); configBuilder .AddAzureKeyVault(vaultLocation, clientSecretCredentials) .Build() .Bind("MongoDatabase", _config); } [GlobalSetup] public void GlobalSetup() { switch (Strategy) { case Indexing.Default: break; case Indexing.FieldSpecific: new MongoClient(_config.ConnectionString) .GetDatabase(_config.DatabaseName) .GetCollection(_config.DocumentContainerName) .Indexes .CreateOne ( new CreateIndexModel ( Builders.IndexKeys.Ascending(d => d.Name), new CreateIndexOptions { Name = nameof(DocumentInformation.Name) } ) ); break; case Indexing.Wildcard: new MongoClient(_config.ConnectionString) .GetDatabase(_config.DatabaseName) .GetCollection(_config.DocumentContainerName) .Indexes .CreateOne ( new CreateIndexModel ( Builders.IndexKeys.Wildcard(), new CreateIndexOptions { Name = "wildcard" } ) ); break; default: throw new ArgumentOutOfRangeException(); } _api.SpinUp(_config).Wait(); } [Benchmark] //todo report the RU cost in the benchmark public async Task DocumentSearch_ByName() { var filter = new DocumentInformationFilter { Name = "5498-SA" }; await _api.GetDocumentInformationByFilter(filter); } [GlobalCleanup] public void GlobalCleanup() { switch (Strategy) { case Indexing.Default: break; case Indexing.FieldSpecific: new MongoClient(_config.ConnectionString) .GetDatabase(_config.DatabaseName) .GetCollection(_config.DocumentContainerName) .Indexes .DropOne(nameof(DocumentInformation.Name)); break; case Indexing.Wildcard: new MongoClient(_config.ConnectionString) .GetDatabase(_config.DatabaseName) .GetCollection(_config.DocumentContainerName) .Indexes .DropOne("wildcard"); break; default: throw new ArgumentOutOfRangeException(); } } }