Entity Framework startup Speed Optimization, entityframework
After recently developed services are hosted on IIS, some problems occur. For example, after deployment, the first startup is slow; after the program is placed for a while, the request is slow again. For example, the first problem can be explained as loading the Assembly into the memory may be slow when a service is requested for the first time, the second problem may be that the thread recycling mechanism of IIS has been put for a long time, idle processes have been recycled, and requests again may be slow.
I didn't care too much at the beginning, but with the release of the system, this initial request, or the long wait for the first request after a few minutes of idle, made the App experience very poor, in many cases, the App does not load data for a long time. If the front-end is not properly processed, the App may be suspended. So I spent some time studying the cause.
At the beginning, I thought it was a problem with the WebService framework. After using the App, you can see through Fiddler that the database of a certain request is up to 6 s, and each time the request times out, it appears in db. Access, which is obviously abnormal. In the early days, we used the original ADO to access the database.. NET executes SQL statements. If there are parameters, parameterization prevents SQL injection, and then basic addition, deletion, and query are implemented through batch generation of stored procedures, the access statements and times of each service are recorded at the underlying framework layer to facilitate optimization. At first, we thought that the performance would fall, but with the expansion of the scale, the efficiency is too low, so we switched to the Entity Framework, by automatically generating entities, then, add, delete, modify, and query are implemented in the way of LINQ, which is much more efficient.
After realizing that it was a problem with data access, I started to find the problem that Entity Framework started slowly for the first time. Then I found it online and found that many people have encountered the same problem. The solution is also mature, such as Pre-Generated Mapping Views and NGen. Here we will explain one by one. In fact, there are many such solutions on the Internet. I will record them here for future reference.
I. Entity Framework Initial Startup Optimization
The methods mentioned below require the support of the Entity Framework version. Therefore, we recommend that you use the latest version when using the Entity Framework.
Pre-Generated Mapping Views
Precautions for using EntityFramework are described in this article in Performance Considerations for Entity Framework. It takes a lot of time to generate a view. When the Entity Framework performs queries or writes data to the database, some map views must be generated to access the database. These map views are a series of abstract declarations on objects in the database. These data is also part of the cache metadata of the app domain, in the same application scope, You can reuse these objects when creating multiple database access contexts. It is time-consuming to generate a ing view during the first query. For more information, see the Pre-Generated Mapping Views on msdn, this article provides two methods to generate a ing view in advance. One is to generate a ing view by installing the EF Power Tools plug-in Visual Studio (in slices, from msdn ).
This method is not restricted by EF versions.
The website also provides the second method to generate a view in advance through code, which must require EF to be of version 6.0 or later. This is also the method used to warm up Entity Framework in this blog post. Generally, for a Web site, you can put it in App_Start for initialization:
Protected void Application_Start (object sender, EventArgs e) {// push EntityFramework using (var dbcontext = new mcccEntities () {var objectContext = (IObjectContextAdapter) dbcontext ). objectContext; var mappingCollection = (StorageMappingItemCollection) objectContext. metadataWorkspace. getItemCollection (DataSpace. CSSpace); mappingCollection. generateViews (new List <EdmSchemaError> ());}}
After the above processing, the initial database query of the program has dropped from 6 s to about 3 s.
In addition to the above optimization, you can use NGen for EF6.0 and later versions to further increase the speed.
Ii. NGen Optimization
For more information about optimization, see the article Improving Startup Performance with NGen (EF6 Onwards). The following method is mainly for EF6 and later versions, because it comes with this feature below this version, it is clear in this article that "in EF earlier than 6.0, the core class library of EF Runtime is also.. NET Framework. NET core class library is automatically loaded when it is loaded. In versions 6.0 and later, EF has been integrated into the EntityFramework NuGet package throughout the runtime, local images must be generated using the NGen tool to achieve similar results ".
Here, we will first talk about the role of the NGen tool and why it can accelerate the Startup Performance of the application .. The. NET framework allows you to generate local image files for hosted applications or assembly to help applications start faster and reduce memory usage in some cases. Before the application is executed, the managed code assembly can be reduced by translating it into a file containing local machine commands. net jit compiler generates local command code when the application starts, which can speed up application startup.
It's easy to use NGen.
1: Start the cmd program on the console as an administrator
2: switch to the Local. NET tool directory:
For 32-bit machines, generally under % WINDIR % \ Microsoft. NET \ Framework \ v4.0.30319 \
For 64-bit machines, usually under % WINDIR % \ Microsoft. NET \ Framework64 \ v4.0.30319 \
3: Run ngen install and add the assembly path and name.
For example, on my machine, you can see the following:
After this operation, the first access speed to the database was finally controlled to within Ms.
The above is the optimization of EF, which solves the problem of the first access to the database after the first deployment. If the application is placed for a while, the re-request will slow down the re-access due to thread pool collection, solve this problem by setting IIS.
Iii. IIS settings
After deploying services or sites on IIS, you can set them in the corresponding thread pool, for example:
- Idle timeout. The default value is 20 minutes, indicating that the thread is idle for 20 minutes. If no request is received, the corresponding thread is idle. Change it to 0 here.
- For example, the Regular recycling time is usually 1740 minutes by default, which is about 29 hours (why is it 29 instead of 24, it is said that it is to minimize the impact of the recycling time, it is convenient for debugging to locate the problem. 29 is the first prime number after 24), indicating that IIS will recycle the threads in the Application Thread Pool every 29 hours.
In this way, the second problem can be solved.
Iv. Summary
This article briefly introduces how to optimize the Initial Startup speed of EntityFramework, as well as solutions to Prevent IIS threads from time-out and idle, as well as solutions for long initial run time due to frequent thread recycling, we hope this will help you solve the above problems.