public IEnumerable <IBenchmarkComponentResult> Execute <T>(BenchmarkProcessorConfiguration benchmarkProcessorConfiguration, params Func <T>[] benchmarkComponents)
            where T : IBenchmarkComponent <IOperationBase>
        {
            if (benchmarkProcessorConfiguration.IfJitOptimizerIsDisabledThenThrowException)
            {
                AppDomain.CurrentDomain.AssemblyLoad += (sender, args) => EnsureAssembliesAreAllowed(benchmarkProcessorConfiguration, new [] { args.LoadedAssembly });

                EnsureAssembliesAreAllowed(benchmarkProcessorConfiguration, AppDomain.CurrentDomain.GetAssemblies());
            }


            for (int iterationIndex = 0; iterationIndex < benchmarkProcessorConfiguration.WarmupIterationCount; iterationIndex++)
            {
                foreach (Func <T> benchmarkFactory in benchmarkComponents)
                {
                    BenchmarkAndExecuteInner(benchmarkProcessorConfiguration, benchmarkFactory().RootOperation);
                }
            }

            List <Tuple <string, List <ISingleIterationOperationResultBase> > > benchmarkComponentsSingleIterationsBenchmarkProcessorResults = new List <Tuple <string, List <ISingleIterationOperationResultBase> > >(benchmarkComponents.Select(benchmarkComponentFactory => new Tuple <string, List <ISingleIterationOperationResultBase> >(benchmarkComponentFactory().Name, new List <ISingleIterationOperationResultBase>())));

            for (int iterationIndex = 0; iterationIndex < benchmarkProcessorConfiguration.BenchmarkIterationCount; iterationIndex++)
            {
                for (int benchmarkIndex = 0; benchmarkIndex < benchmarkComponents.Length; benchmarkIndex++)
                {
                    benchmarkComponentsSingleIterationsBenchmarkProcessorResults[benchmarkIndex].Item2.Add(BenchmarkAndExecuteInner(benchmarkProcessorConfiguration, benchmarkComponents[benchmarkIndex]().RootOperation));
                }
            }

            return(benchmarkComponentsSingleIterationsBenchmarkProcessorResults.Select(tuple => new BenchmarkComponentResult()
            {
                Name = tuple.Item1,
                RootOperationResult = CalculateAggregateResultBasedOnMultipleIterations(tuple.Item2),
            }));
        }
        private void EnsureAssembliesAreAllowed(BenchmarkProcessorConfiguration benchmarkProcessorConfiguration, IEnumerable <Assembly> assemblies)
        {
            Assembly[] assembliesViolatingRuleJitOptimization = !benchmarkProcessorConfiguration.IfJitOptimizerIsDisabledThenThrowException ? new Assembly[] { } : assemblies.Where(IsJitOptimizerDisabled).ToArray();

            if (assembliesViolatingRuleJitOptimization.Any())
            {
                throw new Exception("To allow benchmarks to be executed when jit optimization is disabled, set BenchmarkProcessorConfiguration.IfJitOptimizerIsDisabledThenThrowException to false. The following assemblies have jit optimization disabled:" + string.Join("", assembliesViolatingRuleJitOptimization.Select(assembly => Environment.NewLine + assembly)));
            }
        }
        private SingleIterationOperationGroupResult BenchmarkAndExecuteInner_Group(BenchmarkProcessorConfiguration benchmarkProcessorConfiguration, IOperationGroupBase operationGroup, params IOperationBase[] childOperations)
        {
            List <ISingleIterationOperationResultBase> childOperationResults = childOperations.Select(childOperation => BenchmarkAndExecuteInner(benchmarkProcessorConfiguration, childOperation)).ToList();

            return(new SingleIterationOperationGroupResult()
            {
                Name = operationGroup.Name,
                Duration = new TimeSpan(childOperationResults.Select(childOperationResult => childOperationResult.Duration.Ticks).Sum()),
                ChildOperationResults = childOperationResults,
            });
        }
