示例#1
0
 public void Restart()
 {
     _benchmarkResult = new BenchmarkResult();
     _currentProcess  = Process.GetCurrentProcess();
     GC.Collect();
     _benchmarkResult.GcCollectionCount0 = GC.CollectionCount(0);
     _benchmarkResult.GcCollectionCount1 = GC.CollectionCount(1);
     _benchmarkResult.GcCollectionCount2 = GC.CollectionCount(2);
     _stopWatch = Stopwatch.StartNew();
     _benchmarkResult.UserProcessorTime = _currentProcess.UserProcessorTime;
 }
示例#2
0
            public BenchmarkResult Stop(uint iterations)
            {
                if (_benchmarkResult == null)
                {
                    throw new InvalidOperationException("You can't call Stop without calling Restart.");
                }
                _benchmarkResult.UserProcessorTime = _currentProcess.UserProcessorTime - _benchmarkResult.UserProcessorTime;
                _stopWatch.Stop();
                if (iterations == 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(iterations));
                }
                _benchmarkResult.Iterations         = iterations;
                _benchmarkResult.ElapsedTime        = _stopWatch.Elapsed;
                _benchmarkResult.GcCollectionCount0 = GC.CollectionCount(0) - _benchmarkResult.GcCollectionCount0;
                _benchmarkResult.GcCollectionCount1 = GC.CollectionCount(1) - _benchmarkResult.GcCollectionCount1;
                _benchmarkResult.GcCollectionCount2 = GC.CollectionCount(2) - _benchmarkResult.GcCollectionCount2;
                var result = _benchmarkResult;

                _benchmarkResult = null;
                return(result);
            }
示例#3
0
        private static BenchmarkResult Benchmark(int durationSeconds)
        {
            try
            {
                Console.WriteLine("Guessing number of required iterations...");
                var iterations = GuessIterations(TimeSpan.FromSeconds(durationSeconds));

                Console.WriteLine("BENCHMARKING STARTED");

                var benchmarkResult = Run(iterations);

                Console.WriteLine("done");

                return(benchmarkResult);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                var result = new BenchmarkResult();
                result.Error = ex.ToString();
                return(result);
            }
        }