Benchmarque.Console.Program.Main C# (CSharp) Method

Main() static private method

static private Main ( string argv ) : void
argv string
return void
        static void Main(string[] argv)
        {
            if (argv.Length == 0)
            {
                Console.WriteLine("usage: <assembly name> [filter]");
                return;
            }

            string subjectAssembly = argv[0];
            Console.WriteLine("Subject Assembly: {0}", subjectAssembly);

            string subjectPath = Path.GetFullPath(subjectAssembly);

            string privatePath = Path.GetDirectoryName(subjectPath);

            Assembly specs;
            try
            {
                IEnumerable<string> assembliesToLoad = GetDependentAssemblies(subjectPath);
                foreach (string loadAssemblyName in assembliesToLoad)
                {
                    string loadAssemblyPath = Path.Combine(privatePath, loadAssemblyName);

                    Assembly.LoadFrom(loadAssemblyPath);
                }

                Console.WriteLine("Loading subject assembly: {0}", subjectPath);
                specs = Assembly.LoadFrom(subjectPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to load assembly: {0}", ex.Message);
                return;
            }

            string filter = null;
            if (argv.Length >= 2)
                filter = argv[1];

            Type[] specTypes = specs.GetTypes();

            var types = specTypes
                .Where(type => type.HasInterface(typeof(Benchmark<>)) && type.IsConcreteType())
                .Select(type => new
                    {
                        Type = type,
                        InputType = type.GetInterface(typeof(Benchmark<>)).GetGenericArguments()[0]
                    })
                .Where(type => filter == null || type.InputType.Name.Contains(filter))
                .OrderBy(x => x.InputType.Name);

            foreach (var benchmark in types)
            {
                Type[] subjectTypes = specTypes
                    .Where(type => type.HasInterface(benchmark.InputType))
                    .Where(type => type.IsConcreteType())
                    .OrderBy(x => x.Name)
                    .ToArray();

                Type runnerType = typeof(BenchmarkRunner<>).MakeGenericType(benchmark.InputType);
                var runner = (BenchmarkRunner)Activator.CreateInstance(runnerType, benchmark.Type, subjectTypes);

                IEnumerable<RunResult> results = runner.Run();

                Display(results);
            }
        }