private static void WriteReadDummy(IBenchmarkDummy benchmarkRunner, BenchmarkOperation operations, Random random) { var d = new Dummy() { Id = Guid.NewGuid().ToString("N"), Blob = new byte[random.Next(4 * 1024, 20 * 1024)] // 4-20 KB }; random.NextBytes(d.Blob); if ((operations & BenchmarkOperation.Insert) == BenchmarkOperation.Insert) { benchmarkRunner.Save(d); } if ((operations & BenchmarkOperation.Get) == BenchmarkOperation.Get) { var d2 = benchmarkRunner.Get(d.Id); if (d2 == null) { throw new InvalidOperationException("Could not find item"); } if (d2.Id != d.Id) { throw new InvalidOperationException("Has a different id: " + d2.Id); } } if ((operations & BenchmarkOperation.GetNonExistent) == BenchmarkOperation.GetNonExistent) { var d3 = benchmarkRunner.Get(d.Id + 1); if (d3 != null) { throw new InvalidOperationException("Found a non-existent item"); } } }
private static void WriteReadNTimes(int n, IBenchmarkDummy benchmarkRunner, BenchmarkOperation operations, bool gatherStatistics, Stopwatch stopwatch, StreamWriter writer ) { var random = new Random(); double totalMilliseconds = 0; for (int i = 1; i <= n; i++) { WriteReadDummy(benchmarkRunner, operations, random); if (i % 100 == 0) { Console.Write("\r" + i); if (gatherStatistics) { var current = stopwatch.Elapsed.TotalMilliseconds; writer.Write(i + ","); writer.Write(current - totalMilliseconds); writer.Write("\r\n"); totalMilliseconds = current; } } } }
/// <summary> /// Runs the specified operation and returns the duration of time taken. /// </summary> /// <param name="container">The container to benchmark.</param> /// <param name="operation">The operation.</param> /// <param name="tuple">The tuple.</param> /// <returns> /// A duration /// </returns> /// <exception cref="System.ArgumentException">Operation not supported - operation</exception> private Action <T> GetAction <T>(IBenchmarkContainer <T> container, BenchmarkOperation operation) { Action <T> method; switch (operation) { case BenchmarkOperation.Delete: method = container.Delete; break; case BenchmarkOperation.Find: method = (t) => container.Find(t); break; case BenchmarkOperation.Insert: method = container.Insert; break; case BenchmarkOperation.Iterate: method = (t) => container.Iterate(); break; default: throw new ArgumentException("Operation not supported", nameof(operation)); } return(method); }
/// <summary> /// Records a score for the specified operation. /// </summary> /// <param name="operation">The operation.</param> /// <param name="score">The score.</param> /// <exception cref="System.ArgumentException">Only one score can be recorded per operation</exception> public void Record(BenchmarkOperation operation, double score) { if (!this.Scores.TryAdd(operation, score)) { throw new ArgumentException("Only one score can be recorded per operation"); } ; }
private static void Test(IBenchmarkDummy benchmarkRunner, int n, BenchmarkOperation operations = BenchmarkOperation.Insert | BenchmarkOperation.Get | BenchmarkOperation.GetNonExistent, bool gatherStatistics = false, int concurrency = 50) { string benchmarker = benchmarkRunner.GetType().Name; StreamWriter writer = null; if (gatherStatistics) { writer = new StreamWriter(benchmarker + ".csv"); writer.Write("n, milliseconds\r\n"); } benchmarkRunner.Init(); var stopwatch = new Stopwatch(); var random = new Random(); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Starting benchmark with " + benchmarker); Console.ForegroundColor = ConsoleColor.DarkYellow; ThreadPool.SetMinThreads(concurrency, 100); ThreadPool.SetMaxThreads(concurrency, 100); Console.Write("Warming up"); for (int i = 0; i < concurrency; i++) { ThreadPool.QueueUserWorkItem((o) => Console.Write(".")); } Thread.Sleep(2000); Console.WriteLine("it should be OK now."); stopwatch.Start(); if (concurrency > 0) { WriteReadNTimesAsync(n, benchmarkRunner, operations, gatherStatistics, stopwatch, writer); } else { WriteReadNTimes(n, benchmarkRunner, operations, gatherStatistics, stopwatch, writer); } stopwatch.Stop(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Took " + stopwatch.Elapsed); Console.WriteLine("Average " + (stopwatch.Elapsed.TotalMilliseconds / n).ToString("0.0")); Console.WriteLine("---------------------------------------"); benchmarkRunner.Cleanup(); if (gatherStatistics) { writer.Flush(); writer.Close(); } }
private static void WriteReadNTimesAsync(int n, IBenchmarkDummy benchmarkRunner, BenchmarkOperation operations, bool gatherStatistics, Stopwatch stopwatch, StreamWriter writer ) { var random = new Random(); double totalMilliseconds = 0; failedCount = 0; currentCount = 0; var resetEvent = new AutoResetEvent(false); for (int i = 0; i < n; i++) { ThreadPool.QueueUserWorkItem( (o) => { try { WriteReadDummy(benchmarkRunner, operations, random); Interlocked.Increment(ref currentCount); } catch (Exception e) { Interlocked.Increment(ref failedCount); Interlocked.Increment(ref currentCount); Trace.WriteLine(e.ToString()); } } ); } while (true) { resetEvent.WaitOne(1000); Console.Write("\r" + currentCount); var color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write(" (" + failedCount + ")"); Console.ForegroundColor = color; if (currentCount == n) { break; } } Console.WriteLine(); Console.WriteLine("Failed count: " + failedCount); }
private static void WriteReadNTimesAsync(int n, IBenchmarkDummy benchmarkRunner, BenchmarkOperation operations, bool gatherStatistics, Stopwatch stopwatch, StreamWriter writer ) { var random = new Random(); double totalMilliseconds = 0; failedCount = 0; currentCount = 0; var resetEvent = new AutoResetEvent(false); for (int i = 0; i < n; i++) { ThreadPool.QueueUserWorkItem( (o) => { try { WriteReadDummy(benchmarkRunner, operations, random); Interlocked.Increment(ref currentCount); } catch(Exception e) { Interlocked.Increment(ref failedCount); Interlocked.Increment(ref currentCount); Trace.WriteLine(e.ToString()); } } ); } while (true) { resetEvent.WaitOne(1000); Console.Write("\r" + currentCount); var color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write(" (" + failedCount + ")"); Console.ForegroundColor = color; if(currentCount == n) break; } Console.WriteLine(); Console.WriteLine("Failed count: " + failedCount); }
private static void WriteReadDummy(IBenchmarkDummy benchmarkRunner, BenchmarkOperation operations, Random random) { var d = new Dummy() { Id = Guid.NewGuid().ToString("N"), Blob = new byte[random.Next(4 * 1024, 20 * 1024)] // 4-20 KB }; random.NextBytes(d.Blob); if ((operations & BenchmarkOperation.Insert) == BenchmarkOperation.Insert) { benchmarkRunner.Save(d); } if ((operations & BenchmarkOperation.Get) == BenchmarkOperation.Get) { var d2 = benchmarkRunner.Get(d.Id); if (d2 == null) throw new InvalidOperationException("Could not find item"); if (d2.Id != d.Id) throw new InvalidOperationException("Has a different id: " + d2.Id); } if ((operations & BenchmarkOperation.GetNonExistent) == BenchmarkOperation.GetNonExistent) { var d3 = benchmarkRunner.Get(d.Id + 1); if (d3 != null) throw new InvalidOperationException("Found a non-existent item"); } }
private static void Test(IBenchmarkDummy benchmarkRunner, int n, BenchmarkOperation operations = BenchmarkOperation.Insert | BenchmarkOperation.Get | BenchmarkOperation.GetNonExistent, bool gatherStatistics = false, int concurrency = 50) { string benchmarker = benchmarkRunner.GetType().Name; StreamWriter writer = null; if(gatherStatistics) { writer = new StreamWriter(benchmarker + ".csv"); writer.Write("n, milliseconds\r\n"); } benchmarkRunner.Init(); var stopwatch = new Stopwatch(); var random = new Random(); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Starting benchmark with " + benchmarker); Console.ForegroundColor = ConsoleColor.DarkYellow; ThreadPool.SetMinThreads(concurrency, 100); ThreadPool.SetMaxThreads(concurrency, 100); Console.Write("Warming up"); for (int i = 0; i < concurrency; i++) { ThreadPool.QueueUserWorkItem((o) => Console.Write(".")); } Thread.Sleep(2000); Console.WriteLine("it should be OK now."); stopwatch.Start(); if(concurrency>0) WriteReadNTimesAsync(n, benchmarkRunner, operations, gatherStatistics, stopwatch, writer); else WriteReadNTimes(n, benchmarkRunner, operations, gatherStatistics, stopwatch, writer); stopwatch.Stop(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Took " + stopwatch.Elapsed); Console.WriteLine("Average " + (stopwatch.Elapsed.TotalMilliseconds / n).ToString("0.0")); Console.WriteLine("---------------------------------------"); benchmarkRunner.Cleanup(); if (gatherStatistics) { writer.Flush(); writer.Close(); } }
/// <summary> /// Gets the result key. /// </summary> /// <param name="containerType">Type of the container.</param> /// <param name="cardinality">The cardinality.</param> /// <returns>A string</returns> private static string GetResultKey(BenchmarkContainerType containerType, long cardinality, BenchmarkOperation operation) { return($"{containerType}\\{cardinality}\\{operation}"); }