public override void Execute()
        {
            var rand     = new Random();
            var fromType = typeof(int[]);
            var toType   = typeof(List <decimal>);

            var intArray = new int[50];

            for (int i = 0; i < 50; i++)
            {
                intArray[i] = rand.Next();
            }

            var toElementType = typeof(decimal);

            var fastConverter = SafeMap.GetConverter <int[], List <decimal> >();
            var emitMapper    = ObjectMapperManager.DefaultInstance.GetMapper <int[], List <decimal> >();

            this.WriteHeader();


            this.AddResult("Array.ConvertAll todecimal", i => new List <decimal>(Array.ConvertAll(intArray, Convert.ToDecimal)));
            this.AddResult("Array.ConvertAll changetype", i => new List <decimal>(Array.ConvertAll(intArray, v => (decimal)Convert.ChangeType(v, toElementType))));
            this.AddResult("EmitMapper", i => emitMapper.Map(intArray));
            this.AddResult("SafeMapper", i => fastConverter(intArray));
            this.AddResult("FastMapper", i => TypeAdapter.Adapt(intArray, fromType, toType));
            this.AddResult("AutoMapper", i => Mapper.Map(intArray, fromType, toType));
        }
        public override void Execute()
        {
            var rand     = new Random();
            var fromType = typeof(List <int>);
            var toType   = typeof(decimal[]);

            var intList = new List <int>(50);

            for (int i = 0; i < 50; i++)
            {
                intList.Add(rand.Next());
            }

            var toElementType = typeof(decimal);

            var fastConverter = SafeMap.GetConverter <List <int>, decimal[]>();
            var emitMapper    = ObjectMapperManager.DefaultInstance.GetMapper <List <int>, decimal[]>();

            this.WriteHeader();


            this.AddResult("Array.ConvertAll todecimal", i => Array.ConvertAll(intList.ToArray(), Convert.ToDecimal));
            this.AddResult("Array.ConvertAll changetype", i => Array.ConvertAll(intList.ToArray(), v => (decimal)Convert.ChangeType(v, toElementType)));
            this.AddResult("EmitMapper", i => emitMapper.Map(intList));
            this.AddResult("SafeMapper", i => fastConverter(intList));
            this.AddResult("FastMapper", i => TypeAdapter.Adapt(intList, fromType, toType));
            this.AddResult("AutoMapper", i => Mapper.Map(intList, fromType, toType));
        }
示例#3
0
        public void GetConverter_Generic_StringToInt()
        {
            var converter = SafeMap.GetConverter <string, int>();
            var result    = converter("10");

            Assert.AreEqual(10, result);
        }
示例#4
0
        public void GetConverter_NonGenericWithFormat_StringToInt()
        {
            var converter = SafeMap.GetConverter(typeof(string), typeof(int), CultureInfo.CurrentCulture);
            var result    = converter("10");

            Assert.AreEqual(10, result);
        }
示例#5
0
        public void GetConverter_NonGeneric_StringToInt()
        {
            var converter = SafeMap.GetConverter(typeof(string), typeof(int));
            var result    = converter("10");

            Assert.AreEqual(10, result);
        }
示例#6
0
        public void GetConverter_GenericWithFormat_StringToInt()
        {
            var converter = SafeMap.GetConverter <string, int>(CultureInfo.CurrentCulture);
            var result    = converter("10");

            Assert.AreEqual(10, result);
        }
示例#7
0
        private double ComparePerformance <TFrom, TTo>(Action compareAction, TFrom input, int iterations = 100000)
        {
            var safeMapConverter = SafeMap.GetConverter <TFrom, TTo>();

            var compareTime    = Profiler.Profile(compareAction, iterations, 100);
            var safeMapperTime = Profiler.Profile(() => safeMapConverter(input), iterations, 100);

            return((safeMapperTime - compareTime) / compareTime);
        }
        public override void Execute()
        {
            var rand     = new Random();
            var fromType = typeof(int[]);
            var toType   = typeof(decimal[]);

            var intArray = new int[50];

            for (int i = 0; i < 50; i++)
            {
                intArray[i] = rand.Next();
            }

            var toElementType = typeof(decimal);

            this.WriteHeader();

            var genericConvertType    = typeof(Converter <,>).MakeGenericType(typeof(int), typeof(decimal));
            var fasterflectConvertAll = typeof(Array).DelegateForCallMethod(
                new Type[] { typeof(int), typeof(decimal) },
                "ConvertAll",
                new Type[] { fromType, genericConvertType });


            var fastConverter = SafeMap.GetConverter <int[], decimal[]>();
            var emitMapper    = ObjectMapperManager.DefaultInstance.GetMapper <int[], decimal[]>();



            this.AddResult("Array.ConvertAll todecimal", i => Array.ConvertAll(intArray, Convert.ToDecimal));
            this.AddResult("Array.ConvertAll changetype", i => Array.ConvertAll(intArray, v => Convert.ChangeType(v, toElementType)));
            this.AddResult("EmitMapper", i => emitMapper.Map(intArray));
            this.AddResult("SafeMapper", i => fastConverter(intArray));
            this.AddResult("FastMapper", i => TypeAdapter.Adapt(intArray, fromType, toType));
            //this.AddResult("AutoMapper", i => Mapper.Map(intArray, fromType, toType));
            this.AddResult("Manual forloop", i => this.ConvertArrayManual(intArray, toElementType, v => Convert.ToDecimal(v)));
            this.AddResult("Manual forloop rev", i => this.ConvertArrayManualReverse(intArray, toElementType, v => Convert.ToDecimal(v)));
            this.AddResult("Manual foreachloop", i => this.ConvertArrayManualForEach(intArray, toElementType, v => Convert.ToDecimal(v)));
        }
