加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

实体框架 – ASP.NET Web Api 2/EF6首次调用初始化性能

发布时间:2020-12-15 19:05:10 所属栏目:asp.Net 来源:网络整理
导读:第一次调用我们的API总是非常慢。例如,下面的示例演示了CPU使用情况和第一个调用完成所需的时间: 第一次调用可能需要长达30秒,并吃几乎100%的CPU。呼叫2和3需要200ms(因为他们应该)。在回收应用程序池之后,它将对第一次调用执行相同的操作。 我读了一点
第一次调用我们的API总是非常慢。例如,下面的示例演示了CPU使用情况和第一个调用完成所需的时间:

第一次调用可能需要长达30秒,并吃几乎100%的CPU。呼叫2和3需要200ms(因为他们应该)。在回收应用程序池之后,它将对第一次调用执行相同的操作。

我读了一点关于IIS“热身”,并做以下,但没有改变:

IIS 8 Application Initialization安装:

我在IIS中有以下设置:

>将启动模式设置为AlwaysRunning:

>将“回收超时”设置为0:

>将空闲超时设置为0:

>在站点上将预加载启用设置为true:

我实际上是在RoleEntryPoint.OnStart()中的代码中设置这些。

using (var serverManager = new ServerManager())
{
    serverManager.ApplicationPoolDefaults.ProcessModel.IdleTimeout = TimeSpan.Zero;

    foreach (var application in serverManager.Sites.SelectMany(x => x.Applications))
    {
        application["preloadEnabled"] = true;

    }

    foreach (var applicationPool in serverManager.ApplicationPools)
    {
        applicationPool.AutoStart = true;
        applicationPool["startMode"] = "AlwaysRunning";
        applicationPool.ProcessModel.IdleTimeout = TimeSpan.Zero;
        applicationPool.Recycling.PeriodicRestart.Time = TimeSpan.Zero;

    }

    serverManager.CommitChanges();
}

我几乎可以肯定实体框架可能是罪魁祸首:

>我们在EDMX模型“设计器”中从大约100个表生成模型。
>我们正在生成由EF Power Tools生成的预编译视图。
>以下初始化在Application_Start()中运行:

using (var context = new MyContext())
{
    context.Database.Initialize(false);
}

我在调试时没有这些“初始化”问题。

使用以下技术:

> .NET 4.5.1
> ASP.NET Web Api 2
>实体框架6.1.1
> IIS 8(Azure Web角色)
> Unity 3.5

任何人都可以向我提供任何其他想法或建议吗?

解决方法

不确定是否有人解决了这个问题,但我了解到在Entity Framework的初始启动时出现的一些性能问题。 Julie Lerman在她的关于Entity Framework的Pluralsight课程中讨论了这个问题,并在下面的摘录自微软的文章中提到:

One of the biggest drags on performance is the startup time involved with the first use of a context in an application process. You can do a lot to improve that startup time,though. Hopefully you’ve already learned these tricks from my own writing or other resources,such as the MSDN doc on performance considerations at bit.ly/3D6AiC.

A startup step that often hampers performance is the view gener-ation of mapping views,where EF creates the relevant SQL to query against each of the entity sets in the model. These views get leveraged as your app runs so that for certain queries,EF doesn’t have to work out the SQL on the fly. View generation happens whether you created your model with the EF Designer or with Code First. You can pre-generate these views and compile them into the application to save time.
07000

这里似乎她不只是谈论“初始负载”,而是上下文的实际第一次使用。我想快速搜索Julie Lerman和Entity Framework性能问题。我注意到类似的慢度,当对我的Web API的初始调用。第一次之后的每个调用都明显更快。我个人没有发现它太可怕,所以我忽略它(现在)。然而,我发现它很有趣,它不会发生在调试模式。对不起,如果你已经探索了这些选项,但我希望这有一点帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读