コード例 #1
0
        public NetCoreAssemblyRuntimeLoader(string path, IBenchmarkOutput trace)
        {
            _trace = trace;
            if (!File.Exists(path))
            {
                trace.Error($"[NetCoreAssemblyRuntimeLoader] Unable to find requested assembly [{path}]");
                return;
            }

            Assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
            if (Assembly == null)
            {
                trace.Error($"[NetCoreAssemblyRuntimeLoader] Found assembly [{path}], but was unable to load it.");
                return;
            }
            _dependencyContext = DependencyContext.Load(Assembly);
            _loadContext       = AssemblyLoadContext.GetLoadContext(Assembly);
            _resolver          = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[] {
                new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(path)),
                new ReferenceAssemblyPathResolver(),
                new PackageCompilationAssemblyResolver()
            });

            _loadContext.Resolving += LoadContextOnResolving;
            _referencedAssemblies   = new Lazy <Assembly[]>(LoadReferencedAssemblies);
        }
コード例 #2
0
 public static IEnumerable <Type> LoadAllTypeConfigurators(Assembly assembly, IBenchmarkOutput output = null)
 {
     using (var loader = AssemblyRuntimeLoader.WrapAssembly(assembly, output))
     {
         return(LoadAllTypeConfigurators(loader));
     }
 }
コード例 #3
0
 public AssemblyRuntimeLoader(Assembly assembly, IBenchmarkOutput trace)
 {
     _trace           = trace;
     Assembly         = assembly;
     _binaryDirectory = Path.GetDirectoryName(Assembly.CodeBase);
     AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
     _referencedAssemblies = new Lazy <Assembly[]>(LoadReferencedAssemblies);
 }
コード例 #4
0
 public Benchmark(BenchmarkSettings settings, IBenchmarkInvoker invoker, IBenchmarkOutput writer)
 {
     Settings           = settings;
     _pendingIterations = Settings.NumberOfIterations;
     Invoker            = invoker;
     Output             = writer;
     CompletedRuns      = new Queue <BenchmarkRunReport>(Settings.NumberOfIterations);
     Builder            = new BenchmarkBuilder(Settings);
 }
コード例 #5
0
        /// <summary>
        /// Loads an assembly into the current AppDomain
        /// </summary>
        /// <param name="assembly">An already-loaded assembly.</param>
        /// <param name="trace">Optional. Benchmark tracing system.</param>
        /// <returns>An <see cref="IAssemblyLoader"/> with a reference to the <see cref="Assembly"/> at the specified location.</returns>
        public static IAssemblyLoader WrapAssembly(Assembly assembly, IBenchmarkOutput trace = null)
        {
            trace = trace ?? NoOpBenchmarkOutput.Instance;
#if CORECLR
            return(new NetCoreAssemblyRuntimeLoader(assembly, trace));
#else
            return(new NetFrameworkAssemblyRuntimeLoader(assembly, trace));
#endif
        }
コード例 #6
0
ファイル: Benchmark.cs プロジェクト: ThomasBombadil/NBench
 public Benchmark(BenchmarkSettings settings, IBenchmarkInvoker invoker, IBenchmarkOutput writer)
 {
     Settings = settings;
     _pendingIterations = Settings.NumberOfIterations;
     Invoker = invoker;
     Output = writer;
     CompletedRuns = new Queue<BenchmarkRunReport>(Settings.NumberOfIterations);
     Builder = new BenchmarkBuilder(Settings);
 }
コード例 #7
0
        /// <summary>
        /// Executes the tests
        /// </summary>
        /// <returns>True if all tests passed.</returns>
        public TestRunnerResult Execute()
        {
            // Perform core / thread optimizations if we're running in single-threaded mode
            // But not if the user has specified that they're going to be running multi-threaded benchmarks
            SetProcessPriority(_package.Concurrent);

            IBenchmarkOutput output = CreateOutput();
            var discovery           = new ReflectionDiscovery(output);
            var result = new TestRunnerResult()
            {
                AllTestsPassed = true
            };

            try
            {
                foreach (var testFile in _package.Files)
                {
                    var assembly = AssemblyRuntimeLoader.LoadAssembly(testFile);

                    var benchmarks = discovery.FindBenchmarks(assembly);

                    foreach (var benchmark in benchmarks)
                    {
                        // verify if the benchmark should be included/excluded from the list of benchmarks to be run
                        if (_package.ShouldRunBenchmark(benchmark.BenchmarkName))
                        {
                            output.WriteLine($"------------ STARTING {benchmark.BenchmarkName} ---------- ");
                            benchmark.Run();
                            benchmark.Finish();

                            // if one assert fails, all fail
                            result.AllTestsPassed = result.AllTestsPassed && benchmark.AllAssertsPassed;
                            output.WriteLine($"------------ FINISHED {benchmark.BenchmarkName} ---------- ");
                            result.ExecutedTestsCount = result.ExecutedTestsCount + 1;
                        }
                        else
                        {
                            output.WriteLine($"------------ NOTRUN {benchmark.BenchmarkName} ---------- ");
                            result.IgnoredTestsCount = result.IgnoredTestsCount + 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                output.Error(ex, "Error while executing the tests.");
                result.AllTestsPassed = false;
            }

            return(result);
        }
コード例 #8
0
 public ReflectionDiscovery(IBenchmarkOutput output, IBenchmarkAssertionRunner benchmarkAssertions, RunnerSettings settings)
 {
     Output = output;
     BenchmarkAssertions = benchmarkAssertions;
     RunnerSettings      = settings;
     if (RunnerSettings.TracingEnabled)
     {
         Trace = new BenchmarkOutputTrace(Output);
     }
     else
     {
         Trace = NoOpBenchmarkTrace.Instance;
     }
 }
コード例 #9
0
        public NetCoreAssemblyRuntimeLoader(Assembly assembly, IBenchmarkOutput trace)
        {
            _trace   = trace;
            Assembly = assembly;

            _dependencyContext = DependencyContext.Load(Assembly);
            _loadContext       = AssemblyLoadContext.GetLoadContext(Assembly);
            _resolver          = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[] {
                new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(Assembly.CodeBase)),
                new ReferenceAssemblyPathResolver(),
                new PackageCompilationAssemblyResolver()
            });

            _loadContext.Resolving += LoadContextOnResolving;
            _referencedAssemblies   = new Lazy <Assembly[]>(LoadReferencedAssemblies);
        }
コード例 #10
0
        public AssemblyRuntimeLoader(string path, IBenchmarkOutput trace)
        {
            _trace = trace;
            if (!File.Exists(path))
            {
                trace.Error($"[NetFrameworkAssemblyRuntimeLoader] Unable to find requested assembly [{path}]");
                return;
            }

            Assembly = System.Reflection.Assembly.LoadFile(path);

            if (Assembly == null)
            {
                trace.Error($"[NetFrameworkAssemblyRuntimeLoader] Found assembly [{path}], but was unable to load it.");
                return;
            }

            _binaryDirectory = Path.GetDirectoryName(path);

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
            _referencedAssemblies = new Lazy <Assembly[]>(LoadReferencedAssemblies);
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: twenzel/NBench
        /// <summary>
        /// NBench Runner takes the following <see cref="args"/>
        ///
        /// C:\> NBench.Runner.exe [assembly name] [output-directory={dir-path}]
        ///
        /// </summary>
        /// <param name="args">The commandline arguments</param>
        static int Main(string[] args)
        {
            Output    = new CompositeBenchmarkOutput(new ConsoleBenchmarkOutput(), new MarkdownBenchmarkOutput(CommandLine.GetProperty("output-directory")));
            Discovery = new ReflectionDiscovery(Output);
            string assemblyPath = Path.GetFullPath(args[0]);

            // TODO: See issue https://github.com/petabridge/NBench/issues/3
            var assembly = AssemblyRuntimeLoader.LoadAssembly(assemblyPath);


            /*
             * Set processor affinity
             */
            Process Proc = Process.GetCurrentProcess();

            Proc.ProcessorAffinity = new IntPtr(2); // either of the first two processors

            /*
             * Set priority
             */
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;


            var  benchmarks        = Discovery.FindBenchmarks(assembly);
            bool anyAssertFailures = false;

            foreach (var benchmark in benchmarks)
            {
                Output.WriteLine($"------------ STARTING {benchmark.BenchmarkName} ---------- ");
                benchmark.Run();
                benchmark.Finish();

                // if one assert fails, all fail
                anyAssertFailures = anyAssertFailures || !benchmark.AllAssertsPassed;
                Output.WriteLine($"------------ FINISHED {benchmark.BenchmarkName} ---------- ");
            }
            return(anyAssertFailures ? -1 : 0);
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: ThomasBombadil/NBench
        /// <summary>
        /// NBench Runner takes the following <see cref="args"/>
        /// 
        /// C:\> NBench.Runner.exe [assembly name] [output-directory={dir-path}]
        /// 
        /// </summary>
        /// <param name="args">The commandline arguments</param>
        static int Main(string[] args)
        {
            Output = new CompositeBenchmarkOutput(new ConsoleBenchmarkOutput(), new MarkdownBenchmarkOutput(CommandLine.GetProperty("output-directory")));
            Discovery = new ReflectionDiscovery(Output);
            string assemblyPath = Path.GetFullPath(args[0]);

            // TODO: See issue https://github.com/petabridge/NBench/issues/3
            var assembly = AssemblyRuntimeLoader.LoadAssembly(assemblyPath);


            /*
             * Set processor affinity
             */
            Process Proc = Process.GetCurrentProcess();
            Proc.ProcessorAffinity = new IntPtr(2); // either of the first two processors

            /*
             * Set priority
             */
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;


            var benchmarks = Discovery.FindBenchmarks(assembly);
            bool anyAssertFailures = false;
            foreach (var benchmark in benchmarks)
            {
                Output.WriteLine($"------------ STARTING {benchmark.BenchmarkName} ---------- ");
                benchmark.Run();
                benchmark.Finish();

                // if one assert fails, all fail
                anyAssertFailures = anyAssertFailures || !benchmark.AllAssertsPassed;
                Output.WriteLine($"------------ FINISHED {benchmark.BenchmarkName} ---------- ");
            }
            return anyAssertFailures ? -1 : 0;
        }
コード例 #13
0
ファイル: TestPackage.cs プロジェクト: yycmmc/NBench-1
 public TestPackage AddOutput(IBenchmarkOutput output)
 {
     _outputs.Add(output);
     return(this);
 }
コード例 #14
0
 public ReflectionDiscovery(IBenchmarkOutput output, IBenchmarkAssertionRunner benchmarkAssertions)
 {
     Output = _reflectionOutput = output;
     BenchmarkAssertions = benchmarkAssertions;
 }
コード例 #15
0
 private BenchmarkRunCache(IBenchmarkOutput output)
 {
     discovery = new ReflectionDiscovery(output);
 }
コード例 #16
0
ファイル: Benchmark.cs プロジェクト: tuongntk/NBench
 /// <summary>
 /// Backwards-compatible constructor for NBench 0.1.6 and earlier.
 /// </summary>
 /// <param name="settings">The settings for this benchmark.</param>
 /// <param name="invoker">The invoker used to execute benchmark and setup / cleanup methods.</param>
 /// <param name="writer">The output target this benchmark will write to.</param>
 /// <remarks>Uses the <see cref="DefaultBenchmarkAssertionRunner"/> to assert benchmark data.</remarks>
 public Benchmark(BenchmarkSettings settings, IBenchmarkInvoker invoker, IBenchmarkOutput writer)
     : this(settings, invoker, writer, DefaultBenchmarkAssertionRunner.Instance)
 {
 }
コード例 #17
0
 public ReflectionDiscovery(IBenchmarkOutput output)
 {
     Output = _reflectionOutput = output;
 }
コード例 #18
0
 public ReflectionDiscovery(IBenchmarkOutput output) : this(output, DefaultBenchmarkAssertionRunner.Instance, new RunnerSettings() { TracingEnabled = false })
 {
 }
コード例 #19
0
 public BenchmarkOutputTrace(IBenchmarkOutput benchmarkOutput)
 {
     _benchmarkOutput = benchmarkOutput;
 }
コード例 #20
0
 public NBenchIntregrationTestWithDependenciesLoadAssembly(ITestOutputHelper output)
 {
     _output          = output;
     _benchmarkOutput = new XunitBenchmarkOutputHelper(output);
 }
コード例 #21
0
 public ReflectionDiscovery(IBenchmarkOutput output)
     : this(output, DefaultBenchmarkAssertionRunner.Instance, new RunnerSettings())
 {
 }
コード例 #22
0
 public ReflectionDiscovery(IBenchmarkOutput output, IBenchmarkAssertionRunner benchmarkAssertions, RunnerSettings settings)
 {
     Output = _reflectionOutput = output;
     BenchmarkAssertions = benchmarkAssertions;
     RunnerSettings = settings;
     if(RunnerSettings.TracingEnabled)
         Trace = new BenchmarkOutputTrace(Output);
     else
         Trace = NoOpBenchmarkTrace.Instance;
 }
コード例 #23
0
ファイル: Benchmark.cs プロジェクト: petabridge/NBench
 /// <summary>
 /// Backwards-compatible constructor for NBench 0.1.6 and earlier.
 /// </summary>
 /// <param name="settings">The settings for this benchmark.</param>
 /// <param name="invoker">The invoker used to execute benchmark and setup / cleanup methods.</param>
 /// <param name="writer">The output target this benchmark will write to.</param>
 /// <remarks>Uses the <see cref="DefaultBenchmarkAssertionRunner"/> to assert benchmark data.</remarks>
 public Benchmark(BenchmarkSettings settings, IBenchmarkInvoker invoker, IBenchmarkOutput writer)
     : this(settings, invoker, writer, DefaultBenchmarkAssertionRunner.Instance)
 {
 }
コード例 #24
0
        /// <summary>
        /// Loads an assembly into the current AppDomain
        /// </summary>
        /// <param name="assemblyPath">The path to an assembly</param>
        /// <param name="trace">Optional. Benchmark tracing system.</param>
        /// <returns>An <see cref="IAssemblyLoader"/> with a reference to the <see cref="Assembly"/> at the specified location.</returns>
        public static IAssemblyLoader LoadAssembly(string assemblyPath, IBenchmarkOutput trace = null)
        {
            trace = trace ?? NoOpBenchmarkOutput.Instance;

            return(new Assemblies.AssemblyRuntimeLoader(assemblyPath, trace));
        }
コード例 #25
0
ファイル: ReflectionDiscovery.cs プロジェクト: twenzel/NBench
 public ReflectionDiscovery(IBenchmarkOutput output)
 {
     Output = _reflectionOutput = output;
 }
コード例 #26
0
 public ReflectionDiscovery(IBenchmarkOutput output) : this(output, DefaultBenchmarkAssertionRunner.Instance)
 {
 }
コード例 #27
0
 public ReflectionDiscoveryConfiguratorSpecs(ITestOutputHelper helper)
 {
     _output = new XunitBenchmarkOutputHelper(helper);
 }
コード例 #28
0
ファイル: TestRunner.cs プロジェクト: tuongntk/NBench
        /// <summary>
        /// Executes the tests
        /// </summary>
        /// <returns>True if all tests passed.</returns>
        public TestRunnerResult Execute()
        {
            // Perform core / thread optimizations if we're running in single-threaded mode
            // But not if the user has specified that they're going to be running multi-threaded benchmarks
            SetProcessPriority(_package.Concurrent);

            // pass in the runner settings so we can include them in benchmark reports
            // also, toggles tracing on or off
            var runnerSettings = new RunnerSettings()
            {
                ConcurrentModeEnabled = _package.Concurrent,
                TracingEnabled        = _package.Tracing
            };

            IBenchmarkOutput output = CreateOutput();


            var discovery = new ReflectionDiscovery(output,
                                                    DefaultBenchmarkAssertionRunner.Instance, // one day we might be able to pass in custom assertion runners, hence why this is here
                                                    runnerSettings);
            var result = new TestRunnerResult()
            {
                AllTestsPassed = true
            };

            try
            {
                foreach (var testFile in _package.Files)
                {
                    var assembly = AssemblyRuntimeLoader.LoadAssembly(testFile);

                    var benchmarks = discovery.FindBenchmarks(assembly);

                    foreach (var benchmark in benchmarks)
                    {
                        // verify if the benchmark should be included/excluded from the list of benchmarks to be run
                        if (_package.ShouldRunBenchmark(benchmark.BenchmarkName))
                        {
                            output.StartBenchmark(benchmark.BenchmarkName);
                            benchmark.Run();
                            benchmark.Finish();

                            // if one assert fails, all fail
                            result.AllTestsPassed = result.AllTestsPassed && benchmark.AllAssertsPassed;
                            output.FinishBenchmark(benchmark.BenchmarkName);
                            result.ExecutedTestsCount = result.ExecutedTestsCount + 1;
                        }
                        else
                        {
                            output.SkipBenchmark(benchmark.BenchmarkName);
                            result.IgnoredTestsCount = result.IgnoredTestsCount + 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                output.Error(ex, "Error while executing the tests.");
                result.AllTestsPassed = false;
            }

            return(result);
        }
コード例 #29
0
        /// <summary>
        /// Loads an assembly into the current AppDomain
        /// </summary>
        /// <param name="assembly">An already-loaded assembly.</param>
        /// <param name="trace">Optional. Benchmark tracing system.</param>
        /// <returns>An <see cref="IAssemblyLoader"/> with a reference to the <see cref="Assembly"/> at the specified location.</returns>
        public static IAssemblyLoader WrapAssembly(Assembly assembly, IBenchmarkOutput trace = null)
        {
            trace = trace ?? NoOpBenchmarkOutput.Instance;

            return(new Assemblies.AssemblyRuntimeLoader(assembly, trace));
        }