Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 675|回复: 0

解决ASP.NET Core MVC调试慢的问题

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-5-16 03:19:56 | 显示全部楼层 |阅读模式

    最近在做的一个项目中,发现网页端同时发起了4个AJAX请求需要数秒才获取到响应,太慢了,当我把请求数降低为1个的时候,速度看起来就比较正常,增加到2个同时的请求后,速度就有些慢了,3个的话就明显慢了,4个更慢,如下图:

    而这几个请求获取的数据量都是非常少的,且服务器端也没什么重量级的操作,这太奇怪了。

    我到处寻找解决方案,未果,仔细查看output窗口中日志,也没发现警告或者错误之类的提示。但后来我仔细看了output日志之后,发现了一个问题,那就是output窗口日志输出太多,且输出速度较慢,下面就是日志片段:

    Njt.Crm> 10:05:50.415 [Debug][Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets]  Connection id "0HLEVO0B2MOF4" received FIN. 
    Njt.Crm> 10:05:50.415 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOF8" started. 
    Njt.Crm> 10:05:50.415 [Debug][Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets]  Connection id "0HLEVO0B2MOF6" received FIN. 
    Njt.Crm> 10:05:50.415 [Debug][Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets]  Connection id "0HLEVO0B2MOF5" received FIN. 
    Njt.Crm> 10:05:50.455 [Debug][Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets]  Connection id "0HLEVO0B2MOF7" received FIN. 
    Njt.Crm> 10:05:50.468 [Info][Microsoft.AspNetCore.Hosting.Internal.WebHost]  Request starting HTTP/1.1 GET http://localhost:5004/Mf/MfOrgs/3?MfId=3&PageNo=1&PageSize=40   
    Njt.Crm> 10:05:50.492 [Trace][Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware]  All hosts are allowed. 
    Njt.Crm> 10:05:50.513 [Debug][Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware]  The request path /Mf/MfOrgs/3 does not match a supported file type 
    Njt.Crm> 10:05:50.513 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOF9" started. 
    Njt.Crm> 10:05:50.555 [Trace][Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector]  Performing protect operation to key {d729c8cb-a753-4343-ac75-2dddd71308a6} with purposes ('D:\work2\Njt.Crm2\Njt.Crm', 'SessionMiddleware'). 
    Njt.Crm> 10:05:50.525 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOF5" disconnecting. 
    Njt.Crm> 10:05:50.525 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOF7" disconnecting. 
    Njt.Crm> 10:05:50.525 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOF6" disconnecting. 
    Njt.Crm> 10:05:50.569 [Debug][Microsoft.AspNetCore.Routing.RouteBase]  Request successfully matched the route with name 'default' and template '{controller=Home}/{action=Index}/{id?}'. 
    Njt.Crm> 10:05:50.540 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOF4" disconnecting. 
    Njt.Crm> 10:05:50.569 [Info][Microsoft.AspNetCore.Hosting.Internal.WebHost]  Request starting HTTP/1.1 GET http://localhost:5004/Mf/Contact/3?MfId=3&PageNo=1&PageSize=5   
    Njt.Crm> 10:05:50.569 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOFA" started. 
    Njt.Crm> 10:05:50.569 [Info][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Route matched with {action = "MfOrgs", controller = "Mf"}. Executing action Njt.Crm.Controllers.MfController.MfOrgs (Njt.Crm) 
    Njt.Crm> 10:05:50.569 [Debug][Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets]  Connection id "0HLEVO0B2MOF5" sending FIN. 
    Njt.Crm> 10:05:50.582 [Trace][Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware]  All hosts are allowed. 
    Njt.Crm> 10:05:50.569 [Debug][Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets]  Connection id "0HLEVO0B2MOF7" sending FIN. 
    Njt.Crm> 10:05:50.569 [Debug][Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets]  Connection id "0HLEVO0B2MOF6" sending FIN. 
    Njt.Crm> 10:05:50.582 [Debug][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Execution plan of authorization filters (in the following order): Njt.MvcBaseLib.Filters.UnifiedAuthFilter (Order: 0), Njt.MvcBaseLib.Filters.MaintainFilter (Order: 0), Njt.MvcBaseLib.Filters.AppNotOpenedDenyFilter (Order: 0) 
    Njt.Crm> 10:05:50.582 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOFB" started. 
    Njt.Crm> 10:05:50.582 [Debug][Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware]  The request path /Mf/Contact/3 does not match a supported file type 
    Njt.Crm> 10:05:50.615 [Info][Microsoft.AspNetCore.Hosting.Internal.WebHost]  Request starting HTTP/1.1 GET http://localhost:5004/Mf/Contract/3?MfId=3&PageNo=1&PageSize=40   
    Njt.Crm> 10:05:50.615 [Debug][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Execution plan of resource filters (in the following order): Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter 
    Njt.Crm> 10:05:50.615 [Info][Microsoft.AspNetCore.Hosting.Internal.WebHost]  Request starting HTTP/1.1 GET http://localhost:5004/Mf/Milestone/3?MfId=3&PageNo=1&PageSize=40   
    Njt.Crm> 10:05:50.615 [Trace][Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector]  Performing protect operation to key {d729c8cb-a753-4343-ac75-2dddd71308a6} with purposes ('D:\work2\Njt.Crm2\Njt.Crm', 'SessionMiddleware'). 
    Njt.Crm> 10:05:50.615 [Trace][Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware]  All hosts are allowed. 
    
    //……
    
    Njt.Crm> 10:05:53.160 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: Before executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.184 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.MvcBaseLib.Filters.AppNotOpenedDenyFilter. 
    Njt.Crm> 10:05:53.205 [Info][Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonResultExecutor]  Executing JsonResult, writing value of type 'Njt.Crm.SvcModels.Mf.MilestoneListOfMfIdResp'. 
    Njt.Crm> 10:05:53.228 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.249 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.MvcBaseLib.Filters.MaintainFilter. 
    Njt.Crm> 10:05:53.249 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  After executing action result Microsoft.AspNetCore.Mvc.JsonResult. 
    Njt.Crm> 10:05:53.273 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Resource Filter: Before executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.273 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.MvcBaseLib.Filters.UnifiedAuthFilter. 
    Njt.Crm> 10:05:53.273 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.MvcBaseLib.Filters.AppNotOpenedDenyFilter. 
    Njt.Crm> 10:05:53.301 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Resource Filter: After executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.304 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.Crm.Filters.RightFilterAttribute+RightFilterImplAttribute. 
    Njt.Crm> 10:05:53.304 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.MvcBaseLib.Filters.MaintainFilter. 
    Njt.Crm> 10:05:53.304 [Info][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Executed action Njt.Crm.Controllers.MfController.Contract (Njt.Crm) in 2468.5499ms 
    Njt.Crm> 10:05:53.333 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.MvcBaseLib.Filters.UnifiedAuthFilter. 
    Njt.Crm> 10:05:53.304 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: Before executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.333 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOFA" completed keep alive response. 
    Njt.Crm> 10:05:53.333 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecutionAsync on filter Njt.Crm.Filters.RightFilterAttribute+RightFilterImplAttribute. 
    Njt.Crm> 10:05:53.333 [Info][Microsoft.AspNetCore.Hosting.Internal.WebHost]  Request finished in 2726.3829ms 200 application/json; charset=utf-8 
    Njt.Crm> 10:05:53.333 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.333 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: Before executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.368 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Resource Filter: Before executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.368 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Result Filter: After executing OnResultExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.368 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Resource Filter: After executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.368 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Resource Filter: Before executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.379 [Info][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Executed action Njt.Crm.Controllers.MfController.Contact (Njt.Crm) in 2616.9911ms 
    Njt.Crm> 10:05:53.402 [Trace][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Resource Filter: After executing OnResourceExecuted on filter Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter. 
    Njt.Crm> 10:05:53.402 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOF9" completed keep alive response. 
    Njt.Crm> 10:05:53.402 [Info][Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker]  Executed action Njt.Crm.Controllers.MfController.Milestone (Njt.Crm) in 2507.7129ms 
    Njt.Crm> 10:05:53.402 [Info][Microsoft.AspNetCore.Hosting.Internal.WebHost]  Request finished in 2836.4425ms 200 application/json; charset=utf-8 
    Njt.Crm> 10:05:53.411 [Debug][Microsoft.AspNetCore.Server.Kestrel]  Connection id "0HLEVO0B2MOFB" completed keep alive response. 
    Njt.Crm> 10:05:53.436 [Info][Microsoft.AspNetCore.Hosting.Internal.WebHost]  Request finished in 2810.8861ms 200 application/json; charset=utf-8 

    这4个请求一共导致了300多行log,我只截取了前后30行,看吧,全都是Trace/Debug/Info,这些信息对我们来说基本没什么用,考虑到我用的是NLog日志输出,那只需要修改一下配置,让NLog不输出这些多余的东西不就好了吗?于是我修改了NLog配置:

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          autoReload="true"
          throwExceptions="false"
          internalLogLevel="Off">
      <variable name="theLayout" value="${date:format=HH\:mm\:ss.fff} [${level}][${logger}] ${callsite:className=False:fileName=True:methodName=False} ${message} ${onexception:${newline}}${exception:format=Message,ShortType,StackTrace:innerFormat=Message,ShortType,StackTrace:separator=\r\n:innerExceptionSeparator=\r\n---Inner---\r\n:maxInnerExceptionLevel=5}"/>
      <targets>
        <target name="asyncFile" xsi:type="AsyncWrapper">
          <target name="logfile" xsi:type="File" fileName="${basedir}/log/${shortdate}.log" layout="${theLayout}" encoding="UTF-8" />
        </target>
        <target name="debugger" xsi:type="Debugger" layout="${theLayout}" />
        <target name="console" xsi:type="Console" layout="${theLayout}" />
        <target name="void" xsi:type="Null" formatMessage="false" />
      </targets>
      <rules>
        <logger name="Microsoft.AspNetCore.*" minlevel="Trace" maxlevel="Info" writeTo="void" final="true" />
        <logger name="*" minlevel="Debug" writeTo="asyncFile" />
        <logger name="*" minlevel="Trace" writeTo="debugger"/>
        <logger name="*" minlevel="Trace" writeTo="console"/>
      </rules>
    </nlog>

    注意看这行:

    <logger name="Microsoft.AspNetCore.*" minlevel="Trace" maxlevel="Info" writeTo="void" final="true" />

    意思就是把Microsoft.AspNetCore的日志都写到void去,并且就此结束,不再写到别的日志处理器去(final="true"),void的定义见上面,其实就是什么都不写。

    这么一来,居然有了令人惊喜的效果:

    速度有了质的飞跃,且其它页面打开似乎也快了一点。而我做的只是把多余的日志拿掉了。

    回到开始,为什么4个同时发生的AJAX请求会显著让响应变慢呢?我猜是这样,4个请求同时到达服务器,每个请求都产生很多log,4个请求就产生了4倍的log,且我们的程序在这些Log输出完毕之前,不会进入下一步操作,所以就相当于同时阻挡了这4个请求。

    我还发现这么一来我们整个ASP.NET Core程序的调试都略快于以前了,别人没碰到这个问题的原因是他们可能没像我这样使用NLog,他们也许使用了别的日志框架之类的。

    另外,你也许觉得100多毫秒的响应速度还是太慢了,确实,这跟我的工作环境有关系,我们的数据库服务器并不是放在本地的,我们是通过VPN连接到了远程的机房去访问数据库,所以延迟比较高,如果在本地的话,基本都是几十毫秒的事情。

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-12-23 06:43 , Processed in 0.132364 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表