Skip to content

Commit b10374a

Browse files
committed
Ex10 GHCopilot updates to code project
1 parent bbf1c5f commit b10374a

28 files changed

+2890
-4
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ContosoOnlineStore.Configuration
4+
{
5+
public class AppSettings
6+
{
7+
[Range(1, 1000)]
8+
public int MaxOrderItems { get; set; } = 50;
9+
10+
[Range(100, 30000)]
11+
public int EmailTimeoutMs { get; set; } = 2000;
12+
13+
public bool EnableDetailedLogging { get; set; } = true;
14+
15+
public SecuritySettings SecuritySettings { get; set; } = new();
16+
17+
public PerformanceSettings PerformanceSettings { get; set; } = new();
18+
}
19+
20+
public class SecuritySettings
21+
{
22+
[Range(0.01, 100000.00)]
23+
public decimal MaxProductPrice { get; set; } = 10000.00m;
24+
25+
[Range(0.01, 1000.00)]
26+
public decimal MinProductPrice { get; set; } = 0.01m;
27+
28+
public bool AllowNegativeInventory { get; set; } = false;
29+
}
30+
31+
public class PerformanceSettings
32+
{
33+
[Range(1, 1440)]
34+
public int CacheExpirationMinutes { get; set; } = 30;
35+
36+
[Range(1000, 60000)]
37+
public int DatabaseTimeoutMs { get; set; } = 5000;
38+
39+
[Range(1, 1000)]
40+
public int MaxConcurrentOrders { get; set; } = 100;
41+
}
42+
}

DownloadableCodeProjects/standalone-lab-projects/implement-performance-profiling/ContosoOnlineStore/ContosoOnlineStore.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,23 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
16+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
17+
<PackageReference Include="xunit" Version="2.4.2" />
18+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
19+
<PackageReference Include="Moq" Version="4.20.69" />
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<None Update="appsettings.json">
25+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26+
</None>
27+
</ItemGroup>
28+
1029
</Project>

0 commit comments

Comments
 (0)