Пример #1
0
        static public void Main(string[] args)
        {
            var _benchmark = new Benchmark(() => new Action(() => new Calculator()));

            _benchmark.Add("SimpleInjector", () =>
            {
                var _container = new SimpleInjector.Container();
                _container.Register <ICalculator, Calculator>(SimpleInjector.Lifestyle.Transient);
                return(() => _container.GetInstance <ICalculator>());
            });
            //TODO : change to test new Puresharp DI recast
            _benchmark.Add("Puresharp", () =>
            {
                var _container = new Puresharp.Composition.Container();
                _container.Add <ICalculator>(() => new Calculator(), Puresharp.Composition.Lifetime.Volatile);
                return(() => _container.Enumerable <ICalculator>());
            });
            //TODO : change to test MEF2
            _benchmark.Add("MEF", () =>
            {
                var _container = new System.Composition.Hosting.ContainerConfiguration().WithAssembly(typeof(ICalculator).Assembly).CreateContainer();
                return(() => _container.GetExport <ICalculator>());
            });
            _benchmark.Add("Castle Windsor", () =>
            {
                var _container = new WindsorContainer();
                _container.Register(Castle.MicroKernel.Registration.Component.For <ICalculator>().ImplementedBy <Calculator>());
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("Unity", () =>
            {
                var _container = new UnityContainer();
                _container.RegisterType <ICalculator, Calculator>();
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("StuctureMap", () =>
            {
                var _container = new StructureMap.Container(_Builder => _Builder.For <ICalculator>().Use <Calculator>());
                return(() => _container.GetInstance <ICalculator>());
            });
            _benchmark.Add("DryIoc", () =>
            {
                var _container = new DryIoc.Container();
                _container.Register <ICalculator, Calculator>();
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("Autofac", () =>
            {
                var _builder = new Autofac.ContainerBuilder();
                _builder.RegisterType <Calculator>().As <ICalculator>();
                var _container = _builder.Build(Autofac.Builder.ContainerBuildOptions.None);
                return(() => _container.Resolve <ICalculator>());
            });
            _benchmark.Add("Ninject", () =>
            {
                var _container = new Ninject.StandardKernel();
                _container.Bind <ICalculator>().To <Calculator>();
                return(() => _container.Get <ICalculator>());
            });
            _benchmark.Add("Abioc", () =>
            {
                var _setup = new Abioc.Registration.RegistrationSetup();
                _setup.Register <ICalculator, Calculator>();
                var _container = Abioc.ContainerConstruction.Construct(_setup, typeof(ICalculator).Assembly);
                return(() => _container.GetService <ICalculator>());
            });
            _benchmark.Add("Grace", () =>
            {
                var _container = new Grace.DependencyInjection.DependencyInjectionContainer();
                _container.Configure(c => c.Export <Calculator>().As <ICalculator>());
                return(() => _container.Locate <ICalculator>());
            });
            _benchmark.Run(Console.WriteLine);
        }
Пример #2
0
        static void Main(string[] args)
        {
            //Create benchmark with object parameterless constructor call as reference action.
            var _benchmark = new Benchmark(() => new Action(() => new object()));

            //Add Activator.CreateInstance call as alternative.
            _benchmark.Add("Activator", () =>
            {
                var _type = typeof(object);
                return(new Action(() => { Activator.CreateInstance(_type); }));
            });

            //Add Activator.CreateInstance call as alternative.
            _benchmark.Add("Generic Activator", () =>
            {
                return(new Action(() => { Activator.CreateInstance <object>(); }));
            });

            //Add ConstructorInfo.Invoke as alternative
            _benchmark.Add("ConstructorInfo", () =>
            {
                var _constructor = typeof(object).GetConstructor(Type.EmptyTypes);
                var _arguments   = new object[0];
                return(new Action(() => { _constructor.Invoke(_arguments); }));
            });

            //Add Lambda as alternative
            _benchmark.Add("Lambda", () =>
            {
                var _activate = new Func <object>(() => new object());
                return(new Action(() => { _activate(); }));
            });

            //Add Lambda Expression as alternative
            _benchmark.Add("Expression", () =>
            {
                var _activate = Expression.Lambda <Func <object> >(Expression.New(typeof(object))).Compile();
                return(new Action(() => { _activate(); }));
            });

            //Add FormatterServices.GetUninitializedObject as alternative
            _benchmark.Add("FormatterServices", () =>
            {
                var _type = typeof(object);
                return(new Action(() => { FormatterServices.GetUninitializedObject(_type); }));
            });

            //Add DynamicMethod as alternative
            _benchmark.Add("DynamicMethod", () =>
            {
                var _type   = typeof(object);
                var _method = new DynamicMethod(string.Empty, _type, new Type[] { _type }, true);
                var _body   = _method.GetILGenerator();
                _body.Emit(OpCodes.Newobj, _type.GetConstructor(Type.EmptyTypes));
                _body.Emit(OpCodes.Ret);
                var _activate = _method.CreateDelegate(typeof(Func <object>), null) as Func <object>;
                return(new Action(() => { _activate(); }));
            });

            //Run benchmark.
            _benchmark.Run();
        }