|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Running; |
| 3 | +using ContosoOnlineStore; |
| 4 | +using ContosoOnlineStore.Configuration; |
| 5 | +using ContosoOnlineStore.Services; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using Microsoft.Extensions.Options; |
| 9 | + |
| 10 | +namespace ContosoOnlineStore.Benchmarks |
| 11 | +{ |
| 12 | + [MemoryDiagnoser] |
| 13 | + [SimpleJob] |
| 14 | + public class OrderProcessingBenchmarks |
| 15 | + { |
| 16 | + private IOrderProcessor? _orderProcessor; |
| 17 | + private IProductCatalog? _catalog; |
| 18 | + private IInventoryManager? _inventory; |
| 19 | + private Order? _testOrder; |
| 20 | + private ServiceProvider? _serviceProvider; |
| 21 | + |
| 22 | + [GlobalSetup] |
| 23 | + public void Setup() |
| 24 | + { |
| 25 | + var services = new ServiceCollection(); |
| 26 | + |
| 27 | + // Configure logging |
| 28 | + services.AddLogging(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Warning)); |
| 29 | + |
| 30 | + // Configure settings |
| 31 | + var appSettings = new AppSettings(); |
| 32 | + services.AddSingleton(Options.Create(appSettings)); |
| 33 | + |
| 34 | + // Register services |
| 35 | + services.AddSingleton<ISecurityValidationService, SecurityValidationService>(); |
| 36 | + services.AddSingleton<IProductCatalog, ProductCatalog>(); |
| 37 | + services.AddSingleton<IInventoryManager, InventoryManager>(); |
| 38 | + services.AddSingleton<IEmailService, EmailService>(); |
| 39 | + services.AddSingleton<IOrderProcessor, OrderProcessor>(); |
| 40 | + |
| 41 | + _serviceProvider = services.BuildServiceProvider(); |
| 42 | + |
| 43 | + _catalog = _serviceProvider.GetRequiredService<IProductCatalog>(); |
| 44 | + _inventory = _serviceProvider.GetRequiredService<IInventoryManager>(); |
| 45 | + _orderProcessor = _serviceProvider.GetRequiredService<IOrderProcessor>(); |
| 46 | + |
| 47 | + // Create test order |
| 48 | + _testOrder = new Order("[email protected]", "123 Test Street, Test City, WA 98101"); |
| 49 | + _testOrder.AddItem(new OrderItem(1, 2)); |
| 50 | + _testOrder.AddItem(new OrderItem(5, 1)); |
| 51 | + _testOrder.AddItem(new OrderItem(10, 3)); |
| 52 | + } |
| 53 | + |
| 54 | + [Benchmark] |
| 55 | + public decimal CalculateOrderTotal() |
| 56 | + { |
| 57 | + return _orderProcessor!.CalculateOrderTotal(_testOrder!); |
| 58 | + } |
| 59 | + |
| 60 | + [Benchmark] |
| 61 | + public async Task<bool> ValidateOrderAsync() |
| 62 | + { |
| 63 | + return await _orderProcessor!.ValidateOrderAsync(_testOrder!); |
| 64 | + } |
| 65 | + |
| 66 | + [Benchmark] |
| 67 | + public List<Product> GetAllProducts() |
| 68 | + { |
| 69 | + return _catalog!.GetAllProducts(); |
| 70 | + } |
| 71 | + |
| 72 | + [Benchmark] |
| 73 | + public Product? GetProductById() |
| 74 | + { |
| 75 | + return _catalog!.GetProductById(5); |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark] |
| 79 | + public List<Product> SearchProducts() |
| 80 | + { |
| 81 | + return _catalog!.SearchProducts("phone"); |
| 82 | + } |
| 83 | + |
| 84 | + [Benchmark] |
| 85 | + public Dictionary<int, int> GetLowStockProducts() |
| 86 | + { |
| 87 | + return _inventory!.GetLowStockProducts(50); |
| 88 | + } |
| 89 | + |
| 90 | + [GlobalCleanup] |
| 91 | + public void Cleanup() |
| 92 | + { |
| 93 | + (_serviceProvider as IDisposable)?.Dispose(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public class BenchmarkRunner |
| 98 | + { |
| 99 | + public static void RunBenchmarks() |
| 100 | + { |
| 101 | + Console.WriteLine("Starting performance benchmarks..."); |
| 102 | + var summary = BenchmarkDotNet.Running.BenchmarkRunner.Run<OrderProcessingBenchmarks>(); |
| 103 | + Console.WriteLine("Benchmarks completed!"); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments