-
-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caching git heights #801
Open
AArnott
wants to merge
10
commits into
dotnet:main
Choose a base branch
from
djluck:version-caching-perf-improv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Caching git heights #801
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
86213c2
## Summary
e82e23a
Fixing required documentation errors
9251308
Constructor must be public
fad06f0
Removing test SDK to see if this is cause for broken build
3fe9ec6
Simplifying logic around discarding cache height when base version do…
234625e
Fixing doco issue
85eef33
Testing adding a reference to compiler.unsafe to see if it will help …
7202d1b
More messing around with package versions- this time System.Numerics
670f6e7
Fixing a bug that meant the incorrect height would be returned for co…
2b58c63
Adding support for the single version file scenario- git heights are …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
@@ -410,6 +411,123 @@ public void GetVersionHeight_VeryLongHistory() | |
|
||
this.Repo.GetVersionHeight(); | ||
} | ||
|
||
[Fact] | ||
public void GetVersionHeight_CachingPerf() | ||
{ | ||
const string repoRelativeSubDirectory = "subdir"; | ||
|
||
var semanticVersion1 = SemanticVersion.Parse("1.0"); | ||
this.WriteVersionFile( | ||
new VersionOptions { Version = semanticVersion1 }, | ||
repoRelativeSubDirectory); | ||
|
||
// Add a large volume of commits where the versison file hasn't been bumped- key thing is that when we try to determine the git height, | ||
// we have a lot of commits to walk | ||
const int numCommitsToTraverse = 300; | ||
MeasureRuntime( | ||
() => | ||
{ | ||
for (int i = 0; i < numCommitsToTraverse; i++) | ||
this.Repo.Commit($"Test commit #{i}", this.Signer, this.Signer, new CommitOptions {AllowEmptyCommit = true}); | ||
|
||
return -1; | ||
}, | ||
$"Add {numCommitsToTraverse} commits to the git history" | ||
); | ||
|
||
// First calculation of height will not have the benefit of the cache, will be slow | ||
var (initialHeight, initialTime) = MeasureRuntime(() => this.Repo.Head.GetVersionHeight(repoRelativeSubDirectory), "get the initial (uncached) version height"); | ||
Assert.True(File.Exists(Path.Combine(RepoPath, repoRelativeSubDirectory, GitHeightCache.CacheFileName))); | ||
|
||
// Second calculation of height should be able to use the cache file generated by the previous calculation, even though it's for a child of the original path | ||
var (cachedHeight, cachedTime) = MeasureRuntime(() => this.Repo.Head.GetVersionHeight(Path.Combine(repoRelativeSubDirectory, "new_sub_dir")), "get the version height for the unmodified repository (should be cached)"); | ||
|
||
// Want to see at least 20x perf increase | ||
Assert.InRange(cachedTime, TimeSpan.Zero, TimeSpan.FromTicks(initialTime.Ticks / 20)); | ||
Assert.Equal(initialHeight, numCommitsToTraverse + 1); | ||
Assert.Equal(initialHeight, cachedHeight); | ||
|
||
// Adding an additional commit and then getting the height should only involve walking a single commit | ||
this.Repo.Commit($"Final Test commit", this.Signer, this.Signer, new CommitOptions {AllowEmptyCommit = true}); | ||
// Third calculation of height should be able to use the cache file for the first set of commits but walk the last commit to determine the height | ||
(cachedHeight, cachedTime) = MeasureRuntime(() => this.Repo.Head.GetVersionHeight(repoRelativeSubDirectory), "get the version height for the modified repository (should partially use the cache)"); | ||
Assert.Equal(cachedHeight, numCommitsToTraverse + 2); | ||
// We'd expect a less dramatic perf increase this time but should still be significant | ||
Assert.InRange(cachedTime, TimeSpan.Zero, TimeSpan.FromTicks(initialTime.Ticks / 10)); | ||
} | ||
|
||
[Fact] | ||
public void GetVersionHeight_CachingMultipleVersions() | ||
{ | ||
// TODO probably should make this test more vigorous | ||
const string repoRelativeSubDirectory1 = "subdir1", repoRelativeSubDirectory2 = "subdir2";; | ||
|
||
this.WriteVersionFile( | ||
new VersionOptions { Version = SemanticVersion.Parse("1.1") }, | ||
repoRelativeSubDirectory1); | ||
|
||
this.WriteVersionFile( | ||
new VersionOptions { Version = SemanticVersion.Parse("1.2") }, | ||
repoRelativeSubDirectory2); | ||
|
||
// Verify that two separate cache files are generated | ||
this.Repo.Head.GetVersionHeight(repoRelativeSubDirectory1); | ||
Assert.True(File.Exists(Path.Combine(RepoPath, repoRelativeSubDirectory1, GitHeightCache.CacheFileName))); | ||
|
||
this.Repo.Head.GetVersionHeight(repoRelativeSubDirectory2); | ||
Assert.True(File.Exists(Path.Combine(RepoPath, repoRelativeSubDirectory2, GitHeightCache.CacheFileName))); | ||
} | ||
|
||
[Fact] | ||
public void GetVersionHeight_CachingMultipleParents() | ||
{ | ||
/* | ||
* Layout of branches + commits: | ||
* master: version -> second --------------> | | ||
* | | | ||
* another: | -> branch commit #n x 5 | -> merge commit | ||
*/ | ||
this.WriteVersionFile(); | ||
var anotherBranch = this.Repo.CreateBranch("another"); | ||
var secondCommit = this.Repo.Commit("Second", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); | ||
|
||
// get height of the second commit- will cache the height for this commit | ||
var height = secondCommit.GetVersionHeight(useHeightCaching: true); | ||
Assert.Equal(2, height); | ||
|
||
// add many commits to the another branch + merge with master | ||
Commands.Checkout(this.Repo, anotherBranch); | ||
Commit[] branchCommits = new Commit[5]; | ||
for (int i = 1; i <= branchCommits.Length; i++) | ||
{ | ||
branchCommits[i - 1] = this.Repo.Commit($"branch commit #{i}", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); | ||
} | ||
|
||
this.Repo.Merge(secondCommit, new Signature("t", "[email protected]", DateTimeOffset.Now), new MergeOptions { FastForwardStrategy = FastForwardStrategy.NoFastForward }); | ||
|
||
// The height should be the height of the 'another' branch as it has the greatest height. | ||
// The cached height for the master branch should be ignored. | ||
Assert.Equal(7, this.Repo.Head.GetVersionHeight()); | ||
} | ||
|
||
[Fact] | ||
public void GetVersionHeight_NewCommitsInvalidateCache() | ||
{ | ||
const string repoRelativeSubDirectory = "subdir"; | ||
|
||
var semanticVersion1 = SemanticVersion.Parse("1.0"); | ||
this.WriteVersionFile( | ||
new VersionOptions { Version = semanticVersion1 }, | ||
repoRelativeSubDirectory); | ||
|
||
var height1 = this.Repo.Head.GetVersionHeight(repoRelativeSubDirectory); | ||
this.Repo.Commit($"Test commit (should invalidate cache)", this.Signer, this.Signer, new CommitOptions {AllowEmptyCommit = true}); | ||
var height2 = this.Repo.Head.GetVersionHeight(repoRelativeSubDirectory); | ||
|
||
Assert.Equal(1, height1); | ||
Assert.Equal(2, height2); | ||
} | ||
|
||
[Fact] | ||
public void GetCommitsFromVersion_WithPathFilters() | ||
|
@@ -825,4 +943,21 @@ private void VerifyCommitsWithVersion(Commit[] commits) | |
Assert.Equal(commits[i], this.Repo.GetCommitFromVersion(encodedVersion)); | ||
} | ||
} | ||
|
||
private (T, TimeSpan) MeasureRuntime<T>(Func<T> toTime, string description) | ||
{ | ||
var sp = Stopwatch.StartNew(); | ||
try | ||
{ | ||
var result = toTime(); | ||
sp.Stop(); | ||
|
||
return (result, sp.Elapsed); | ||
} | ||
finally | ||
{ | ||
sp.Stop(); | ||
this.Logger.WriteLine($"Took {sp.Elapsed} to {description}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.IO; | ||
using LibGit2Sharp; | ||
using Xunit; | ||
using Version = System.Version; | ||
|
||
namespace Nerdbank.GitVersioning | ||
{ | ||
public class GitHeightCacheTests | ||
{ | ||
[Fact] | ||
public void CachedHeightAvailable_NoCacheFile() | ||
{ | ||
var cache = new GitHeightCache(Directory.GetCurrentDirectory(), "non-existent-dir", new Version(1, 0)); | ||
Assert.False(cache.CachedHeightAvailable); | ||
} | ||
|
||
[Fact] | ||
public void CachedHeightAvailable_RootCacheFile() | ||
{ | ||
File.WriteAllText($"./{GitHeightCache.CacheFileName}", ""); | ||
var cache = new GitHeightCache(Directory.GetCurrentDirectory(), null, new Version(1, 0)); | ||
Assert.True(cache.CachedHeightAvailable); | ||
} | ||
|
||
[Fact] | ||
public void CachedHeightAvailable_CacheFile() | ||
{ | ||
Directory.CreateDirectory("./testDir"); | ||
File.WriteAllText($"./testDir/{GitHeightCache.CacheFileName}", ""); | ||
var cache = new GitHeightCache(Directory.GetCurrentDirectory(),"testDir/", new Version(1, 0)); | ||
Assert.True(cache.CachedHeightAvailable); | ||
} | ||
|
||
[Fact] | ||
public void GitHeightCache_RoundtripCaching() | ||
{ | ||
var cache = new GitHeightCache(Directory.GetCurrentDirectory(), null, new Version(1, 0)); | ||
|
||
// test initial set | ||
cache.SetHeight(new ObjectId("8b1f731de6b98aaf536085a62c40dfd3e38817b6"), 2); | ||
var cachedHeight = cache.GetHeight(); | ||
Assert.Equal("8b1f731de6b98aaf536085a62c40dfd3e38817b6", cachedHeight.CommitId.Sha); | ||
Assert.Equal(2, cachedHeight.Height); | ||
Assert.Equal("1.0", cachedHeight.BaseVersion.ToString()); | ||
|
||
// verify overwriting works correctly | ||
cache.SetHeight(new ObjectId("352459698e082aebef799d77807961d222e75efe"), 3); | ||
cachedHeight = cache.GetHeight(); | ||
Assert.Equal("352459698e082aebef799d77807961d222e75efe", cachedHeight.CommitId.Sha); | ||
Assert.Equal("1.0", cachedHeight.BaseVersion.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably be constructing the git height cache only when
useHeightCaching
is true.