Skip to content

Commit e833025

Browse files
committed
Add benchmark project
LibGit2Sharp.Benchmarks uses BenchmarkDotNet to measure ObjectId construction, commit walks and diffs, reporting allocations for each. It targets net472 and net11.0, so CI now installs the .NET 11 SDK for the build and Windows/macOS test jobs.
1 parent cbb949d commit e833025

9 files changed

Lines changed: 254 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install .NET SDK
2222
uses: actions/setup-dotnet@v4.0.0
2323
with:
24-
dotnet-version: 9.0.x
24+
dotnet-version: 11.0.x
2525
- name: Build
2626
run: dotnet build LibGit2Sharp.sln --configuration Release
2727
- name: Upload packages
@@ -62,6 +62,7 @@ jobs:
6262
uses: actions/setup-dotnet@v4.0.0
6363
with:
6464
dotnet-version: |
65+
11.0.x
6566
9.0.x
6667
8.0.x
6768
- name: Run ${{ matrix.tfm }} tests
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using BenchmarkDotNet.Attributes;
2+
3+
namespace LibGit2Sharp.Benchmarks
4+
{
5+
/// <summary>
6+
/// Every commit yielded by the walk builds an <see cref="ObjectId"/> from native memory.
7+
/// </summary>
8+
[MemoryDiagnoser]
9+
public class CommitWalkBenchmarks
10+
{
11+
TempRepository repo;
12+
13+
[Params(1000)]
14+
public int CommitCount { get; set; }
15+
16+
[GlobalSetup]
17+
public void Setup()
18+
{
19+
repo = new TempRepository();
20+
21+
for (var i = 0; i < CommitCount; i++)
22+
{
23+
repo.Commit("file.txt", i.ToString(), $"Commit {i}");
24+
}
25+
}
26+
27+
[GlobalCleanup]
28+
public void Cleanup()
29+
{
30+
repo.Dispose();
31+
}
32+
33+
[Benchmark]
34+
public int WalkCommits()
35+
{
36+
var count = 0;
37+
38+
foreach (var commit in repo.Repository.Commits)
39+
{
40+
if (commit.Id != null)
41+
{
42+
count++;
43+
}
44+
}
45+
46+
return count;
47+
}
48+
}
49+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Text;
2+
using BenchmarkDotNet.Attributes;
3+
4+
namespace LibGit2Sharp.Benchmarks
5+
{
6+
/// <summary>
7+
/// Diffs two versions of a file where every other line changed, so the
8+
/// per-line diff callbacks dominate.
9+
/// </summary>
10+
[MemoryDiagnoser]
11+
public class DiffBenchmarks
12+
{
13+
TempRepository repo;
14+
Commit oldCommit;
15+
Commit newCommit;
16+
Blob oldBlob;
17+
Blob newBlob;
18+
19+
[Params(10000)]
20+
public int LineCount { get; set; }
21+
22+
[GlobalSetup]
23+
public void Setup()
24+
{
25+
repo = new TempRepository();
26+
oldCommit = repo.Commit("file.txt", BuildContent(false), "Old");
27+
newCommit = repo.Commit("file.txt", BuildContent(true), "New");
28+
oldBlob = (Blob)oldCommit["file.txt"].Target;
29+
newBlob = (Blob)newCommit["file.txt"].Target;
30+
}
31+
32+
[GlobalCleanup]
33+
public void Cleanup()
34+
{
35+
repo.Dispose();
36+
}
37+
38+
string BuildContent(bool modified)
39+
{
40+
var builder = new StringBuilder();
41+
42+
for (var i = 0; i < LineCount; i++)
43+
{
44+
if (modified && i % 2 == 0)
45+
{
46+
builder.Append("Changed line ").Append(i).Append('\n');
47+
continue;
48+
}
49+
50+
builder.Append("Original line ").Append(i).Append('\n');
51+
}
52+
53+
return builder.ToString();
54+
}
55+
56+
[Benchmark]
57+
public int PatchBetweenTrees()
58+
{
59+
var patch = repo.Repository.Diff.Compare<Patch>(oldCommit.Tree, newCommit.Tree);
60+
return patch.LinesAdded;
61+
}
62+
63+
[Benchmark]
64+
public int ContentChangesBetweenBlobs()
65+
{
66+
var changes = repo.Repository.Diff.Compare(oldBlob, newBlob);
67+
return changes.LinesAdded;
68+
}
69+
}
70+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks Condition="'$(TargetFrameworks)'==''">net472;net11.0</TargetFrameworks>
6+
<LangVersion>12.0</LangVersion>
7+
<IsPackable>false</IsPackable>
8+
<Optimize>true</Optimize>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Linq;
2+
using BenchmarkDotNet.Attributes;
3+
4+
namespace LibGit2Sharp.Benchmarks
5+
{
6+
[MemoryDiagnoser]
7+
public class ObjectIdBenchmarks
8+
{
9+
const string sha = "8496071c1b46c854b31185ea97743be6a8774479";
10+
11+
readonly byte[] rawId = new ObjectId(sha).RawId.ToArray();
12+
13+
[Benchmark]
14+
public ObjectId FromSha()
15+
{
16+
return new ObjectId(sha);
17+
}
18+
19+
[Benchmark]
20+
public ObjectId TryParse()
21+
{
22+
ObjectId.TryParse(sha, out var id);
23+
return id;
24+
}
25+
26+
[Benchmark]
27+
public ObjectId FromRawId()
28+
{
29+
return new ObjectId(rawId);
30+
}
31+
}
32+
}

‎LibGit2Sharp.Benchmarks/Program.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using BenchmarkDotNet.Running;
2+
3+
namespace LibGit2Sharp.Benchmarks
4+
{
5+
public static class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
10+
}
11+
}
12+
}

