ASP.NET Web Api 2/EF6 首次调用初始化性能 [英] ASP.NET Web Api 2 / EF6 first call initialization performance

查看:18
本文介绍了ASP.NET Web Api 2/EF6 首次调用初始化性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次调用我们的 API 总是非常慢.例如,下面演示了第一次调用完成所用的 CPU 使用率和时间:

第一次调用最多可能需要 30 秒,并且占用几乎 100% 的 CPU.调用 2 和 3 需要 200 毫秒(应该如此).回收应用程序池后,它会和第一次调用一样.

我已经阅读了一些关于 IIS预热"的内容并做了以下操作,但没有任何改变:

在这里,她似乎不仅仅是在谈论初始加载",而是在谈论上下文的实际首次使用.我想快速搜索一下 Julie Lerman 和实体框架性能问题.在对我的 Web API 进行初始调用时,我注意到类似的缓慢.第一个之后的每个呼叫都明显更快.我个人还没有发现它太糟糕了,所以我忽略了它(现在).但是,我确实觉得有趣的是它不会在调试模式下发生.抱歉,如果您已经探索过这些选项,但我希望这会有所帮助.

The first call to our API is always extremely slow. For example, below demonstrates the CPU usage and time it takes for the first call to complete:

The first call can take up to 30 seconds and eats almost 100% CPU. Call 2 and 3 take 200ms (as they should). After recycling the application pool, it will do the same thing with the first call.

I've read a bit about IIS "warm-up" and done the following, but nothing has changed:

IIS 8 Application Initialization is installed:

I have the following set in IIS:

  • Set Start Mode to AlwaysRunning:

  • Set the Recycling Timeout to 0:

  • Set the Idle Time-out to 0:

  • Set Preload Enabled to true on the site:

I am actually setting these in code in 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();
}

I am almost certain that Entity Framework could be the culprit:

  • We are generating models from around 100 tables in a EDMX model "designer".

  • We are generating precompiled views generated by EF Power Tools.

  • The following initialization is running in Application_Start():

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

I don't have these "initialization" problems when debugging.

The following tech is being used:

  • .NET 4.5.1
  • ASP.NET Web Api 2
  • Entity Framework 6.1.1
  • IIS 8 (Azure Web Role)
  • Unity 3.5

Can anyone provide me with any other ideas or suggestions?

解决方案

Not sure if anyone has addressed this yet, but I've learned about some performance issues arising on the initial startup of Entity Framework. Julie Lerman discussed this in her Pluralsight course on Entity Framework, and is also alluded to in the following article excerpt from Microsoft:

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. http://msdn.microsoft.com/en-us/magazine/dn532202.aspx

Here it seems she's not just talking about the 'initial load' but the actual first use of a context. I'd to a quick search for Julie Lerman and Entity Framework performance issues. I noticed similar slowness when making the initial calls to my Web API. Every call after the first was significantly faster. I personally haven't found it to be too awful, so I'm ignoring it (for now). However I do find it interesting that it doesn't occur in debug mode. Sorry if you've already explored these options, but I hope this helps a little.

这篇关于ASP.NET Web Api 2/EF6 首次调用初始化性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