示例#4
0
        public static void RunSample()
        {
            IList<CountryOrRegionGdpData> listData = new DataImporter().Import();

            Func<BenchmarkComponentBase>[] benchmarkComponents =
            {
                () => new BenchmarkComponentKeyedCollection(listData),
                () => new BenchmarkComponentMultiplyIndexedKeyedCollection(listData),
            };

            BenchmarkProcessor benchmarkProcessor = new BenchmarkProcessor();

            BenchmarkProcessorConfiguration benchmarkProcessorConfiguration = new BenchmarkProcessorConfiguration();

            IEnumerable<IBenchmarkComponentResult> benchmarkResults = benchmarkProcessor.Execute(benchmarkProcessorConfiguration, benchmarkComponents);

            foreach (IBenchmarkComponentResult benchmarkComponentResult in benchmarkResults)
                Console.WriteLine("Benchmark Component: {0}{1}{2}", benchmarkComponentResult.Name, Environment.NewLine, FormatBenchmarkResults(benchmarkComponentResult.RootOperationResult, 0));

            Console.ReadLine();
        }
        private ISingleIterationOperationResultBase BenchmarkAndExecuteInner <TOperation>(BenchmarkProcessorConfiguration benchmarkProcessorConfiguration, TOperation operation)
            where TOperation : IOperationBase
        {
            if (benchmarkProcessorConfiguration.GarbageCollectBeforeEachOperation)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }

            if (benchmarkProcessorConfiguration.IfDebuggerIsAttachedThenThrowException && System.Diagnostics.Debugger.IsAttached)
            {
                throw new Exception("Debugger is attached. To allow benchmarks to be executed while the debugger is attached, set BenchmarkProcessorConfiguration.IfDebuggerIsAttachedThenThrowException to false.");
            }

            if (operation is IOperationGroup <IOperationBase> )
            {
                var operationTyped = (IOperationGroup <IOperationBase>)operation;
                return(BenchmarkAndExecuteInner_Group(benchmarkProcessorConfiguration, operationTyped, operationTyped.Operation1));
            }

            if (operation is IOperationGroup <IOperationBase, IOperationBase> )
            {
                var operationTyped = (IOperationGroup <IOperationBase, IOperationBase>)operation;
                return(BenchmarkAndExecuteInner_Group(benchmarkProcessorConfiguration, operationTyped, operationTyped.Operation1, operationTyped.Operation2));
            }

            if (operation is IOperationGroup <IOperationBase, IOperationBase, IOperationBase> )
            {
                var operationTyped = (IOperationGroup <IOperationBase, IOperationBase, IOperationBase>)operation;
                return(BenchmarkAndExecuteInner_Group(benchmarkProcessorConfiguration, operationTyped, operationTyped.Operation1, operationTyped.Operation2, operationTyped.Operation3));
            }

            if (operation is IOperationGroup <IOperationBase, IOperationBase, IOperationBase, IOperationBase> )
            {
                var operationTyped = (IOperationGroup <IOperationBase, IOperationBase, IOperationBase, IOperationBase>)operation;
                return(BenchmarkAndExecuteInner_Group(benchmarkProcessorConfiguration, operationTyped, operationTyped.Operation1, operationTyped.Operation2, operationTyped.Operation3, operationTyped.Operation4));
            }

            if (operation is IOperationGroup <IOperationBase, IOperationBase, IOperationBase, IOperationBase, IOperationBase> )
            {
                var operationTyped = (IOperationGroup <IOperationBase, IOperationBase, IOperationBase, IOperationBase, IOperationBase>)operation;
                return(BenchmarkAndExecuteInner_Group(benchmarkProcessorConfiguration, operationTyped, operationTyped.Operation1, operationTyped.Operation2, operationTyped.Operation3, operationTyped.Operation4, operationTyped.Operation5));
            }

            if (operation is IOperationWithAction)
            {
                IOperationWithAction operationTyped = (IOperationWithAction)operation;
                return(ExecuteInner_OperationWithAction(operationTyped));
            }

            return(ExecuteInner_OperationWithFunc((dynamic)operation));
        }