static async Task <BenchmarkResult> RunBenchmark(ILockingImplementation impl) { var tasks = Enumerable.Range(0, 1000) .Select(_ => new Task <long>(() => { var sw = Stopwatch.StartNew(); impl.AquireLock(); impl.ExecuteCode(() => Thread.Sleep(10)); impl.ReleaseLock(); sw.Stop(); return(sw.ElapsedMilliseconds); })) .ToList(); var swTotal = Stopwatch.StartNew(); tasks.ForEach(t => t.Start()); await Task.WhenAll(tasks); swTotal.Stop(); var results = tasks.Select(t => t.Result).ToList(); return(new BenchmarkResult(impl.Name) { TotalExecutionTime = swTotal.ElapsedMilliseconds, TotalExecutionTimeSum = results.Sum(), MinExecutionTime = results.Min(), MaxExecutionTime = results.Max() }); }
static void Main() { var locks = new ILockingImplementation[] { new RWSlimLock(), new MonitorLock(), new StraightLock() }; foreach (var impl in locks) { //This blocks Console.WriteLine(RunBenchmark(impl).Result); Console.WriteLine(); } Console.WriteLine("Done"); Console.ReadLine(); }