Exemple #1
0
        private IEnumerable <Summary> RunBenchmarks(string[] args, IConfig config)
        {
            var globalChronometer = Chronometer.Start();
            var summaries         = new List <Summary>();

            if (ShouldDisplayOptions(args))
            {
                DisplayOptions();
                return(Enumerable.Empty <Summary>());
            }

            var  effectiveConfig = ManualConfig.Union(config ?? DefaultConfig.Instance, ManualConfig.Parse(args));
            bool join            = args.Any(arg => arg.EqualsWithIgnoreCase("--join"));

            if (join)
            {
                var typesWithMethods = typeParser.MatchingTypesWithMethods(args);
                var benchmarks       = typesWithMethods.SelectMany(typeWithMethods =>
                                                                   typeWithMethods.AllMethodsInType
                        ? BenchmarkConverter.TypeToBenchmarks(typeWithMethods.Type, effectiveConfig)
                        : BenchmarkConverter.MethodsToBenchmarks(typeWithMethods.Type, typeWithMethods.Methods, effectiveConfig)).ToArray();
                summaries.Add(BenchmarkRunner.Run(benchmarks, effectiveConfig));
            }
            else
            {
                foreach (var typeWithMethods in typeParser.MatchingTypesWithMethods(args))
                {
                    logger.WriteLineHeader("Target type: " + typeWithMethods.Type.Name);
                    if (typeWithMethods.AllMethodsInType)
                    {
                        summaries.Add(BenchmarkRunner.Run(typeWithMethods.Type, effectiveConfig));
                    }
                    else
                    {
                        summaries.Add(BenchmarkRunner.Run(typeWithMethods.Type, typeWithMethods.Methods, effectiveConfig));
                    }
                    logger.WriteLine();
                }
            }

            // TODO: move this logic to the RunUrl method
#if CLASSIC
            if (args.Length > 0 && (args[0].StartsWith("http://") || args[0].StartsWith("https://")))
            {
                var url  = args[0];
                Uri uri  = new Uri(url);
                var name = uri.IsFile ? Path.GetFileName(uri.LocalPath) : "URL";
                summaries.Add(BenchmarkRunner.RunUrl(url, effectiveConfig));
            }
#endif

            var clockSpan = globalChronometer.Stop();
            BenchmarkRunnerCore.LogTotalTime(logger, clockSpan.GetTimeSpan(), "Global total time");
            return(summaries);
        }
        private IEnumerable <Summary> RunBenchmarks(string[] args, IConfig config)
        {
            var globalChronometer = Chronometer.Start();
            var summaries         = new List <Summary>();

            if (ShouldDisplayOptions(args))
            {
                DisplayOptions();
                return(Enumerable.Empty <Summary>());
            }

            var  effectiveConfig = ManualConfig.Union(config ?? DefaultConfig.Instance, ManualConfig.Parse(args));
            bool join            = args.Any(arg => arg.EqualsWithIgnoreCase("--join"));

            var benchmarks = typeParser.MatchingTypesWithMethods(args)
                             .Select(typeWithMethods =>
                                     typeWithMethods.AllMethodsInType
                        ? BenchmarkConverter.TypeToBenchmarks(typeWithMethods.Type, effectiveConfig)
                        : BenchmarkConverter.MethodsToBenchmarks(typeWithMethods.Type, typeWithMethods.Methods, effectiveConfig))
                             .ToArray();

            summaries.AddRange(BenchmarkRunner.Run(benchmarks, effectiveConfig, summaryPerType: !join));

            var clockSpan = globalChronometer.GetElapsed();

            BenchmarkRunner.LogTotalTime(logger, clockSpan.GetTimeSpan(), "Global total time");
            return(summaries);
        }
        public void CanSelectClassesWithFullName()
        {
            var types = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC) };
            var typeParser = new TypeParser(types, ConsoleLogger.Default);

            var matches = typeParser.MatchingTypesWithMethods(new[] { "class=BenchmarkDotNet.Tests.ClassC,BenchmarkDotNet.Tests.ClassA" });

            // ClassC not matched as it has NO methods with the [Benchmark] attribute
            Assert.Equal(1, matches.Count());
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassA" && match.AllMethodsInType));
        }
        public void CanSelectNamespaces()
        {
            var types = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(NOTTests.ClassD) };
            var typeParser = new TypeParser(types, ConsoleLogger.Default);

            var matches = typeParser.MatchingTypesWithMethods(new[] { "namespace=BenchmarkDotNet.Tests" });

            Assert.Equal(2, matches.Count());
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassA" && match.AllMethodsInType));
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassB" && match.AllMethodsInType));
        }
        public void CanSelectAttributes()
        {
            var types = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(NOTTests.ClassD) };
            var typeParser = new TypeParser(types, ConsoleLogger.Default);

            var matches = typeParser.MatchingTypesWithMethods(new[] { "attribute=Run" });

            // Find entire classes or individual methods that have the [Run] attribute
            Assert.Equal(2, matches.Count());
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassA" && match.AllMethodsInType));
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassD" &&
                                                   match.Methods.Any(m => m.Name == "Method1")));
        }
        public void CanSelectClasses()
        {
            var types = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC) };
            var typeParser = new TypeParser(types, ConsoleLogger.Default);

            var matches = typeParser.MatchingTypesWithMethods(new[] { "class=ClassC,ClassA" });
            // TODO do we want to allow "class = ClassC, ClassA" aswell as "class=ClassC,ClassA"
            //var matches = typeParser.MatchingTypesWithMethods(new[] { "class = ClassC, ClassA" });

            // ClassC not matched as it has NO methods with the [Benchmark] attribute
            Assert.Equal(1, matches.Count());
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassA" && match.AllMethodsInType));
        }
        public void CanSelectMethodsWithFullName()
        {
            var types = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC) };
            var typeParser = new TypeParser(types, ConsoleLogger.Default);

            var matches = typeParser.MatchingTypesWithMethods(new[] { "method=BenchmarkDotNet.Tests.ClassA.Method2,BenchmarkDotNet.Tests.ClassB.Method3" });

            Assert.Equal(2, matches.Count());
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassA" &&
                                                   match.Methods.Any(m => m.Name == "Method2")));
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassB" &&
                                                   match.Methods.Any(m => m.Name == "Method3")));
        }