示例#9
0
        private void ProfileConvert <TSource, TDestination>(TSource[] input, CultureInfo formatProvider, Action <int> compareFunc)
        {
            var safeMapper      = SafeMap.GetConverter <TSource, TDestination>();
            var sourceType      = typeof(TSource);
            var destinationType = typeof(TDestination);

            var emitMapper = ObjectMapperManager.DefaultInstance.GetMapper <TSource, TDestination>();

            //var fFastMapper = fFastMap.MapperFor<TSource, TDestination>();

            if (typeof(TDestination) != typeof(string))
            {
                if (typeof(TDestination) == typeof(DateTime) && typeof(TSource) == typeof(string))
                {
                    AutoMapper.Mapper.CreateMap(typeof(TSource), typeof(TDestination)).ConvertUsing(typeof(AutoMapperDateTimeTypeConverter));
                }
                else
                {
                    AutoMapper.Mapper.CreateMap <TSource, TDestination>();
                }
            }

            AutoMapper.Mapper.CreateMap <Address, AddressDto>();
            AutoMapper.Mapper.CreateMap <AddressDto, Address>();
            AutoMapper.Mapper.CreateMap <BenchSource.Int1, BenchDestination.Int1>();
            AutoMapper.Mapper.CreateMap <BenchSource.Int2, BenchDestination.Int2>();

            this.WriteHeader(string.Format("Profiling convert from {0} to {1}, {2} iterations", typeof(TSource).Name, typeof(TDestination).Name, input.Length));

            if (compareFunc != null)
            {
                this.AddResult("Native", compareFunc);
            }

            this.AddResult("SafeMapper", i => safeMapper(input[i]));

            this.AddResult("EmitMapper", i => emitMapper.Map(input[i]));

            this.AddResult("FastMapper", i => TypeAdapter.Adapt(input[i], sourceType, destinationType));

            //this.AddResult("fFastMapper", i => fFastMapper.Map(input[i]));

            /*
             * this.AddResult("ValueInjecter",
             *  i =>
             *      {
             *          var result = new TDestination();
             *          result.InjectFrom(input[i]);
             *      });*/

            /*
             * this.AddResult(
             *      "ServiceStack",
             *      i => AutoMappingUtils.ConvertTo<TDestination>(input[i]));
             *
             * this.AddResult(
             *      "SimpleTypeConverter",
             *      i => SimpleTypeConverter.ConvertTo(input[i], typeof(TDestination), formatProvider));
             *
             *
             * this.AddResult(
             *      "UniversalTypeConverter",
             *      i => UniversalTypeConverter.Convert(input[i], typeof(TDestination), formatProvider));*/


            this.AddResult("AutoMapper", i => AutoMapper.Mapper.Map <TSource, TDestination>(input[i]));
        }
 public SafeMapperBenchmark()
 {
     _simpleConverter = SafeMap.GetConverter <SimplePoco, SimplePocoDTO>();
     _nestedConverter = SafeMap.GetConverter <NestedPoco, NestedPocoDTO>();
 }
        private void ProfileConvert <TSource, TDestination>(TSource[] input, CultureInfo formatProvider, Action <int> compareFunc) where TDestination : new()
        {
            var sourceType      = typeof(TSource);
            var destinationType = typeof(TDestination);
            var fastConverter   = SafeMap.GetConverter <TSource, TDestination>();

            var emitMapper = ObjectMapperManager.DefaultInstance.GetMapper <TSource, TDestination>();

            Action <Action <int>, int> trycatchDelegate = (Action <int> action, int i) =>
            {
                try
                {
                    action(i);
                }
                catch (Exception)
                {
                }
            };

            if (typeof(TDestination) != typeof(string))
            {
                if (typeof(TDestination) == typeof(DateTime) && typeof(TSource) == typeof(string))
                {
                    AutoMapper.Mapper.CreateMap(typeof(TSource), typeof(TDestination)).ConvertUsing(typeof(AutoMapperDateTimeTypeConverter));
                }
                else
                {
                    AutoMapper.Mapper.CreateMap <TSource, TDestination>();
                }
            }

            AutoMapper.Mapper.CreateMap <Address, AddressDto>();

            this.WriteHeader(string.Format("Profiling convert from {0} to {1}, {2} iterations", typeof(TSource).Name, typeof(TDestination).Name, input.Length));

            if (compareFunc != null)
            {
                this.AddResult("Native", k => trycatchDelegate(compareFunc, k));
            }

            this.AddResult(
                "SafeMapper",
                k => trycatchDelegate(i => fastConverter(input[i]), k));

            this.AddResult("EmitMapper", k => trycatchDelegate(i => emitMapper.Map(input[i]), k));

            this.AddResult("FastMapper", k => trycatchDelegate(i => TypeAdapter.Adapt(input[i], sourceType, destinationType), k));


            this.AddResult(
                "ValueInjecter",
                k => trycatchDelegate(
                    i =>
            {
                var result = new TDestination();
                result.InjectFrom(input[i]);
            },
                    k));

            /*this.AddResult(
             *      "SimpleTypeConverter",
             *      i => SimpleTypeConverter.SimpleTypeConverter.ConvertTo(input[i], destinationType, formatProvider));
             */
            /*
             * this.AddResult(
             *      "UniversalTypeConverter",
             *      i => UniversalTypeConverter.Convert(input[i], typeof(TDestination), formatProvider));
             */

            this.AddResult("AutoMapper", k => trycatchDelegate(i => AutoMapper.Mapper.Map <TSource, TDestination>(input[i]), k));
        }