Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Install .NET SDK
uses: actions/setup-dotnet@v4.0.0
with:
dotnet-version: 9.0.x
dotnet-version: 11.0.x
- name: Build
run: dotnet build LibGit2Sharp.sln --configuration Release
- name: Upload packages
Expand Down Expand Up @@ -62,6 +62,7 @@ jobs:
uses: actions/setup-dotnet@v4.0.0
with:
dotnet-version: |
11.0.x
9.0.x
8.0.x
- name: Run ${{ matrix.tfm }} tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Thumbs.db
*.ncb
*.suo
.vs/
.idea/
*.sln.ide/
*.tlb
*.tlh
Expand Down
49 changes: 49 additions & 0 deletions LibGit2Sharp.Benchmarks/CommitWalkBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using BenchmarkDotNet.Attributes;

namespace LibGit2Sharp.Benchmarks
{
/// <summary>
/// Every commit yielded by the walk builds an <see cref="ObjectId"/> from native memory.
/// </summary>
[MemoryDiagnoser]
public class CommitWalkBenchmarks
{
TempRepository repo;

[Params(1000)]
public int CommitCount { get; set; }

[GlobalSetup]
public void Setup()
{
repo = new TempRepository();

for (var i = 0; i < CommitCount; i++)
{
repo.Commit("file.txt", i.ToString(), $"Commit {i}");
}
}

[GlobalCleanup]
public void Cleanup()
{
repo.Dispose();
}

[Benchmark]
public int WalkCommits()
{
var count = 0;

foreach (var commit in repo.Repository.Commits)
{
if (commit.Id != null)
{
count++;
}
}

return count;
}
}
}
70 changes: 70 additions & 0 deletions LibGit2Sharp.Benchmarks/DiffBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Text;
using BenchmarkDotNet.Attributes;

namespace LibGit2Sharp.Benchmarks
{
/// <summary>
/// Diffs two versions of a file where every other line changed, so the
/// per-line diff callbacks dominate.
/// </summary>
[MemoryDiagnoser]
public class DiffBenchmarks
{
TempRepository repo;
Commit oldCommit;
Commit newCommit;
Blob oldBlob;
Blob newBlob;

[Params(10000)]
public int LineCount { get; set; }

[GlobalSetup]
public void Setup()
{
repo = new TempRepository();
oldCommit = repo.Commit("file.txt", BuildContent(false), "Old");
newCommit = repo.Commit("file.txt", BuildContent(true), "New");
oldBlob = (Blob)oldCommit["file.txt"].Target;
newBlob = (Blob)newCommit["file.txt"].Target;
}

[GlobalCleanup]
public void Cleanup()
{
repo.Dispose();
}

string BuildContent(bool modified)
{
var builder = new StringBuilder();

for (var i = 0; i < LineCount; i++)
{
if (modified && i % 2 == 0)
{
builder.Append("Changed line ").Append(i).Append('\n');
continue;
}

builder.Append("Original line ").Append(i).Append('\n');
}

return builder.ToString();
}

[Benchmark]
public int PatchBetweenTrees()
{
var patch = repo.Repository.Diff.Compare<Patch>(oldCommit.Tree, newCommit.Tree);
return patch.LinesAdded;
}

[Benchmark]
public int ContentChangesBetweenBlobs()
{
var changes = repo.Repository.Diff.Compare(oldBlob, newBlob);
return changes.LinesAdded;
}
}
}
19 changes: 19 additions & 0 deletions LibGit2Sharp.Benchmarks/LibGit2Sharp.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks Condition="'$(TargetFrameworks)'==''">net472;net11.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<IsPackable>false</IsPackable>
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj" />
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions LibGit2Sharp.Benchmarks/LookupBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Linq;
using BenchmarkDotNet.Attributes;

namespace LibGit2Sharp.Benchmarks
{
/// <summary>
/// Every lookup passes the id to libgit2 as a ref GitOid.
/// </summary>
[MemoryDiagnoser]
public class LookupBenchmarks
{
TempRepository repo;
ObjectId[] ids;

[Params(1000)]
public int CommitCount { get; set; }

[GlobalSetup]
public void Setup()
{
repo = new TempRepository();

for (var i = 0; i < CommitCount; i++)
{
repo.Commit("file.txt", i.ToString(), $"Commit {i}");
}

ids = repo.Repository.Commits.Select(c => c.Id).ToArray();
}

[GlobalCleanup]
public void Cleanup()
{
repo.Dispose();
}

[Benchmark]
public int LookupCommits()
{
var count = 0;

foreach (var id in ids)
{
if (repo.Repository.Lookup<Commit>(id) != null)
{
count++;
}
}

return count;
}
}
}
32 changes: 32 additions & 0 deletions LibGit2Sharp.Benchmarks/ObjectIdBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Linq;
using BenchmarkDotNet.Attributes;

