Six seconds warmup time for the first entity framework 6 nonquery

前端 未结 4 1682
夕颜
夕颜 2021-01-05 23:51

From my integration test:

// Act
Stopwatch w = new Stopwatch();
w.Start();
userService.Create(userDTO);
w.Stop();


public void Create(UserDTO userDTO)
{
            


        
相关标签:
4条回答
  • 2021-01-06 00:20

    Try EF6 CodeFirst View Generation T4 Template for C#. Pre-generated views improve application start-up time by moving the work that would have to be done at runtime to design time. more info

    0 讨论(0)
  • 2021-01-06 00:26

    Ngen will cut that in half. Entity Framework is not compiled natively. I have a script that compiles everything in the output directory. Makes a big difference even when debugging.

        @ECHO OFF
        REM *********************************************************************************************************
        REM Compiles project's .net assemblies to native images to improve startup time and overall performance
        REM ---------------------------------------------------------------------------------------------------------
        REM Author: Brian Freeman
        REM History:
        REM     12/2/2013 Created
        REM *********************************************************************************************************
        REM Scenarios:
        REM     /Debug          - Generate images that can be used under a debugger
        REM     /Profile        - Generate images that can be used under a profiler
        REM     /NoDependencies - Generate the minimal number of native images
        REM                       required by this scenario
        REM Options
        REM     /verbose
    
        SET DEFAULTOPTIONS= /Debug /Verbose
    
        @ECHO. -------------------------------------
        @ECHO. Native Image Generator (Ngen.exe) 
        @ECHO. -------------------------------------
        @ECHO Current Defaults are %DEFAULTOPTIONS%
    
    
        REM ---------------------------------------------------------------------
        REM Small chance these might not be the locations of the .net framework
        REM Needs to be added to as the framework gets new versions
        REM ---------------------------------------------------------------------
        SET ngenx86=C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe
        SET ngenx64=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ngen.exe
    
        REM Run from the current Directory to ensure any dependencies are available
        pushd ..\DatabaseHelper\Bin\Debug
    
        REM SKIP vshost.exe
        ATTRIB +S ..\*.vshost.exe /s
    
        @ECHO. -------------------------------------
        @ECHO. Generator x64 Images
        @ECHO. -------------------------------------
    
        for /f "delims=" %%f in ('dir *.dll /b /s /a-d-h-s') do %ngenx64% install  "%%f"  %* %DEFAULTOPTIONS%
        for /f "delims=" %%f in ('dir *.exe /b /s /a-d-h-s') do %ngenx64% install  "%%f"  %* %DEFAULTOPTIONS%
    
        @ECHO. -------------------------------------
        @ECHO. Generator x86 Images
        @ECHO. -------------------------------------
    
        for /f "delims=" %%f in ('dir *.dll /b /s /a-d-h-s') do %ngenx86% install  "%%f"  %* %DEFAULTOPTIONS%
        for /f "delims=" %%f in ('dir *.exe /b /s /a-d-h-s') do %ngenx86% install  "%%f"  %* %DEFAULTOPTIONS%
        @ECHO OFF
    
        ATTRIB -S ..\*.vshost.exe /s
    
        popd
    
    
        @ECHO. ---------------------
        @ECHO. FINISHED
        @ECHO. ---------------------
    
    0 讨论(0)
  • 2021-01-06 00:33

    The time is not spent to insert a simple data. EF creates the model in the memory, that is where the time you spent goes.

    EF creates Entity Data Model and executes View Generation(not db views) for the first time you do an operation on context. Have a look at this blog post.

    Take a look here to improve the performance by using pre-generated views to decrease model load time.

    To improve the performance you can initialize your context async when you start your application. Beware the multithreading issues.

    using (var context = new MyContext())
    {
        context.Database.Initialize(false);
    }
    
    0 讨论(0)
  • 2021-01-06 00:35

    Have you tried EF 6.0.2? It addresses several performance issues and may help. Find more details (including a list of bugs fixed in this release) here

    0 讨论(0)
提交回复
热议问题