Exemplo n.º 1
0
        private static BenchmarkRunInfo MethodsToBenchmarksWithFullConfig(Type containingType, MethodInfo[] benchmarkMethods, ReadOnlyConfig fullConfig)
        {
            if (fullConfig == null)
            {
                throw new ArgumentNullException(nameof(fullConfig));
            }

            var helperMethods = containingType.GetMethods(); // benchmarkMethods can be filtered, without Setups, look #564

            var globalSetupMethods      = GetAttributedMethods <GlobalSetupAttribute>(helperMethods, "GlobalSetup");
            var globalCleanupMethods    = GetAttributedMethods <GlobalCleanupAttribute>(helperMethods, "GlobalCleanup");
            var iterationSetupMethods   = GetAttributedMethods <IterationSetupAttribute>(helperMethods, "IterationSetup");
            var iterationCleanupMethods = GetAttributedMethods <IterationCleanupAttribute>(helperMethods, "IterationCleanup");

            var targetMethods = benchmarkMethods.Where(method => method.HasAttribute <BenchmarkAttribute>()).ToArray();

            var parameterDefinitions   = GetParameterDefinitions(containingType);
            var parameterInstancesList = parameterDefinitions.Expand();

            var rawJobs = fullConfig.GetJobs().ToArray();

            if (rawJobs.IsEmpty())
            {
                rawJobs = new[] { Job.Default }
            }
            ;
            var jobs = rawJobs.Distinct().ToArray();

            var targets = GetTargets(targetMethods, containingType, globalSetupMethods, globalCleanupMethods, iterationSetupMethods, iterationCleanupMethods).ToArray();

            var benchmarks = new List <Benchmark>();

            foreach (var target in targets)
            {
                var argumentsDefinitions = GetArgumentsDefinitions(target.Method, target.Type).ToArray();

                benchmarks.AddRange(
                    from job in jobs
                    from parameterInstance in parameterInstancesList
                    from argumentDefinition in argumentsDefinitions
                    select Benchmark.Create(target, job, new ParameterInstances(parameterInstance.Items.Concat(argumentDefinition.Items).ToArray()))
                    );
            }

            var filters            = fullConfig.GetFilters().ToList();
            var filteredBenchmarks = GetFilteredBenchmarks(benchmarks, filters);

            var orderProvider = fullConfig.GetOrderProvider() ?? DefaultOrderProvider.Instance;

            return(new BenchmarkRunInfo(
                       orderProvider.GetExecutionOrder(filteredBenchmarks).ToArray(),
                       containingType,
                       fullConfig));
        }
        public static BenchmarkRunInfo[] SourceToBenchmarks(string source, IConfig config = null)
        {
            string benchmarkContent   = source;
            var    cSharpCodeProvider = new CSharpCodeProvider();
            var    compilerParameters = new CompilerParameters(
                new[]
            {
                "mscorlib.dll",
                "System.dll",
                "System.Core.dll"
            })
            {
                CompilerOptions  = "/unsafe /optimize",
                GenerateInMemory = false,
                OutputAssembly   = Path.Combine(
                    Path.GetDirectoryName(typeof(Benchmark).Assembly.Location),
                    $"{Path.GetFileNameWithoutExtension(Path.GetTempFileName())}.dll")
            };

            compilerParameters.ReferencedAssemblies.Add(typeof(Benchmark).Assembly.Location);
            var compilerResults = cSharpCodeProvider.CompileAssemblyFromSource(compilerParameters, benchmarkContent);

            if (compilerResults.Errors.HasErrors)
            {
                var logger = config?.GetCompositeLogger() ?? HostEnvironmentInfo.FallbackLogger;

                compilerResults.Errors.Cast <CompilerError>().ToList().ForEach(error => logger.WriteLineError(error.ErrorText));
                return(Array.Empty <BenchmarkRunInfo>());
            }

            var types = compilerResults.CompiledAssembly.GetTypes();

            var resultBenchmarks = new List <BenchmarkRunInfo>();

            foreach (var type in types)
            {
                var runInfo    = TypeToBenchmarks(type, config);
                var benchmarks = runInfo.Benchmarks.Select(b =>
                {
                    var target = b.Target;
                    return(Benchmark.Create(
                               new Target(target.Type, target.Method, target.GlobalSetupMethod, target.GlobalCleanupMethod,
                                          target.IterationSetupMethod, target.IterationCleanupMethod,
                                          target.MethodDisplayInfo, benchmarkContent, target.Baseline, target.Categories, target.OperationsPerInvoke),
                               b.Job,
                               b.Parameters));
                });
                resultBenchmarks.Add(
                    new BenchmarkRunInfo(benchmarks.ToArray(), runInfo.Type, runInfo.Config));
            }

            return(resultBenchmarks.ToArray());
        }