namespace LibGit2Sharp.Benchmarks
{
[MemoryDiagnoser]
public class ObjectIdBenchmarks
{
const string sha = "8496071c1b46c854b31185ea97743be6a8774479";

readonly byte[] rawId = new ObjectId(sha).RawId.ToArray();

[Benchmark]
public ObjectId FromSha()
{
return new ObjectId(sha);
}

[Benchmark]
public ObjectId TryParse()
{
ObjectId.TryParse(sha, out var id);
return id;
}

[Benchmark]
public ObjectId FromRawId()
{
return new ObjectId(rawId);
}
}
}
12 changes: 12 additions & 0 deletions LibGit2Sharp.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BenchmarkDotNet.Running;

namespace LibGit2Sharp.Benchmarks
{
public static class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
}
19 changes: 19 additions & 0 deletions LibGit2Sharp.Benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# LibGit2Sharp.Benchmarks

[BenchmarkDotNet](https://benchmarkdotnet.org/) benchmarks for LibGit2Sharp. Every benchmark reports allocations.

The diff, commit walk and lookup benchmarks build a throwaway repository under the temp folder in their setup and delete it afterwards.

Run all benchmarks on .NET 11:

```
dotnet run -c Release -f net11.0 --project LibGit2Sharp.Benchmarks -- --filter *
```

Run on .NET Framework (Windows only):

```
dotnet run -c Release -f net472 --project LibGit2Sharp.Benchmarks -- --filter *
```

Pick benchmarks with `--filter`, for example `--filter *Diff*`. For a quicker, less precise run, add `--job short`.
45 changes: 45 additions & 0 deletions LibGit2Sharp.Benchmarks/TempRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.IO;

namespace LibGit2Sharp.Benchmarks
{
/// <summary>
/// A throwaway repository on disk, deleted on dispose.
/// </summary>
sealed class TempRepository : IDisposable
{
static readonly Signature signature = new Signature("Benchmark", "benchmark@example.com", new DateTimeOffset(2024, 1, 1, 0, 0, 0, TimeSpan.Zero));

readonly string directory;

public TempRepository()
{
directory = Path.Combine(Path.GetTempPath(), "LibGit2Sharp.Benchmarks", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(directory);
Repository.Init(directory);
Repository = new Repository(directory);
}

public Repository Repository { get; }

public Commit Commit(string fileName, string content, string message)
{
File.WriteAllText(Path.Combine(directory, fileName), content);
Commands.Stage(Repository, fileName);
return Repository.Commit(message, signature, signature);
}

public void Dispose()
{
Repository.Dispose();

// libgit2 writes object files as read-only, which blocks Directory.Delete on Windows.
foreach (var file in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories))
{
File.SetAttributes(file, FileAttributes.Normal);
}

Directory.Delete(directory, true);
}
}
}
6 changes: 6 additions & 0 deletions LibGit2Sharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeLibraryLoadTestApp.x8
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeLibraryLoadTestApp.x64", "NativeLibraryLoadTestApp\x64\NativeLibraryLoadTestApp.x64.csproj", "{5C55175D-6A1F-4C51-B791-BF7DD00124EE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibGit2Sharp.Benchmarks", "LibGit2Sharp.Benchmarks\LibGit2Sharp.Benchmarks.csproj", "{918C6DA8-750B-499D-8E65-225767BF8704}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +43,10 @@ Global
{5C55175D-6A1F-4C51-B791-BF7DD00124EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C55175D-6A1F-4C51-B791-BF7DD00124EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C55175D-6A1F-4C51-B791-BF7DD00124EE}.Release|Any CPU.Build.0 = Release|Any CPU
{918C6DA8-750B-499D-8E65-225767BF8704}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{918C6DA8-750B-499D-8E65-225767BF8704}.Debug|Any CPU.Build.0 = Debug|Any CPU
{918C6DA8-750B-499D-8E65-225767BF8704}.Release|Any CPU.ActiveCfg = Release|Any CPU
{918C6DA8-750B-499D-8E65-225767BF8704}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading