gpt4 book ai didi

entity-framework - ASP.NET Web Api 2/EF6 首次调用初始化性能

转载 作者:行者123 更新时间:2023-12-03 07:30:30 25 4
gpt4 key购买 nike

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

CPU usage

第一个电话最多可以占用 30 秒 并吃掉几乎 100% 的 CPU。调用 2 和 3 需要 200 毫秒(应该如此)。回收应用程序池后,它会和第一次调用做同样的事情。

我已经阅读了一些关于 IIS“热身”的内容并完成了以下操作,但没有任何改变:

IIS 8 Application Initialization已安装:

IIS 8 Application Initialization

我在 IIS 中有以下设置:

  • 套装启动模式 始终运行 :

  • AlwaysRunning
  • 设置 回收超时 到 0:

  • Recycling Timeout
  • 设置 空闲超时 到 0:

  • Idle Time-out
  • 套装启用预加载 在网站上为真:

  • Preload Enabled

    我实际上是在 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();
    }

    我几乎可以肯定 Entity Framework 可能是罪魁祸首:
  • 我们正在 EDMX 模型“设计器”中从大约 100 个表生成模型。
  • 我们正在生成 预编译 View 由 EF Power Tools 生成。
  • 以下初始化 正在运行 Application_Start() :
    using (var context = new MyContext())
    {
    context.Database.Initialize(false);
    }

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

    正在使用以下技术:
  • .NET 4.5.1
  • ASP.NET Web Api 2
  • Entity Framework 6.1.1
  • IIS 8(Azure Web 角色)
  • 统一 3.5

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

    最佳答案

    不确定是否有人已经解决了这个问题,但我已经了解了 Entity Framework 初始启动时出现的一些性能问题。 Julie Lerman 在她关于 Entity Framework 的 Pluralsight 类(class)中讨论了这一点,并且在 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


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

    关于entity-framework - ASP.NET Web Api 2/EF6 首次调用初始化性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26160950/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com