经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
.NET8.0 AOT 经验分享 - 专项测试各大 ORM 是否支持
来源:cnblogs  作者:FreeSql  时间:2023/11/20 8:56:29  对本文有异议

AOT 特点

发布和部署本机 AOT 应用具有以下优势:

  • 最大程度减少磁盘占用空间:使用本机 AOT 发布时,将生成一个可执行文件,其中仅包含支持程序所需的外部依赖项的代码。减小的可执行文件大小可能会导致:
    • 较小的容器映像,例如在容器化部署方案中。
    • 缩短了较小映像的部署时间。
  • 缩短启动时间:本机 AOT 应用程序可缩短启动时间,这意味着
    • 应用已准备好更快地为请求提供服务。
    • 改进了容器业务流程协调程序需要管理从应用的一个版本到另一个版本的转换的部署。
  • 减少内存需求:本机 AOT 应用可能会减少内存需求,具体由应用执行的工作决定。 减少内存消耗可以提高部署密度和可伸缩性。

模板应用在基准测试实验室中运行,来比较 AOT 已发布的应用、已修剪的运行时应用和未修剪的运行时应用的性能。下图显示了基准测试的结果:

上图显示本机 AOT 降低了应用大小、内存使用量和启动时间


本文内容

2023年11月15日,对.net的开发圈是一个重大的日子,.net 8.0正式版发布。

那一天,我发表了一篇关于 《.NET8.0 的升级和 AOT 经验的文章》,整体总结如下:

.NET8.0 AOT 已经到了可用的阶段,期待未来版本能改进以下问题:

  • 发布速度变快,目前20-30秒一次实在太慢
  • 编译前检查错误,而不是等发布后再报运行时错误
  • 加强调试,.pdb 100兆++ 为何调试还都是 c++ 有关内容,不能白瞎了这么大的调试文件啊
  • 尽快修复 Console.WriteLine(Enum.GetValues(typeof(TaskInterval))) 这个问题

如果一个全新的 AOT webapi 应用发布在国产系统上运行,算不算国产信创?

我是开源人:https://github.com/2881099

今天发表 AOT 经验的续篇和 ORM 有关,肯定会产生一些火药味,咱们能不能理智用技术的角度看完内容,提示:测试你不是贬低你,OK???

我在 github 上创建一个专门测试 AOT 发布的开源项目,有兴趣可以参与提交代码。

https://github.com/2881099/aot_test


FreeSql v3.2.805 + Sqlite

  1. 发布耗时 31.882
  2. orm_freesql.exe ( 16,927KB)
  3. orm_freesql.pdb (123,812KB)
  4. SQLite.Interop.dll ( 1,723KB)
  5. E:\github\aot_test\orm_freesql\bin\Release\net8.0\publish\win-x64>orm_freesql.exe
  6. FreeSql AOT】开始测试...
  7. Insert 1 80ms
  8. Select 1 5ms
  9. Update 1 86ms
  10. Select 1 0ms
  11. Delete 1 74ms
  12. FreeSql AOT】测试结束.

PS:没有对 AOT 的支持做专门改进,都是老代码。


SqlSugar v5.1.4.117 + Sqlite

程序代码设置要求:

  1. StaticConfig.EnableAot = true;
  1. 发布耗时 01:51.002 分钟
  2. orm_sqlsugar.exe ( 50,133KB)
  3. orm_sqlsugar.pdb (346,412KB)
  4. e_sqlite3 ( 1,597KB)
  5. Microsoft.Data.SqlClient.SNI.dll ( 499KB)
  6. E:\github\aot_test\orm_sqlsugar\bin\Release\net8.0\publish\win-x64>orm_sqlsugar.exe
  7. SqlSugar AOT】开始测试...
  8. Insert 1 214ms
  9. Select 1 29ms
  10. Update 1 119ms
  11. Select 1 0ms
  12. Delete 1 82ms
  13. SqlSugar AOT】测试结束.

EFCore v8.0 + Sqlite

  1. 发布耗时 50.749
  2. orm_efcore.exe ( 17,410KB)
  3. orm_efcore.pdb (168,788KB)
  4. e_sqlite3 ( 1,652KB)
  5. E:\github\aot_test\orm_efcore\bin\Release\net8.0\publish\win-x64>orm_efcore.exe
  6. EFCore AOT】开始测试...
  7. Unhandled Exception: System.InvalidOperationException: Model building is not supported when publishing with NativeAOT. Use a compiled model.
  8. at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean) + 0x148
  9. at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() + 0x1c
  10. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xc2
  11. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite, RuntimeResolverContext) + 0x35
  12. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) + 0xa4
  13. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite, RuntimeResolverContext) + 0x83
  14. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xc2
  15. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite, RuntimeResolverContext) + 0x35
  16. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) + 0xa4
  17. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite, RuntimeResolverContext) + 0x83
  18. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xc2
  19. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite, RuntimeResolverContext) + 0x35
  20. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) + 0xa4
  21. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite, RuntimeResolverContext) + 0x83
  22. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xc2
  23. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite, RuntimeResolverContext) + 0x35
  24. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) + 0xa4
  25. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite, RuntimeResolverContext) + 0x83
  26. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xc2
  27. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite, RuntimeResolverContext) + 0x35
  28. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) + 0xa4
  29. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite, RuntimeResolverContext) + 0x83
  30. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xc2
  31. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite, RuntimeResolverContext) + 0x35
  32. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) + 0xa4
  33. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite, ServiceProviderEngineScope) + 0x3d
  34. at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier, ServiceProviderEngineScope) + 0xa3
  35. at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type) + 0x42
  36. at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider, Type) + 0x50
  37. at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider) + 0x29
  38. at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() + 0x32
  39. at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() + 0x14f
  40. at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() + 0x22
  41. at Microsoft.EntityFrameworkCore.DbContext.EntryWithoutDetectChanges[TEntity](TEntity entity) + 0x18
  42. at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState) + 0x1d
  43. at Program.<Main>$(String[] args) + 0x1da
  44. at orm_efcore!<BaseAddress>+0x7c2ab0

Dapper

Dapper 已经出了新的 DapperAOT 版本,就不测试了,100%支持 AOT。

写到最后

希望多用技术交流,我写文章不是自夸 Free 系列开源项目有多流弊,实实在在的花了时间研究,测试通过后才发的文章,分享给大家多一个选择,让大家知晓已支持 AOT,仅此而已。

提示:测试你不是贬低你,OK???其他 ORM 可以按 https://github.com/2881099/aot_test 的方式提交测试代码,我确实没时间测试所有 ORM,不是为了比较而比较。

被抢 SEO 关键字行为已经不是一次两次了,确实没多大意思。

最后上一个统计表格吧,PS:支持 AOT 没什么了不起!

测试项目 发布耗时 发布后 .exe 体积 发布后 .pdb 体积 通过AOT
FreeSql v3.2.805 + Sqlite 31.882 秒 16,927KB 123,812KB 通过
SqlSugar v5.1.4.117 + Sqlite 111.002 秒 50,133KB 346,412KB 通过
EFCore v8.0 + Sqlite 50.749 17,410KB 168,788KB 未通过
DapperAOT 未测试(支持) 未测试(支持) 未测试(支持) 通过

如果大家对 AOT 有兴趣,我后面会持续分享自己的经验,PS mysql 测试也是没问题的,其他数据库如果有使用问题可以与我交流。

我是开源人:https://github.com/2881099

原文链接:https://www.cnblogs.com/FreeSql/p/17842391.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号