Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Six seconds warmup time for the first entity framework 6 nonquery

From my integration test:

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


public void Create(UserDTO userDTO)
{
    var user = userDTO.ToEntity();
    _context.Entry(user).State = EntityState.Added;
    _context.SaveChanges();
}

6,2 seconds to do an "sql insert" is crazy. I already see the application users complaining when they first open a project which they use the whole year. So everyday they have to wait 6 seconds...

I thought the warm up time in EF6 has improved?

Is there anything I can do to improve this miserable behavior?

like image 494
Pascal Avatar asked Dec 13 '13 11:12

Pascal


3 Answers

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);
}
like image 98
Mert Akcakaya Avatar answered Nov 29 '22 01:11

Mert Akcakaya


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. ---------------------
like image 29
Iguanaware Avatar answered Nov 29 '22 01:11

Iguanaware


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

like image 29
VahidN Avatar answered Nov 29 '22 03:11

VahidN