From dbaa8bb20a9a6fd5d4cdb29ebe2c6c4cc90f41cd Mon Sep 17 00:00:00 2001 From: Matt Burke Date: Tue, 15 Dec 2009 15:23:13 -0500 Subject: [PATCH] Improved benchmarks. --- GitTfsBenchmarks/Benchmark.cs | 15 ++++- .../Benchmarks/HashAndInsertObject.cs | 56 ++++++++++++------- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/GitTfsBenchmarks/Benchmark.cs b/GitTfsBenchmarks/Benchmark.cs index dbc2f155..86013458 100644 --- a/GitTfsBenchmarks/Benchmark.cs +++ b/GitTfsBenchmarks/Benchmark.cs @@ -23,6 +23,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Reflection; using System.Collections; @@ -65,6 +66,8 @@ public class Benchmark // just makes it easier to read the code... BindingFlags publicStatic = BindingFlags.Public | BindingFlags.Static; + var exceptions = new List(); + foreach (Type type in typeof(Benchmark).Assembly.GetTypes()) { // Find an Init method taking string[], if any @@ -141,6 +144,7 @@ public class Benchmark foreach (MethodInfo method in benchmarkMethods) { + var shouldCleanUpOnFailure = true; try { // Reset (if appropriate) @@ -168,6 +172,7 @@ public class Benchmark } // Clean up (if appropriate) + shouldCleanUpOnFailure = false; if (cleanupMethod != null) { cleanupMethod.Invoke(null, null); @@ -180,15 +185,16 @@ public class Benchmark catch (TargetInvocationException e) { Exception inner = e.InnerException; + exceptions.Add(inner ?? e); string message = (inner == null ? null : inner.Message); if (message == null) { message = "(No message)"; } - Console.WriteLine(" {0}: Failed ({1})", method.Name, message); + Console.WriteLine(" {0}: Failed ({1}) [{2}]", method.Name, message, exceptions.Count); // Clean up (if appropriate) - if (cleanupMethod != null) + if (shouldCleanUpOnFailure && cleanupMethod != null) { cleanupMethod.Invoke(null, null); } @@ -196,6 +202,11 @@ public class Benchmark } } } + for (var i = 0; i < exceptions.Count; i++) + { + Console.WriteLine(); + Console.WriteLine("[" + (i + 1) + "] " + exceptions[i]); + } } /// diff --git a/GitTfsBenchmarks/Benchmarks/HashAndInsertObject.cs b/GitTfsBenchmarks/Benchmarks/HashAndInsertObject.cs index 89f5020b..19c33b67 100644 --- a/GitTfsBenchmarks/Benchmarks/HashAndInsertObject.cs +++ b/GitTfsBenchmarks/Benchmarks/HashAndInsertObject.cs @@ -1,12 +1,11 @@ using System; -using System.Collections; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Security.Cryptography; using System.Text; -using System.Text.RegularExpressions; using GitSharp.Core; +using SEP.Extensions; using Sep.Git.Tfs.Core; using Sep.Git.Tfs.Util; @@ -14,6 +13,21 @@ namespace Sep.Git.Tfs.Benchmarks { class HashAndInsertObject { + class TestGitObject + { + public string ObjectId { get; set; } + public string Contents { get; set; } + public string ObjectPath { get { return ".git/objects/" + ObjectId.Substring(0, 2) + "/" + ObjectId.Substring(2); } } + } + + private static readonly List TestObjects = new List + { + new TestGitObject + { + ObjectId = "0e44708cb3166a9f6c5c0a038bc7b2c0c2435e13", + Contents = "teststring\r\nanother line\rafter just r\nafter just n" + } + }; private static readonly GitHelpers gitHelper = new GitHelpers(TextWriter.Null); #region WithPureDotNet @@ -128,26 +142,28 @@ namespace Sep.Git.Tfs.Benchmarks public static void Check() { - foreach (var file in GetExpectedFiles()) + foreach (var testObject in TestObjects) { - if (!File.Exists(file)) + if (!File.Exists(testObject.ObjectPath)) { - throw new Exception("Expected file " + file + " was not found!"); + throw new Exception("Expected file " + testObject.ObjectPath + " was not found!"); + } + try + { + var objectContents = gitHelper.Command("show", testObject.ObjectId); + if(objectContents != testObject.Contents) + { + throw new Exception("Expected object " + testObject.ObjectId + " to be " + testObject.Contents.Inspect() + + " but it was " + objectContents.Inspect()); + } + } + catch (GitCommandException e) + { + throw new Exception("Unable to read object " + testObject.ObjectId + ".", e); } } } - private static IEnumerable GetExpectedFiles() - { - yield return ".git/objects/0e/44708cb3166a9f6c5c0a038bc7b2c0c2435e13"; - } - - private static IEnumerable Split(string files) - { - var regex = new Regex("\\s+"); - return regex.Split(files); - } - private static void Run(string name, Func hashAndStore) { var dirName = Path.Combine(Environment.CurrentDirectory, "hash-object-" + name); @@ -156,10 +172,12 @@ namespace Sep.Git.Tfs.Benchmarks Environment.CurrentDirectory = dirName; gitHelper.CommandNoisy("init"); - for (int i = 0; i < 300; i++) + foreach (var testObject in TestObjects) { - hashAndStore( - MakeMemoryStream("teststring\r\nanother line\rafter just r\nafter just n")); + for (int i = 0; i < 300; i++) + { + hashAndStore(MakeMemoryStream(testObject.Contents)); + } } }