Skip to content

Instantly share code, notes, and snippets.

@joelverhagen
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelverhagen/11216239 to your computer and use it in GitHub Desktop.
Save joelverhagen/11216239 to your computer and use it in GitHub Desktop.
Performance tests of LibGit2Sharp "git add" and "git commit" versus the standard "git.exe".
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using LibGit2Sharp;
namespace LibGitTest
{
internal class Program
{
private const int GuidLength = 36;
private const int FileCount = 5000;
private const int FileSize = 32*1024;
private const string RepositoryPath = "Test";
private const string GitPath = @"C:\Program Files (x86)\Git\bin\git.exe";
private static string GenerateRandomFile(int bytes)
{
var sb = new StringBuilder();
for (int i = 0; i < bytes; i += GuidLength + 1)
{
sb.Append(Guid.NewGuid() + "\n");
}
sb.Remove(bytes - 1, sb.Length - bytes);
return sb.ToString();
}
private static void DeleteRepository()
{
if (Directory.Exists(RepositoryPath))
{
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.Arguments = "/c rmdir /s /q \"" + RepositoryPath + "\"";
p.Start();
p.WaitForExit();
}
}
private static void Main(string[] args)
{
RunTest("command_line_times.txt", CommandLineTest);
RunTest("LibGit2Sharp_times.txt", LibGit2SharpTest);
}
private static void RunTest(string timesFile, Func<string, Tuple<long, long>> stageAndCommit)
{
DeleteRepository();
Repository.Init(RepositoryPath);
using (var logFileStream = new FileStream(timesFile, FileMode.Create, FileAccess.Write))
{
using (var writer = new StreamWriter(logFileStream))
{
writer.WriteLine("Stage Time\tCommit Time");
for (int i = 1; i <= FileCount; i++)
{
string id = i.ToString(CultureInfo.InvariantCulture);
string filePath = Path.Combine(RepositoryPath, id + ".txt");
File.WriteAllText(filePath, GenerateRandomFile(FileSize));
Tuple<long, long> times = stageAndCommit(filePath);
writer.WriteLine(times.Item1 + "\t" + times.Item2);
writer.Flush();
Console.WriteLine(i + " / " + FileCount);
}
}
}
}
private static Tuple<long, long> LibGit2SharpTest(string filePath)
{
using (var repository = new Repository(RepositoryPath))
{
var stopwatch = new Stopwatch();
string fullPath = Path.GetFullPath(filePath);
stopwatch.Restart();
repository.Index.Stage(fullPath);
stopwatch.Stop();
long writeTime = stopwatch.ElapsedTicks;
var author = new Signature("Me", "Me", DateTimeOffset.Now);
stopwatch.Restart();
repository.Commit("Test commit.", author, author);
stopwatch.Stop();
long commitTime = stopwatch.ElapsedTicks;
return new Tuple<long, long>(writeTime, commitTime);
}
}
private static Tuple<long, long> CommandLineTest(string filePath)
{
var stopwatch = new Stopwatch();
var stageProcess = new Process();
stageProcess.StartInfo.FileName = GitPath;
stageProcess.StartInfo.WorkingDirectory = RepositoryPath;
stageProcess.StartInfo.Arguments = "add " + filePath;
stageProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
stopwatch.Restart();
stageProcess.Start();
stageProcess.WaitForExit();
stopwatch.Stop();
long writeTime = stopwatch.ElapsedTicks;
var commitProcess = new Process();
commitProcess.StartInfo.FileName = GitPath;
commitProcess.StartInfo.WorkingDirectory = RepositoryPath;
commitProcess.StartInfo.Arguments = "commit -m \"Test commit.\"";
commitProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
stopwatch.Restart();
stageProcess.Start();
stageProcess.WaitForExit();
stopwatch.Stop();
long commitTime = stopwatch.ElapsedTicks;
return new Tuple<long, long>(writeTime, commitTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment