Exemplo n.º 1
0
 static MethodInfo MethodHasAppropriateOverload(MethodInfo method, MethodInfo[] methodsOrdered)
 {
     var parameters = method.GetParametersOrdered();
     var name = method.Name;
     return methodsOrdered
         .Where(x => x.Name == name)
         .FirstOrDefault(x => MethodHasOverloadForApiOptions(x, parameters));
 }
        public static Benchmark[] MethodsToBenchmarks(Type containingType, MethodInfo[] methods, IConfig config = null)
        {
            var setupMethod = GetSetupMethod(methods);
            var targetMethods = methods.Where(method => method.HasAttribute<BenchmarkAttribute>()).ToArray();

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

            var rawJobs = config?.GetJobs().ToArray() ?? new IJob[0];
            if (rawJobs.IsEmpty())
                rawJobs = new[] { Job.Default };
            var jobs = rawJobs.Distinct().ToArray();

            var targets = GetTargets(targetMethods, containingType, setupMethod).ToArray();

            var benchmarks = (
                from target in targets
                from job in jobs
                from parameterInstance in parameterInstancesList
                select new Benchmark(target, job, parameterInstance)).ToArray();

            var orderProvider = config?.GetOrderProvider() ?? DefaultOrderProvider.Instance;
            return orderProvider.GetExecutionOrder(benchmarks).ToArray();
        }
Exemplo n.º 3
0
        protected void RunTests(DbEntityProvider provider, string baselineFile, string newBaselineFile, bool executeQueries, MethodInfo[] tests)
        {
            this.provider = provider;
            this.executeQueries = executeQueries;

            ReadBaselines(baselineFile);

            if (!string.IsNullOrEmpty(newBaselineFile))
            {
                baselineWriter = new XmlTextWriter(newBaselineFile, Encoding.UTF8);
                baselineWriter.Formatting = Formatting.Indented;
                baselineWriter.Indentation = 2;
                baselineWriter.WriteStartDocument();
                baselineWriter.WriteStartElement("baselines");
            }

            int iTest = 0;
            int iPassed = 0;
            var failures = new List<TestFailure>();
            ConsoleColor originalColor = Console.ForegroundColor;
            if (WriteOutput)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Running tests: {0}", this.GetType().Name);
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            var timer = new System.Diagnostics.Stopwatch();
            timer.Start();

            try
            {
                foreach (MethodInfo method in tests.Where(m => m != null && TestIsEnabled(m)))
                {
                    iTest++;
                    currentMethod = method;
                    string testName = method.Name.Substring(4);
                    bool passed = false;
                    if (WriteOutput)
                    {
                        Console.WriteLine();
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    SetupTest();
                    string reason = "";
                    try
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                        TestMethod test = (TestMethod)Delegate.CreateDelegate(typeof(TestMethod), this, method);
                        test();
                        if (testName.EndsWith("Fails"))
                        {
                            passed = false;
                            reason = "Expected failure";
                        }
                        else
                        {
                            passed = true;
                            iPassed++;
                        }
                    }
                    catch (Exception tf)//(TestFailureException tf)
                    {
                        if (testName.EndsWith("Fails"))
                        {
                            passed = true;
                            iPassed++;
                        }
                        else if (tf.Message != null)
                        {
                            reason = tf.Message;
                        }
                    }
                    finally
                    {
                        TeardownTest();
                    }

                    if (!passed)
                    {
                        failures.Add(new TestFailure { TestName = method.Name, Reason = reason });
                    }

                    if (WriteOutput)
                    {
                        Console.ForegroundColor = passed ? ConsoleColor.Green : ConsoleColor.Red;
                        Console.WriteLine("Test {0}: {1} - {2}", iTest, method.Name, passed ? "PASSED" : "FAILED");
                        if (!passed && !string.IsNullOrEmpty(reason))
                            Console.WriteLine("Reason: {0}", reason);
                    }
                }
            }
            finally
            {
                if (baselineWriter != null)
                {
                    baselineWriter.WriteEndElement();
                    baselineWriter.Close();
                }
            }

            timer.Stop();

            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("SUMMARY: {0}", this.GetType().Name);
            Console.WriteLine("Total tests run: {0}", iTest);
            Console.WriteLine("Total elapsed time: {0}", timer.Elapsed);

            Console.ForegroundColor = ConsoleColor.Green;
            if (iPassed == iTest)
            {
                Console.WriteLine("ALL tests passed!");
            }
            else
            {
                Console.WriteLine("Total tests passed: {0}", iPassed);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Total tests failed: {0}", iTest - iPassed);
                foreach (var failure in failures)
                {
                    Console.WriteLine("  {0}: {1}", failure.TestName, failure.Reason != null ? failure.Reason : string.Empty);
                }
            }
            Console.ForegroundColor = originalColor;
            Console.WriteLine();
        }
Exemplo n.º 4
0
 void ObjectFactory(object instance, MethodInfo[] methods)
 {
     var name = new Regex("(Object)?Factory", RegexOptions.IgnoreCase);
     var objectFactory = methods.Where(m =>
         m.ReturnType == typeof(object)
         && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(new[] { typeof(Type) })
         && name.IsMatch(m.Name))
         .SingleOrDefault();
     if (null != objectFactory)
     {
         _configuration.Factory = (Func<Type, object>)Delegate.CreateDelegate(typeof(Func<Type, object>), instance, objectFactory);
     }
 }
 private static IEnumerable<Target> GetTargets(MethodInfo[] targetMethods, Type type, MethodInfo setupMethod) => targetMethods.
     Where(m => m.HasAttribute<BenchmarkAttribute>()).
     Select(methodInfo => CreateTarget(type, setupMethod, methodInfo, methodInfo.ResolveAttribute<BenchmarkAttribute>(), targetMethods));
Exemplo n.º 6
0
 private static MethodInfo GetMatchingMethod(MethodInfo[] methods2, MethodInfo method1)
 {
     ParameterInfo[] method1Params = method1.GetParameters();
     foreach (MethodInfo m in methods2.Where(m => m.Name == method1.Name))
     {
         if (!AreTypesEqual(m.ReturnType, method1.ReturnType))
         {
             continue;
         }
         ParameterInfo[] mParams = m.GetParameters();
         if (mParams.Count() == method1Params.Count())
         {
             bool found = true;
             for (int i = 0; i < mParams.Count(); i++)
             {
                 if (!AreTypesEqual(method1Params[i].ParameterType, mParams[i].ParameterType))
                 {
                     found = false;
                     break;
                 }
             }
             if (found)
             {
                 return m;
             }
         }
     }
     return null;
 }