ASP.NET Web Api 2 / EF6 first call initialization performance

asp.net-web-api2 entity-framework entity-framework-6 iis iis-8

Question

Our API's initial request is always quite slow. The CPU utilization and duration of the first call, for instance, are shown below as examples:

CPU usage

The first call can consume up to 12 zz and nearly all of the CPU. Calls 2 and 3 take 200 milliseconds (as they should). The first call will be handled in the same way as the application pool once it has been recycled.

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

It has been installed:

IIS 8 Application Initialization

IIS is configured with the following:

  • Transpose zzz-44 zzz to zzz-48 zzz:

AlwaysRunning

  • Zzz-62-Zzz to 0 setting

Recycling Timeout

  • Placing the Vacant Time-out at 0

Idle Time-out

  • On the website, set Preload is active to true.

Preload Enabled

Actually, I'm putting these in coding inRoleEntryPoint.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 may be to blame, I'm virtually positive about that.

  • We are using an EDMX model "designer" to generate models from about 100 tables.

  • ready-made views is being produced by EF Power Tools.

  • The initialization is now running inApplication_Start() :

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

When I'm debugging, I don't experience these "initialization" issues.

The following technology is employed:

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

Can someone give me any further recommendations or ideas?

1
28
10/9/2014 7:22:48 AM

Popular Answer

I don't know if this has been addressed yet, but I've heard that Entity Framework can have some performance problems when it first starts up. This was covered by Julie Lerman in her Pluralsight course on Entity Framework, and is also mentioned in the following snippet from a Microsoft article:

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 appears that she is referring to a context's actual first use rather than just the "first load." I quickly looked up Julie Lerman and performance difficulties with Entity Framework. When I first called my Web API, there was a similar lag time. The subsequent calls were all much faster than the initial one. I'm ignoring it since I don't find it to be particularly terrible (for now). That it doesn't happen in debug mode intrigues me, though. I apologize if you've already looked into these possibilities, but I do hope this was somewhat helpful.

5
1/10/2015 10:06:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow