Skip to content

Commit 7aa80cf

Browse files
committed
WIP adding BenchmarkDotNet
1 parent ba7d497 commit 7aa80cf

6 files changed

Lines changed: 69 additions & 5 deletions

File tree

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
"cwd": "${workspaceFolder}/src/ConsoleApp",
1212
"stopAtEntry": false,
1313
"console": "internalConsole"
14+
},
15+
{
16+
"name": "Benchmark",
17+
"type": "coreclr",
18+
"request": "launch",
19+
"preLaunchTask": "build:release",
20+
"program": "${workspaceFolder}/src/ConsoleApp/bin/Release/net9.0/ConsoleApp.dll",
21+
"args": [ "--benchmark", "true" ],
22+
"cwd": "${workspaceFolder}/src/ConsoleApp",
23+
"stopAtEntry": false,
24+
"console": "internalConsole"
1425
}
1526
]
1627
}

.vscode/tasks.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
"/consoleloggerparameters:NoSummary;ForceNoAlign"
1313
],
1414
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "build:release",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"build",
22+
"${workspaceFolder}/src/dotnet-basic-console.sln",
23+
"-c",
24+
"Release",
25+
"/property:GenerateFullPaths=true",
26+
"/consoleloggerparameters:NoSummary;ForceNoAlign"
27+
],
28+
"problemMatcher": "$msCompile"
1529
}
1630
]
1731
}

src/ConsoleApp/BenchmarkService.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using BenchmarkDotNet.Attributes;
2+
using ConsoleApp.Models.Configuration;
3+
using ConsoleApp.Services;
4+
using Microsoft.Extensions.Options;
5+
6+
namespace ConsoleApp;
7+
8+
public class BenchmarkService(
9+
IDemoService demoService,
10+
IOptions<ConsoleAppSettings> options)
11+
{
12+
private readonly IDemoService _demoService = demoService;
13+
private readonly IOptions<ConsoleAppSettings> _options = options;
14+
15+
[GlobalSetup]
16+
public void Setup()
17+
{
18+
// initialize things in class if needed
19+
}
20+
21+
[Benchmark]
22+
public void WriteWelcomeMessage()
23+
{
24+
for (var i = 0; i < 100; i++)
25+
{
26+
_demoService.WriteWelcomeMessage();
27+
}
28+
}
29+
}

src/ConsoleApp/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Identity.Client;
1010
using Serilog;
1111
using OpenTelemetry.Logs;
12+
using BenchmarkDotNet.Running;
1213

1314
var builder = Host.CreateDefaultBuilder(args)
1415
.ConfigureAppConfiguration((hostContext, configBuilder) =>
@@ -54,4 +55,13 @@
5455

5556
using var host = builder.Build();
5657

57-
await host.RunAsync();
58+
var configuration = host.Services.GetRequiredService<IConfiguration>();
59+
60+
if (configuration.GetValue<bool?>("benchmark") == true)
61+
{
62+
BenchmarkRunner.Run<BenchmarkService>();
63+
}
64+
else
65+
{
66+
await host.RunAsync();
67+
}

src/ConsoleApp/Services/ClientCredentialService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace ConsoleApp.Services;
66

7-
internal interface IClientCredentialService
7+
public interface IClientCredentialService
88
{
99
Task<AuthenticationResult?> GetAuthenticationResult(IEnumerable<string>? scopes = null);
1010
Task<string> GetAccessToken(IEnumerable<string>? scopes = null);
1111
}
1212

13-
internal class ClientCredentialService(ILogger<ClientCredentialService> logger,
13+
public class ClientCredentialService(ILogger<ClientCredentialService> logger,
1414
IOptions<ConfidentialClientApplicationOptions> confidentialClientApplicationOptions) : IClientCredentialService
1515
{
1616
private readonly ILogger<ClientCredentialService> _logger = logger;

src/ConsoleApp/Services/DemoService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace ConsoleApp.Services;
88

9-
internal interface IDemoService
9+
public interface IDemoService
1010
{
1111
void WriteWelcomeMessage();
1212
Task<string> MakeGraphApiCall();
1313
}
1414

15-
internal class DemoService(ILogger<DemoService> logger,
15+
public class DemoService(ILogger<DemoService> logger,
1616
IClientCredentialService clientCredentialService,
1717
IOptions<ConsoleAppSettings> consoleAppSettings,
1818
IHttpClientFactory httpClientFactory) : IDemoService

0 commit comments

Comments
 (0)