Exemple #8
0
        private IEnumerable <Summary> RunBenchmarks(string[] args)
        {
            var globalChronometer = Chronometer.Start();
            var summaries         = new List <Summary>();

            if (ShouldDisplayOptions(args))
            {
                DisplayOptions();
                return(Enumerable.Empty <Summary>());
            }

            var config = ManualConfig.Union(DefaultConfig.Instance, ManualConfig.Parse(args));

            foreach (var typeWithMethods in typeParser.MatchingTypesWithMethods(args))
            {
                logger.WriteLineHeader("Target type: " + typeWithMethods.Type.Name);
                if (typeWithMethods.AllMethodsInType)
                {
                    summaries.Add(BenchmarkRunner.Run(typeWithMethods.Type, config));
                }
                else
                {
                    summaries.Add(BenchmarkRunner.Run(typeWithMethods.Type, typeWithMethods.Methods, config));
                }
                logger.WriteLine();
            }

            // TODO: move this logic to the RunUrl method
#if CLASSIC
            if (args.Length > 0 && (args[0].StartsWith("http://") || args[0].StartsWith("https://")))
            {
                var url  = args[0];
                Uri uri  = new Uri(url);
                var name = uri.IsFile ? Path.GetFileName(uri.LocalPath) : "URL";
                summaries.Add(BenchmarkRunner.RunUrl(url, config));
            }
#endif

            var clockSpan = globalChronometer.Stop();
            BenchmarkRunner.LogTotalTime(logger, clockSpan.GetTimeSpan(), "Global total time");
            return(summaries);
        }
        public void EntireClassOverridesIndividualMethods()
        {
            var types = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC) };
            var typeParser = new TypeParser(types, ConsoleLogger.Default);

            var matches = typeParser.MatchingTypesWithMethods(new[] { "method=Method2,Method3", "class=ClassA" });

            Assert.Equal(2, matches.Count());
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassA" && match.AllMethodsInType));
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassB" &&
                                                   match.Methods.Any(m => m.Name == "Method2") &&
                                                   match.Methods.Any(m => m.Name == "Method3")));
        }
        public void CanSelectPluralVersions()
        {
            var types = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(NOTTests.ClassD) };
            var typeParser = new TypeParser(types, ConsoleLogger.Default);

            // Note we are using "classes" here rather than "class" (we want to be nicer to our users!!)
            // Likewise you can also use "methods" and "namespaces"
            var matches = typeParser.MatchingTypesWithMethods(new[] { "classes=ClassC,ClassA", "methods=Method2" });

            // ClassC not matched as it has NO methods with the [Benchmark] attribute
            Assert.Equal(3, matches.Count());
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassA" && match.AllMethodsInType));
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassB" && match.Methods.Any(m => m.Name == "Method2")));
            Assert.Equal(1, matches.Count(match => match.Type.Name == "ClassD" && match.Methods.Any(m => m.Name == "Method2")));
        }