Skip to content

Commit

Permalink
WIP adding BenchmarkDotNet
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch-b committed Nov 2, 2024
1 parent ba7d497 commit 7aa80cf
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
"cwd": "${workspaceFolder}/src/ConsoleApp",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Benchmark",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build:release",
"program": "${workspaceFolder}/src/ConsoleApp/bin/Release/net9.0/ConsoleApp.dll",
"args": [ "--benchmark", "true" ],
"cwd": "${workspaceFolder}/src/ConsoleApp",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
14 changes: 14 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "build:release",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/dotnet-basic-console.sln",
"-c",
"Release",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
}
]
}
29 changes: 29 additions & 0 deletions src/ConsoleApp/BenchmarkService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BenchmarkDotNet.Attributes;
using ConsoleApp.Models.Configuration;
using ConsoleApp.Services;
using Microsoft.Extensions.Options;

namespace ConsoleApp;

public class BenchmarkService(
IDemoService demoService,
IOptions<ConsoleAppSettings> options)
{
private readonly IDemoService _demoService = demoService;
private readonly IOptions<ConsoleAppSettings> _options = options;

[GlobalSetup]
public void Setup()
{
// initialize things in class if needed
}

[Benchmark]
public void WriteWelcomeMessage()
{
for (var i = 0; i < 100; i++)
{
_demoService.WriteWelcomeMessage();
}
}
}
12 changes: 11 additions & 1 deletion src/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Identity.Client;
using Serilog;
using OpenTelemetry.Logs;
using BenchmarkDotNet.Running;

var builder = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, configBuilder) =>
Expand Down Expand Up @@ -54,4 +55,13 @@

using var host = builder.Build();

await host.RunAsync();
var configuration = host.Services.GetRequiredService<IConfiguration>();

if (configuration.GetValue<bool?>("benchmark") == true)
{
BenchmarkRunner.Run<BenchmarkService>();
}
else
{
await host.RunAsync();
}
4 changes: 2 additions & 2 deletions src/ConsoleApp/Services/ClientCredentialService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace ConsoleApp.Services;

internal interface IClientCredentialService
public interface IClientCredentialService
{
Task<AuthenticationResult?> GetAuthenticationResult(IEnumerable<string>? scopes = null);
Task<string> GetAccessToken(IEnumerable<string>? scopes = null);
}

internal class ClientCredentialService(ILogger<ClientCredentialService> logger,
public class ClientCredentialService(ILogger<ClientCredentialService> logger,
IOptions<ConfidentialClientApplicationOptions> confidentialClientApplicationOptions) : IClientCredentialService
{
private readonly ILogger<ClientCredentialService> _logger = logger;
Expand Down
4 changes: 2 additions & 2 deletions src/ConsoleApp/Services/DemoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace ConsoleApp.Services;

internal interface IDemoService
public interface IDemoService
{
void WriteWelcomeMessage();
Task<string> MakeGraphApiCall();
}

internal class DemoService(ILogger<DemoService> logger,
public class DemoService(ILogger<DemoService> logger,
IClientCredentialService clientCredentialService,
IOptions<ConsoleAppSettings> consoleAppSettings,
IHttpClientFactory httpClientFactory) : IDemoService
Expand Down

0 comments on commit 7aa80cf

Please sign in to comment.