c#  映射对比测试(测试对象,测试案例,测试结果)

测试组件对象:

TinyMapper-EmitMapper-AutoMapper-NLiteMapper-Handwritten

对比测试案例:

:Models

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9    public  class Models
10     {
11         public class Person
12         {
13             public Guid Id { get; set; }
14             public String Name { get; set; }
15             public Int32 Age { get; set; }
16             public Address Address { get; set; }
17             public string Number { get; set; }
18         }
19         public class PersonDto
20         {
21             public Guid Id { get; set; }
22             public String UserName { get; set; }
23             public Int32 Age { get; set; }
24             public Address Address { get; set; }
25             public string Number { get; set; }
26         }
27 
28         public sealed class Address
29         {
30             public string Phone { get; set; }
31             public string Street { get; set; }
32             public string ZipCode { get; set; }
33         }
34     }
35 }

:CodeTimer

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Runtime.InteropServices;
 6 using System.Text;
 7 using System.Threading;
 8 using System.Threading.Tasks;
 9 
10 namespace ConsoleApplication1
11 {
12     public sealed class CodeTimer
13     {
14         public static void Initialize()
15         {
16             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
17             Thread.CurrentThread.Priority = ThreadPriority.Highest;
18             Time("", 1, () => { });
19         }
20 
21         public static void Time(string name, Action action)
22         {
23             Time(name, 1, action);
24         }
25 
26         public static void Time(string name, int iteration, Action action)
27         {
28             if (String.IsNullOrEmpty(name)) return;
29 
30             // 1.
31             ConsoleColor currentForeColor = Console.ForegroundColor;
32             Console.ForegroundColor = ConsoleColor.Red;
33             Console.WriteLine(name);
34 
35             // 2.
36             GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
37             int[] gcCounts = new int[GC.MaxGeneration + 1];
38             for (int i = 0; i <= GC.MaxGeneration; i++)
39             {
40                 gcCounts[i] = GC.CollectionCount(i);
41             }
42 
43             // 3.
44             Stopwatch watch = new Stopwatch();
45             watch.Start();
46             long cycleCount = GetCycleCount();
47             for (int i = 0; i < iteration; i++) action();
48             long cpuCycles = GetCycleCount() - cycleCount;
49             watch.Stop();
50 
51             // 4.
52             Console.ForegroundColor = currentForeColor;
53             Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
54             Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));
55 
56             // 5.
57             for (int i = 0; i <= GC.MaxGeneration; i++)
58             {
59                 int count = GC.CollectionCount(i) - gcCounts[i];
60                 Console.WriteLine("\tGen " + i + ": \t\t" + count);
61             }
62 
63             Console.WriteLine();
64 
65         }
66 
67         private static long GetCycleCount()
68         {
69             return GetCurrentThreadTimes();
70         }
71 
72         [DllImport("kernel32.dll", SetLastError = true)]
73         static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
74            out long lpExitTime, out long lpKernelTime, out long lpUserTime);
75 
76         private static long GetCurrentThreadTimes()
77         {
78             long l;
79             long kernelTime, userTimer;
80             GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
81                out userTimer);
82             return kernelTime + userTimer;
83         }
84 
85         [DllImport("kernel32.dll")]
86         static extern IntPtr GetCurrentThread();
87     }
88 }