‎LibGit2Sharp.Benchmarks/README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# LibGit2Sharp.Benchmarks
2+
3+
[BenchmarkDotNet](https://benchmarkdotnet.org/) benchmarks for LibGit2Sharp. Every benchmark reports allocations.
4+
5+
The diff and commit walk benchmarks build a throwaway repository under the temp folder in their setup and delete it afterwards.
6+
7+
Run all benchmarks on .NET 11:
8+
9+
```
10+
dotnet run -c Release -f net11.0 --project LibGit2Sharp.Benchmarks -- --filter *
11+
```
12+
13+
Run on .NET Framework (Windows only):
14+
15+
```
16+
dotnet run -c Release -f net472 --project LibGit2Sharp.Benchmarks -- --filter *
17+
```
18+
19+
Pick benchmarks with `--filter`, for example `--filter *Diff*`. For a quicker, less precise run, add `--job short`.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace LibGit2Sharp.Benchmarks
5+
{
6+
/// <summary>
7+
/// A throwaway repository on disk, deleted on dispose.
8+
/// </summary>
9+
sealed class TempRepository : IDisposable
10+
{
11+
static readonly Signature signature = new Signature("Benchmark", "benchmark@example.com", new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.Zero));
12+
13+
readonly string directory;
14+
15+
public TempRepository()
16+
{
17+
directory = Path.Combine(Path.GetTempPath(), "LibGit2Sharp.Benchmarks", Guid.NewGuid().ToString("N"));
18+
Directory.CreateDirectory(directory);
19+
Repository.Init(directory);
20+
Repository = new Repository(directory);
21+
}
22+
23+
public Repository Repository { get; }
24+
25+
public Commit Commit(string fileName, string content, string message)
26+
{
27+
File.WriteAllText(Path.Combine(directory, fileName), content);
28+
Commands.Stage(Repository, fileName);
29+
return Repository.Commit(message, signature, signature);
30+
}
31+
32+
public void Dispose()
33+
{
34+
Repository.Dispose();
35+
36+
// libgit2 writes object files as read-only, which blocks Directory.Delete on Windows.
37+
foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories))
38+
{
39+
File.SetAttributes(file, FileAttributes.Normal);
40+
}
41+
42+
Directory.Delete(directory, true);
43+
}
44+
}
45+
}

‎LibGit2Sharp.sln‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeLibraryLoadTestApp.x8
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeLibraryLoadTestApp.x64", "NativeLibraryLoadTestApp\x64\NativeLibraryLoadTestApp.x64.csproj", "{5C55175D-6A1F-4C51-B791-BF7DD00124EE}"
2121
EndProject
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibGit2Sharp.Benchmarks", "LibGit2Sharp.Benchmarks\LibGit2Sharp.Benchmarks.csproj", "{918C6DA8-750B-499D-8E65-225767BF8704}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -41,6 +43,10 @@ Global
4143
{5C55175D-6A1F-4C51-B791-BF7DD00124EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
4244
{5C55175D-6A1F-4C51-B791-BF7DD00124EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
4345
{5C55175D-6A1F-4C51-B791-BF7DD00124EE}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{918C6DA8-750B-499D-8E65-225767BF8704}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{918C6DA8-750B-499D-8E65-225767BF8704}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{918C6DA8-750B-499D-8E65-225767BF8704}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{918C6DA8-750B-499D-8E65-225767BF8704}.Release|Any CPU.Build.0 = Release|Any CPU
4450
EndGlobalSection
4551
GlobalSection(SolutionProperties) = preSolution
4652
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)