第一次调用可能需要长达30秒,并吃几乎100%的cpu。呼叫2和3需要200ms(因为他们应该)。在回收应用程序池之后,它将对第一次调用执行相同的操作。
我读了一点关于IIS“热身”,并做以下,但没有改变:
IIS 8 Application Initialization安装:
我在IIS中有以下设置:
>将启动模式设置为AlwaysRunning:
>将“回收超时”设置为0:
>将空闲超时设置为0:
我实际上是在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
任何人都可以向我提供任何其他想法或建议吗?
解决方法
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 generation 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的初始调用。第一次之后的每个调用都明显更快。我个人没有发现它太可怕,所以我忽略它(现在)。然而,我发现它很有趣,它不会发生在调试模式。对不起,如果你已经探索了这些选项,但我希望这有一点帮助。