public static BenchmarkResult Execute(bool throwExceptionOnExerciserFailure) { var type = typeof(T); if ((type.IsNested && !type.IsNestedPublic) || (!type.IsNested && !type.IsPublic)) { throw new BenchmarkerClassNotPublicException($"Benchmark class '{typeof(T).FullName}' is not public. Benchmark.Net requires it to be public."); } var logInterceptor = new UnitsOfWorkLogInterceptor(); var config = ManualConfig.CreateEmpty(); config.Add(Job.Default .With(Platform.X64) .WithGcServer(true) .WithGcConcurrent(true) .WithEvaluateOverhead(true) .WithOutlierMode(BenchmarkDotNet.Mathematics.OutlierMode.All)); config.Add(MemoryDiagnoser.Default); config.Add(new UnitsOfWorkDiagnoser(logInterceptor)); config.Add(logInterceptor); config.Add(DefaultConfig.Instance); config.Add(JitOptimizationsValidator.DontFailOnError); var result = new BenchmarkResult(); var benchmarkResult = BenchmarkRunner.Run <T>(config); if (benchmarkResult.Reports.Count() != 1) { throw new Exception("BenchmarkDotNet did not produce benchmark results."); } //Update benchmark results result.EndTime = DateTime.UtcNow; result.Duration_Mean_Nanoseconds = benchmarkResult.Reports[0].ResultStatistics.Mean; result.Duration_Min_Nanoseconds = benchmarkResult.Reports[0].ResultStatistics.Min; result.Duration_Max_Nanoseconds = benchmarkResult.Reports[0].ResultStatistics.Max; result.Duration_StdDev_Nanoseconds = benchmarkResult.Reports[0].ResultStatistics.StandardDeviation; result.Memory_BytesAllocated = benchmarkResult.Reports[0].GcStats.BytesAllocatedPerOperation; result.GC_Gen0Collections = benchmarkResult.Reports[0].GcStats.Gen0Collections; result.GC_Gen1Collections = benchmarkResult.Reports[0].GcStats.Gen1Collections; result.GC_Gen2Collections = benchmarkResult.Reports[0].GcStats.Gen2Collections; result.CountUnitsOfWorkExecuted_Min = Convert.ToInt64(benchmarkResult.Reports[0].Metrics[MetricNameCountUnitsOfWorkPerformedMin].Value); result.CountUnitsOfWorkExecuted_Mean = benchmarkResult.Reports[0].Metrics[MetricNameCountUnitsOfWorkPerformedAvg].Value; result.CountUnitsOfWorkExecuted_Max = Convert.ToInt64(benchmarkResult.Reports[0].Metrics[MetricNameCountUnitsOfWorkPerformedMax].Value); result.CountUnitsOfWorkExecuted_StdDev = Convert.ToInt64(benchmarkResult.Reports[0].Metrics[MetricNameCountUnitsOfWorkPerformedStdDev].Value); result.CountExceptions = Convert.ToInt64(benchmarkResult.Reports[0].Metrics[MetricNameCountExceptions].Value); if (throwExceptionOnExerciserFailure && result.CountExceptions > 0) { throw new Exception($"Exerciser Encountered {result.CountExceptions} during its runs"); } return(result); }
public UnitsOfWorkDiagnoser(UnitsOfWorkLogInterceptor uowLogInterceptor) { _uowLogInterceptor = uowLogInterceptor; }