Example #1
0
 public BenchmarkTask(int processCount, BenchmarkConfiguration configuration, BenchmarkSettings settings, BenchmarkParams @params = null)
 {
     ProcessCount = processCount;
     Configuration = configuration;
     Settings = settings;
     Params = @params;
 }
        private static IEnumerable<Benchmark> CompetitionToBenchmarks(object competition, BenchmarkSettings defaultSettings)
        {
            if (defaultSettings == null)
                defaultSettings = BenchmarkSettings.CreateDefault();
            var targetType = competition.GetType();
            var methods = targetType.GetMethods();
            var setupMethod = methods.FirstOrDefault(m => m.ResolveAttribute<SetupAttribute>() != null);
            if (setupMethod != null)
            {
                // setupMethod is optional, but if it's there it must have the correct signature, accessibility, etc
                AssertMethodHasCorrectSignature("Setup", setupMethod);
                AssertMethodIsAccessible("Setup", setupMethod);
                AssertMethodIsNotDeclaredInGeneric("Setup", setupMethod);
                AssertMethodIsNotGeneric("Setup", setupMethod);
            }

            // If there is one, get the single Field or Property that has the [Params(..)] attribute
            var fields = targetType.GetFields().Select(f => new
                {
                    f.Name,
                    Attribute = f.ResolveAttribute<ParamsAttribute>(),
                    IsStatic = f.IsStatic,
                });
            var properties = targetType.GetProperties().Select(f => new
                {
                    f.Name,
                    Attribute = f.ResolveAttribute<ParamsAttribute>(),
                    IsStatic = f.GetSetMethod().IsStatic
                });
            var fieldOrProperty = fields.Concat(properties).FirstOrDefault(i => i.Attribute != null);

            for (int i = 0; i < methods.Length; i++)
            {
                var methodInfo = methods[i];
                var benchmarkAttribute = methodInfo.ResolveAttribute<BenchmarkAttribute>();
                if (benchmarkAttribute != null)
                {
                    var target = new BenchmarkTarget(targetType, methodInfo, setupMethod, benchmarkAttribute.Description);
                    AssertMethodHasCorrectSignature("Benchmark", methodInfo);
                    AssertMethodIsAccessible("Benchmark", methodInfo);
                    AssertMethodIsNotDeclaredInGeneric("Benchmark", methodInfo);
                    AssertMethodIsNotGeneric("Benchmark", methodInfo);
                    foreach (var task in BenchmarkTask.Resolve(methodInfo, defaultSettings))
                    {
                        if (fieldOrProperty == null)
                        {
                            yield return new Benchmark(target, task);
                        }
                        else
                        {
                            var @params = new BenchmarkParams(fieldOrProperty.Name, fieldOrProperty.IsStatic, fieldOrProperty.Attribute.Args);
                            // All the properties of BenchmarkTask and it's children are immutable, so cloning a BenchmarkTask like this should be safe
                            var newTask = new BenchmarkTask(task.ProcessCount, task.Configuration, task.Settings, @params);
                            yield return new Benchmark(target, newTask);
                        }
                    }
                }
            }
        }