下面是官方的性能测试 Demo,感性的也可以去 Github 上下载。

贴出代码目的是如果后期直接从自己的博客中在线看。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using AutoMapper;
  5. using Benchmark.Classes;
  6. using Mapster;
  7. using FastExpressionCompiler;
  8. using System.Linq.Expressions;
  9.  
  10. namespace Benchmark
  11. {
  12. class Program
  13. {
  14. static double sAM;
  15. static double sMP;
  16. static double sMF;
  17. static double sEM;
  18.  
  19. static Func<LambdaExpression, Delegate> defaultCompiler = TypeAdapterConfig.GlobalSettings.Compiler;
  20.  
  21. static void Main(string[] args)
  22. {
  23. try
  24. {
  25. Mapper.Initialize(cfg =>
  26. {
  27. cfg.CreateMap<Foo, Foo>();
  28. cfg.CreateMap<Address, Address>();
  29. cfg.CreateMap<Address, AddressDTO>();
  30. cfg.CreateMap<Customer, CustomerDTO>();
  31. });
  32. TestSimpleTypes();
  33. TestComplexTypes();
  34.  
  35. Console.WriteLine();
  36. Console.WriteLine($"======================================================================================");
  37. Console.WriteLine($"| Tests | Time (ms) | Slower than Mapster | Slower than Mapster + FEC |");
  38. Console.WriteLine($"======================================================================================");
  39. Console.WriteLine($"| Mapster v3.1.8 | { sMP,9} | { sMP / sMP,18:N2}X | { sMP / sMF,24:N2}X |");
  40. Console.WriteLine($"| Mapster v3.1.8 + FEC | { sMF,9} | { sMF / sMP,18:N2}X | { sMF / sMF,24:N2}X |");
  41. Console.WriteLine($"| Automapper v7.0.1 | { sAM,9} | { sAM / sMP,18:N2}X | { sAM / sMF,24:N2}X |");
  42. Console.WriteLine($"| ExpressMapper v1.9.1 | { sEM,9} | { sEM / sMP,18:N2}X | { sEM / sMF,24:N2}X |");
  43. Console.WriteLine($"======================================================================================");
  44. Console.WriteLine();
  45. }
  46.  
  47. finally
  48. {
  49. Console.WriteLine("Finish");
  50. Console.ReadLine();
  51. }
  52. }
  53.  
  54. static void TestSimpleTypes()
  55. {
  56. Console.WriteLine("Test 1 : Simple Types");
  57. Console.WriteLine("Competitors : Mapster, ExpressMapper, AutoMapper");
  58.  
  59. var foo = GetFoo();
  60.  
  61. TestSimple(foo, );
  62.  
  63. TestSimple(foo, );
  64.  
  65. TestSimple(foo, );
  66.  
  67. TestSimple(foo, );
  68.  
  69. //Console.WriteLine();
  70. //Console.WriteLine("Automapper to Mapster ratio: " + (AutomapperTime / MapsterTime).ToString("###.00") + " X slower");
  71. //Console.WriteLine("ExpressMapper to Mapster ratio: " + (ExpressMapperTime / MapsterTime).ToString("###.00") + " X slower");
  72. //Console.WriteLine();
  73.  
  74. }
  75.  
  76. static void TestComplexTypes()
  77. {
  78. Console.WriteLine();
  79. Console.WriteLine();
  80.  
  81. Console.WriteLine("Test 2 : Complex Types");
  82. Console.WriteLine("Competitors : Mapster, ExpressMapper, AutoMapper");
  83.  
  84. var customer = GetCustomer();
  85.  
  86. //TypeAdapterConfig.GlobalSettings.DestinationTransforms.Upsert<Guid>(x => x);
  87.  
  88. //ObjectMapperManager.DefaultInstance.GetMapper<Customer, CustomerDTO>().Map(customer);
  89.  
  90. Test(customer, );
  91.  
  92. Test(customer, );
  93.  
  94. Test(customer, );
  95.  
  96. Test(customer, );
  97.  
  98. Test(customer, );
  99.  
  100. //Console.WriteLine();
  101. //Console.WriteLine("Automapper to Mapster ratio: " + (AutomapperTime/MapsterTime).ToString("###.00") + " X slower");
  102. //Console.WriteLine("ExpressMapper to Mapster ratio: " + (ExpressMapperTime/MapsterTime).ToString("###.00") + " X slower");
  103. //Console.WriteLine();
  104. }
  105.  
  106. static void Test(Customer item, int iterations)
  107. {
  108. Console.WriteLine();
  109.  
  110. Console.WriteLine("Iterations : {0}", iterations);
  111.  
  112. //TestCustomerNative(item, iterations);
  113.  
  114. TypeAdapterConfig.GlobalSettings.Compiler = defaultCompiler; //switch compiler
  115. TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
  116. item.Adapt<Customer, CustomerDTO>(); //exercise
  117. TestMapsterAdapter<Customer, CustomerDTO>(item, iterations, ref sMP);
  118.  
  119. TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
  120. TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
  121. item.Adapt<Customer, CustomerDTO>(); //exercise
  122. TestMapsterAdapter<Customer, CustomerDTO>(item, iterations, ref sMF);
  123.  
  124. ExpressMapper.Mapper.Map<Customer, CustomerDTO>(item); //exercise
  125. TestExpressMapper<Customer, CustomerDTO>(item, iterations, ref sEM);
  126.  
  127. Mapper.Map<Customer, CustomerDTO>(item); //exercise
  128. TestAutoMapper<Customer, CustomerDTO>(item, iterations, ref sAM);
  129. }
  130.  
  131. static void TestSimple(Foo item, int iterations)
  132. {
  133.  
  134. Console.WriteLine();
  135.  
  136. Console.WriteLine("Iterations : {0}", iterations);
  137.  
  138. TypeAdapterConfig.GlobalSettings.Compiler = defaultCompiler; //switch compiler
  139. TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
  140. item.Adapt<Foo, Foo>(); //exercise
  141. TestMapsterAdapter<Foo, Foo>(item, iterations, ref sMP);
  142.  
  143. TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast(); //switch compiler
  144. TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
  145. item.Adapt<Foo, Foo>(); //exercise
  146. TestMapsterAdapter<Foo, Foo>(item, iterations, ref sMF);
  147.  
  148. //TestValueInjecter<Foo, Foo>(item, iterations);
  149.  
  150. ExpressMapper.Mapper.Map<Foo, Foo>(item); //exercise
  151. TestExpressMapper<Foo, Foo>(item, iterations, ref sEM);
  152.  
  153. Mapper.Map<Foo, Foo>(item); //exercise
  154. TestAutoMapper<Foo, Foo>(item, iterations, ref sAM);
  155. }
  156.  
  157. static void TestCustomerNative(Customer item, int iterations)
  158. {
  159. Console.WriteLine("Handwritten Mapper:\t" + Loop<Customer>(item, get =>
  160. {
  161. var dto = new CustomerDTO();
  162.  
  163. dto.Id = get.Id;
  164. dto.Name = get.Name;
  165. dto.AddressCity = get.Address.City;
  166.  
  167. dto.Address = new Address() { Id = get.Address.Id, Street = get.Address.Street, Country = get.Address.Country, City = get.Address.City };
  168.  
  169. dto.HomeAddress = new AddressDTO() { Id = get.HomeAddress.Id, Country = get.HomeAddress.Country, City = get.HomeAddress.City };
  170.  
  171. dto.Addresses = new AddressDTO[get.Addresses.Length];
  172. for (int i = ; i < get.Addresses.Length; i++)
  173. {
  174. dto.Addresses[i] = new AddressDTO() { Id = get.Addresses[i].Id, Country = get.Addresses[i].Country, City = get.Addresses[i].City };
  175. }
  176.  
  177. dto.WorkAddresses = new List<AddressDTO>();
  178. foreach (var workAddress in get.WorkAddresses)
  179. {
  180. dto.WorkAddresses.Add(new AddressDTO() { Id = workAddress.Id, Country = workAddress.Country, City = workAddress.City });
  181. }
  182.  
  183. }, iterations));
  184. }
  185.  
  186. static void TestMapsterAdapter<TSrc, TDest>(TSrc item, int iterations, ref double counter)
  187. where TSrc : class
  188. where TDest : class, new()
  189. {
  190. var time = Loop(item, get => get.Adapt<TSrc, TDest>(), iterations);
  191. Console.WriteLine("Mapster:\t\t" + time);
  192. counter += time;
  193. }
  194.  
  195. static void TestExpressMapper<TSrc, TDest>(TSrc item, int iterations, ref double counter)
  196. where TSrc : class
  197. where TDest : class, new()
  198. {
  199. var time = Loop(item, get => ExpressMapper.Mapper.Map<TSrc, TDest>(get), iterations);
  200. Console.WriteLine("ExpressMapper:\t\t" + time);
  201. counter += time;
  202. }
  203.  
  204. static void TestAutoMapper<TSrc, TDest>(TSrc item, int iterations, ref double counter)
  205. where TSrc : class
  206. where TDest : class, new()
  207. {
  208. //if (iterations > 50000)
  209. // Console.WriteLine("AutoMapper still working please wait...");
  210.  
  211. var time = Loop(item, get => Mapper.Map<TSrc, TDest>(get), iterations);
  212. Console.WriteLine("AutoMapper:\t\t" + time);
  213. counter += time;
  214. }
  215.  
  216. static long Loop<T>(T item, Action<T> action, int iterations = )
  217. {
  218. return Time(item, a =>
  219. {
  220. for (int i = ; i < iterations; i++)
  221. {
  222. action(a);
  223. }
  224. });
  225. }
  226.  
  227. static long Time<T>(T item, Action<T> action)
  228. {
  229. var sw = new Stopwatch();
  230. sw.Start();
  231. action(item);
  232. sw.Stop();
  233. return sw.ElapsedMilliseconds;
  234. }
  235.  
  236. #region Data
  237.  
  238. static Customer GetCustomer()
  239. {
  240. Customer c = new Customer()
  241. {
  242. Address = new Address() { City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad." },
  243. HomeAddress = new Address() { City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad." },
  244. Id = ,
  245. Name = "Eduardo Najera",
  246. Credit = 234.7m,
  247. WorkAddresses = new List<Address>()
  248. {
  249. new Address() {City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad."},
  250. new Address() {City = "izmir", Country = "turkey", Id = , Street = "konak"}
  251. },
  252. Addresses = new List<Address>()
  253. {
  254. new Address() {City = "istanbul", Country = "turkey", Id = , Street = "istiklal cad."},
  255. new Address() {City = "izmir", Country = "turkey", Id = , Street = "konak"}
  256. }.ToArray()
  257. };
  258.  
  259. return c;
  260. }
  261.  
  262. static Foo GetFoo()
  263. {
  264. var o = new Foo
  265. {
  266. Name = "foo",
  267. Int32 = ,
  268. Int64 = ,
  269. NullInt = ,
  270. DateTime = DateTime.Now,
  271. Doublen = ,
  272. Foo1 = new Foo { Name = "foo one" },
  273. Foos = new List<Foo>
  274. {
  275. new Foo {Name = "j1", Int64 = , NullInt = },
  276. new Foo {Name = "j2", Int32 = , NullInt = },
  277. new Foo {Name = "j3", Int32 = , NullInt = },
  278. },
  279. FooArr = new[]
  280. {
  281. new Foo {Name = "a1"},
  282. new Foo {Name = "a2"},
  283. new Foo {Name = "a3"},
  284. },
  285. IntArr = new[] { , , , , },
  286. Ints = new[] { , , },
  287. };
  288.  
  289. return o;
  290. }
  291.  
  292. #endregion
  293.  
  294. }
  295. }

谢谢浏览!

一个比 AutoMapper 更快的模型映射的组件 Mapster的更多相关文章

  1. 一个比NPM更快更安全可靠的JavaScript包管理工具——Yarn

    yarn安装: npm intall -g yarn 查看安装是否成功: yarn -v yarn常用的命令以及和npm的对照如下: 更详细的请查看官方文档

  2. linux 下程序员专用搜索源码用来替代grep的软件ack(后来发现一个更快的: ag), 且有vim插件的

    发现一个比ack更快更好用的:  https://github.com/ggreer/the_silver_searcher   , 使用时命令为ag,它是基于ack的代码二次开发的,所有使用方法基本 ...

  3. 比NGINX更快:nginx-1.15.5 vs mongols-1.2.3

    nginx是多进程web服务器的优秀代表. 本文要用mongols-1.2.3实现一个比nginx更快的多进程的web服务器. mongols是C++ 服务器基础设施库, 它的主要特性如下: tcp ...

  4. 修复bug有哪些更快的技术?做好这6点就够了

    你有没有想过为什么有时修复错误似乎比它应该花费更长的时间?当你终于找到问题时,事实证明你所需要的只是一个小小的改变.然而,花了很多时间才能找到正在发生的事情.这种情况比我想象的更频繁. 另一方面,当您 ...

  5. MindSpore模型精度调优实战:如何更快定位精度问题

    摘要:为大家梳理了针对常见精度问题的调试调优指南,将以"MindSpore模型精度调优实战"系列文章的形式分享出来,帮助大家轻松定位精度问题,快速优化模型精度. 本文分享自华为云社 ...

  6. EdgeFormer: 向视觉 Transformer 学习,构建一个比 MobileViT 更好更快的卷积网络

    ​  前言 本文主要探究了轻量模型的设计.通过使用 Vision Transformer 的优势来改进卷积网络,从而获得更好的性能. 欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结.最新技术跟 ...

  7. android开发者您还在为模拟器犯愁吗?神级android模拟器---Genymotion一个更快、接近完美的模拟器……

    摘要:Android系统非常特别,App须要进行模拟化測试.即使这样仍然有解决的办法---虚拟化技术. 之前的模拟器比方eclipse自带的是非常慢的一种,并且模拟器的版本号并非最新的.开机.能够说差 ...

  8. LSM树——LSM 将B+树等结构昂贵的随机IO变的更快,而代价就是读操作要处理大量的索引文件(sstable)而不是一个,另外还是一些IO被合并操作消耗。

    Basic Compaction 为了保持LSM的读操作相对较快,维护并减少sstable文件的个数是很重要的,所以让我们更深入的看一下合并操作.这个过程有一点儿像一般垃圾回收算法. 当一定数量的ss ...

  9. ZeroMQ一个更小、更快、更简单的智能传输层协议

    这个githube上的教程是非常好的,是个中文翻译,大家直接学这个就行 https://github.com/anjuke/zguide-cn/tree/master/bin 原文地址: https: ...

随机推荐

  1. Mac 应用程序不能打开解决方法

    Mac 应用程序不能打开解决方法 关键是 文件原本是可执行文件,由于权限丢失,才变成了类型不明的文件,导致软件无法打开. 参考: https://www.macbl.com/article/tips/ ...

  2. ASP.NET Core 如何用 Cookie 来做身份验证

    前言 本示例完全是基于 ASP.NET Core 3.0.本文核心是要理解 Claim, ClaimsIdentity, ClaimsPrincipal,读者如果有疑问,可以参考文章 理解ASP.NE ...

  3. GO 键盘输入和打印输出

    键盘输入和打印输出 一.打印输出 1.1 fmt包 fmt包实现了类似C语言printf和scanf的格式化I/O.格式化verb('verb')源自C语言但更简单. 详见官网fmt的API:http ...

  4. 你以为你真的了解final吗?

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  5. 异常处理类-Throwable源码详解

    package java.lang; import java.io.*; /** * * Throwable是所有Error和Exceptiong的父类 * 注意它有四个构造函数: * Throwab ...

  6. golang的析构函数

    runtime.SetFinalizer 使用这个函数可以给一个对象设置一个析构函数,如果这个对象没有引用了,那么就会调用这个析构函数,然后会把这个对象给释放掉

  7. [日常] Redis中set集合的使用思考

    公司部门同事有个需求,就是需要把当前另一个部门a中存储的数据全部导出来,自己当前业务b的数据全部导出来,两个要取一下差集,把a中存在,b中不存在的记下来,要去调用某接口把对应的文件删除.这个我感觉可以 ...

  8. 渗透测试学习 十三、 SQLmap使用详解

    SQLmap介绍 sqlmap是一个由python语言编写的开源的渗透测试工具,它主要是检测SQL注入漏洞,是一款功能强大的SQL漏洞检测利用工具. 他可以检测的数据库有:access.msSQL.M ...

  9. Shel脚本-初步入门之《06》

    Shel脚本-初步入门-06 Shell 脚本的建立和执行 6.Shell 脚本的建立和执行 6.1 Shell脚本的建立 在 Linux 系统中,Shell 脚本(bash Shell 程序)通常是 ...

  10. 爬虫---lxml爬取博客文章

    上一篇大概写了下lxml的用法,今天我们通过案例来实践,爬取我的博客博客并保存在本地 爬取博客园博客 爬取思路: 1.首先找到需要爬取的博客园地址 2.解析博客园地址 # coding:utf-8 i ...