/// <summary> /// Measures the time required to run this benchmark. /// </summary> /// <returns>The time that it took to run this benchmark.</returns> public TimeSpan Run() { WarmUp(); Stopwatch sw = new Stopwatch(); // Make sure that each interation takes over 100ms // We do this to make sure the Stopwatch is keeping time accurately sw.Start(); MethodToBenchmark(); sw.Stop(); // If it isn't, do enough calls so that it is above 100ms int callsPerInteration = sw.Elapsed.TotalMilliseconds < 100 ? (int)Math.Ceiling(100 / sw.Elapsed.TotalMilliseconds) + 1 : 1; sw.Reset(); TimeSpan[] times = new TimeSpan[_timesToRun]; for (int i = 0; i < _timesToRun; i++) { sw.Start(); for (int j = 0; j < callsPerInteration; j++) { MethodToBenchmark(); } sw.Stop(); times[i] = new TimeSpan((int)Math.Round(sw.Elapsed.Ticks / (double)callsPerInteration)); sw.Reset(); } // Throw out the top and bottom 25% as outliers return new TimeSpan((int)Math.Round(times.OrderBy(x => x.Ticks).Skip(_timesToRun / 4).Take(_timesToRun / 2).Average(x => x.Ticks))); }
public void Benchmark2() { var sw = new Stopwatch(); var times = new TimeSpan[10]; for (var i = 0; i < 10; i++) { sw.Start(); var allFriends = this.db.Views.QueryAsync<Person>(new QueryViewRequest("person", "all_friends")).Result; var randomPerson = allFriends.Rows.RandomEntry(this.rng).Value; var friendsFromRandom = this.RelationsFrom(randomPerson, "Friend").ToList(); var friendsFromFriendsFromRandom = friendsFromRandom.SelectMany(x => this.RelationsFrom(x, "Friend")).ToList(); Console.WriteLine("Count results : " + friendsFromFriendsFromRandom.Count); times[i] = sw.Elapsed; sw.Stop(); sw.Reset(); } Console.WriteLine("====================================================================="); Console.WriteLine("Min time : " + times.Min()); Console.WriteLine("Max time : " + times.Max()); Console.WriteLine("Average time : " + TimeSpan.FromTicks((long)times.Average(x => x.Ticks))); Console.WriteLine("Median time : " + times.OrderBy(x => x).ElementAt(times.Length / 2)); Console.WriteLine("====================================================================="); }