:Program

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using Nelibur.ObjectMapper;//安装TinyMapper
  7 using Nelibur.ObjectMapper.Bindings;
  8 using EmitMapper;//安装EmitMapper
  9 using AutoMapper;//安装AutoMapper
 10 using NLite;
 11 using EmitMapper.MappingConfiguration;
 12 using System.Reflection;//安装NLite
 13 
 14 namespace ConsoleApplication1
 15 {
 16     public class Program : Models
 17     {
 18         protected static List<Person> _person;
 19         protected static List<PersonDto> _personDto;
 20 
 21         static void Main(string[] args)
 22         {
 23             Base();
 24             //进行测试 测试次数1次
 25             CodeTimer.Time("--TinyMapper--测试", 1, () => Test1());
 26             CodeTimer.Time("--EmitMapper--测试", 1, () => Test2());
 27             CodeTimer.Time("--AutoMapper--测试", 1, () => Test3());
 28             CodeTimer.Time("--NLiteMapper--测试", 1, () => Test4());
 29             CodeTimer.Time("--Handwritten--测试", 1, () => Test5());
 30             ConsoleColor currentForeColor = Console.ForegroundColor;
 31             Console.ForegroundColor = ConsoleColor.Yellow;
 32             Console.WriteLine("任务完成");
 33             Console.ReadKey();
 34         }
 35 
 36         #region 初始化数据
 37         public static void Base()
 38         {
 39             _person = new List<Person>();
 40 
 41             for (int i = 0; i < 1000000; i++)
 42             {
 43                 Person _per = new Person()
 44                 {
 45                     Id = Guid.NewGuid(),
 46                     Name = "老黑",
 47                     Age = 22,
 48                     Address = new Address
 49                     {
 50                         Phone = "18772871387",
 51                         Street = "小红门",
 52                         ZipCode = "邮编未知",
 53                     },
 54                     Number = "123456789"
 55                 };
 56                 _person.Add(_per);
 57             }
 58         }
 59         #endregion
 60 
 61         #region TinyMapper映射
 62         static void Test1()
 63         {
 64             //TinyMapper.Bind<Person,PersonDto>();
 65             //_personDto = TinyMapper.Map<List<PersonDto>>(_person);
 66             TinyMapper.Bind<Person, PersonDto>(config =>
 67             {
 68                 config.Bind(x => x.Id, y => y.Id);
 69                 config.Bind(x => x.Name, y => y.UserName);
 70                 config.Bind(x => x.Age, y => y.Age);
 71                 config.Bind(x => x.Address.Phone, y => y.Address.Phone);
 72                 config.Bind(x => x.Address.Street, y => y.Address.Street);
 73                 config.Bind(x => x.Address.ZipCode, y => y.Address.ZipCode);
 74                 config.Bind(x => x.Number, y => y.Number);
 75             });
 76             _personDto = TinyMapper.Map<List<PersonDto>>(_person);
 77         }
 78         #endregion
 79 
 80         #region EmitMapper 映射
 81         static void Test2()
 82         {
 83             _personDto.Clear();
 84             //EmitMapper.ObjectsMapper<List<Person>, List<PersonDto>> mapper = ObjectMapperManager.DefaultInstance.GetMapper<List<Person>, List<PersonDto>>();
 85             //_personDto = mapper.Map(_person);
 86             EmitMapper.ObjectsMapper<List<Person>, List<PersonDto>> mapper;
 87             mapper = ObjectMapperManager.DefaultInstance.GetMapper<List<Person>, List<PersonDto>>(new DefaultMapConfig()
 88                .ConvertUsing<Person, PersonDto>(value => new PersonDto
 89                {
 90                    Id = value.Id,
 91                    UserName = value.Name,
 92                    Age = value.Age,
 93                    Address = new Address()
 94                    {
 95                        Phone = value.Address.Phone,
 96                        Street = value.Address.Street,
 97                        ZipCode = value.Address.ZipCode,
 98                    },
 99                    Number = value.Number
100                })
101                 );
102             _personDto = mapper.Map(_person);
103         }
104         #endregion
105 
106         #region AutoMapper 映射
107         static void Test3()
108         {
109             _personDto.Clear();
110             //AutoMapper.Mapper.CreateMap<Person,PersonDto>();
111             //_personDto = AutoMapper.Mapper.Map<List<PersonDto>>(_person);
112 
113 
114             AutoMapper.Mapper.CreateMap<Person, PersonDto>()
115                 .ConstructUsing(value => new PersonDto
116                 {
117                     Id = value.Id,
118                     UserName = value.Name,
119                     Age = value.Age,
120                     Address = new Address()
121                     {
122                         Phone = value.Address.Phone,
123                         Street = value.Address.Street,
124                         ZipCode = value.Address.ZipCode,
125                     },
126                     Number = value.Number
127                 });
128             _personDto = AutoMapper.Mapper.Map<List<PersonDto>>(_person);
129         }
130         #endregion
131 
132         #region NLiteMapper 映射
133         static void Test4()
134         {
135             _personDto.Clear();
136             //NLite.Mapping.IMapper<List<Person>, List<PersonDto>> mapper = NLite.Mapper.CreateMapper<List<Person>, List<PersonDto>>();
137             //_personDto = mapper.Map(_person);
138             NLite.Mapping.IMapper<List<Person>, List<PersonDto>> mapper;
139             mapper = NLite.Mapper.CreateMapper<List<Person>, List<PersonDto>>()
140             .ConvertUsing<Person, PersonDto>(v => new PersonDto
141             {
142                 Id = v.Id,
143                 UserName = v.Name,
144                 Age = v.Age,
145                 Address = new Address()
146                 {
147                     Phone = v.Address.Phone,
148                     Street = v.Address.Street,
149                     ZipCode = v.Address.ZipCode,
150                 },
151                 Number = v.Number
152             });
153             _personDto = mapper.Map(_person);
154         }
155         #endregion
158         #region Handwritten 手工映射
159         static void Test5()
160         {
161             _personDto.Clear();
162             _personDto = new List<PersonDto>();
163             PersonDto p = new PersonDto();
164             for (int i = 0; i < _person.Count; i++)
165             {
166                 p.Id = _person[i].Id;
167                 p.UserName = _person[i].Name;
168                 p.Age = _person[i].Age;
169                 p.Address = _person[i].Address;
170                 p.Number = _person[i].Number;
171                 _personDto.Add(p);
172             }
173         }
174         #endregion

对比测试结果截图: