Skip to content

Commit 6c14cc6

Browse files
CopilotTheAngryByrd
andcommitted
Add benchmarking infrastructure to Library template
Co-authored-by: TheAngryByrd <[email protected]>
1 parent 2e5c97f commit 6c14cc6

File tree

8 files changed

+140
-0
lines changed

8 files changed

+140
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Benchmark
2+
3+
on:
4+
push:
5+
branches:
6+
- MyReleaseBranch
7+
pull_request:
8+
branches:
9+
- MyReleaseBranch
10+
11+
jobs:
12+
benchmark:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v5
16+
- name: Setup necessary dotnet SDKs
17+
uses: actions/setup-dotnet@v4
18+
with:
19+
dotnet-version: |
20+
8.x
21+
- name: Run benchmarks
22+
run: |
23+
chmod +x ./build.sh
24+
./build.sh RunBenchmarks
25+
env:
26+
CI: true
27+
CONFIGURATION: Release
28+
- name: Store benchmark result
29+
uses: benchmark-action/github-action-benchmark@v1
30+
with:
31+
name: MyLib.1 Benchmark
32+
tool: 'benchmarkdotnet'
33+
output-file-path: benchmarks/MyLib.1.Benchmarks/BenchmarkDotNet.Artifacts/results/*.json
34+
github-token: ${{ secrets.GITHUB_TOKEN }}
35+
auto-push: true
36+
# Show alert with commit comment on detecting possible performance regression
37+
alert-threshold: '200%'
38+
comment-on-alert: true
39+
fail-on-alert: true
40+
alert-comment-cc-users: '@MyGithubUsername'

Content/Library/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
<PackageVersion Include="Fake.BuildServer.GitHubActions" Version="$(FakeVersion)" />
3030
<PackageVersion Include="MSBuild.StructuredLogger" Version="2.2.441" />
3131
<PackageVersion Include="Argu" Version="6.0" />
32+
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
3233
</ItemGroup>
3334
</Project>

Content/Library/MyLib.1.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ACBEE43C
1111
EndProject
1212
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "MyLib.1.Tests", "tests\MyLib.1.Tests\MyLib.1.Tests.fsproj", "{1CA2E092-2320-451D-A4F0-9ED7C7C528CA}"
1313
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{B5B6CE6A-9B36-4F0F-8624-9B2CE14AC287}"
15+
EndProject
16+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "MyLib.1.Benchmarks", "benchmarks\MyLib.1.Benchmarks\MyLib.1.Benchmarks.fsproj", "{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}"
17+
EndProject
1418
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "build", "build\build.fsproj", "{40D2259D-991D-44C4-B45D-C88CE0710C23}"
1519
EndProject
1620
Global
@@ -62,9 +66,22 @@ Global
6266
{40D2259D-991D-44C4-B45D-C88CE0710C23}.Release|x64.Build.0 = Release|Any CPU
6367
{40D2259D-991D-44C4-B45D-C88CE0710C23}.Release|x86.ActiveCfg = Release|Any CPU
6468
{40D2259D-991D-44C4-B45D-C88CE0710C23}.Release|x86.Build.0 = Release|Any CPU
69+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
70+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
71+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Debug|x64.ActiveCfg = Debug|x64
72+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Debug|x64.Build.0 = Debug|x64
73+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Debug|x86.ActiveCfg = Debug|x86
74+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Debug|x86.Build.0 = Debug|x86
75+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
76+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Release|Any CPU.Build.0 = Release|Any CPU
77+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Release|x64.ActiveCfg = Release|x64
78+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Release|x64.Build.0 = Release|x64
79+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Release|x86.ActiveCfg = Release|x86
80+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F}.Release|x86.Build.0 = Release|x86
6581
EndGlobalSection
6682
GlobalSection(NestedProjects) = preSolution
6783
{5D30E174-2538-47AC-8443-318C8C5DC2C9} = {C397A34C-84F1-49E7-AEBC-2F9F2B196216}
6884
{1CA2E092-2320-451D-A4F0-9ED7C7C528CA} = {ACBEE43C-7A88-4FB1-9B06-DB064D22B29F}
85+
{E6C4F8E5-9B5D-4A12-8F1E-2A9C7D3E4B6F} = {B5B6CE6A-9B36-4F0F-8624-9B2CE14AC287}
6986
EndGlobalSection
7087
EndGlobal
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Project>
2+
<Import Project="../Directory.Build.props" />
3+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace MyLib._1.Benchmarks
2+
3+
open BenchmarkDotNet.Attributes
4+
open MyLib._1
5+
open System
6+
7+
[<MemoryDiagnoser>]
8+
type LibraryBenchmarks() =
9+
10+
let samplePerson = {
11+
Say.Person.Name = "Benchmark"
12+
Say.Person.FavoriteNumber = 42
13+
Say.Person.FavoriteColor = Say.FavoriteColor.Blue
14+
Say.Person.DateOfBirth = DateTimeOffset.Now
15+
}
16+
17+
[<Benchmark>]
18+
member _.HelloPerson() = Say.helloPerson samplePerson
19+
20+
[<Benchmark>]
21+
member _.AddNumbers() = Say.add 100 200
22+
23+
[<Benchmark>]
24+
member _.AddNumbersLoop() =
25+
let mutable result = 0
26+
27+
for i in 1..1000 do
28+
result <- Say.add result i
29+
30+
result
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<GenerateProgramFile>false</GenerateProgramFile>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="../../src/MyLib.1/MyLib.1.fsproj" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<PackageReference Include="BenchmarkDotNet" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<Compile Include="Library.Benchmarks.fs" />
16+
<Compile Include="Program.fs" />
17+
</ItemGroup>
18+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Program
2+
3+
open BenchmarkDotNet.Running
4+
open MyLib._1.Benchmarks
5+
6+
[<EntryPoint>]
7+
let main args =
8+
BenchmarkRunner.Run<LibraryBenchmarks>()
9+
|> ignore
10+
11+
0

Content/Library/build/build.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,22 @@ let watchTests _ =
505505

506506
cancelEvent.Cancel <- true
507507

508+
let benchmarksGlob =
509+
rootDirectory
510+
</> "benchmarks/**/*.??proj"
511+
512+
let runBenchmarks _ =
513+
!!benchmarksGlob
514+
|> Seq.iter (fun proj ->
515+
DotNet.exec id "run" $"--project \"{proj}\" --configuration Release"
516+
|> fun result ->
517+
if
518+
result.ExitCode
519+
<> 0
520+
then
521+
failwithf "Benchmark failed for project %s" proj
522+
)
523+
508524
let generateAssemblyInfo _ =
509525

510526
let (|Fsproj|Csproj|Vbproj|) (projFileName: string) =
@@ -727,6 +743,7 @@ let initTargets () =
727743
Target.create "GenerateCoverageReport" generateCoverageReport
728744
Target.create "ShowCoverageReport" showCoverageReport
729745
Target.create "WatchTests" watchTests
746+
Target.create "RunBenchmarks" runBenchmarks
730747
Target.create "GenerateAssemblyInfo" generateAssemblyInfo
731748
Target.create "DotnetPack" dotnetPack
732749
Target.create "SourceLinkTest" sourceLinkTest
@@ -782,6 +799,9 @@ let initTargets () =
782799
"DotnetBuild"
783800
==>! "WatchDocs"
784801

802+
"DotnetBuild"
803+
==>! "RunBenchmarks"
804+
785805
"DotnetTest"
786806
==> "GenerateCoverageReport"
787807
==>! "ShowCoverageReport"

0 commit comments

Comments
 (0)