Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance cost of creating ObjectContext in every method in Entity Framework v1

While using .NET 3.5 SP1 in ASP.NET MVC application, the ObjectContext can have lifetime on one Http Request OR of a SINGLE method.

using (MyEntities context =  new MyEntities ())
{
//DO query etc
}

How much is increased performance cost of creating ObjectContext in every method VS per request ?

Thanks.

like image 429
dev Avatar asked Nov 04 '09 02:11

dev


1 Answers

I am using EF6 and schema of 163 entities that are db first generated from oracle.

I am measuring Initialization times and time to get 100 records from indexed table.

C# Test

var times = new List<Tuple<DateTime, DateTime, DateTime>>();
var carTypes = new List<CAR_TYPE>();
var j = 1;
while (j <= 10000)
{
    for (int i = 0; i < j; i++)
    {
        var startTime = DateTime.Now;

        using (var db = new EcomEntities())
        {
            var contextInitializationTime = DateTime.Now;
            carTypes = db.CAR_TYPE.Take(100).ToList();
            var executionTime = DateTime.Now;
            times.Add(new Tuple<DateTime, DateTime, DateTime>(startTime, contextInitializationTime, executionTime));
        }
    }
    var averageInitTime = times.Average(o => o.Item2.Subtract(o.Item1).TotalMilliseconds);
    var averageRunTime = times.Average(o => o.Item3.Subtract(o.Item1).TotalMilliseconds);
    Debug.WriteLine("averageInitTime - " + j + " " + averageInitTime);
    Debug.WriteLine("averageRunTime - " + j + " " + averageRunTime);
    j = j*10;
}

Results:

                    Runs        MS                         Runs      MS
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 1     | 134.0134 | averageRunTime  | 1     | 1719.172 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 10    | 12.27395 | averageRunTime  | 10    | 160.3797 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 100   | 1.540695 | averageRunTime  | 100   | 19.94794 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 1000  | 0.281756 | averageRunTime  | 1000  | 6.121224 |
+------------------+-------+----------+-----------------+-------+----------+
| averageInitTime  | 10000 | 0.167058 | averageRunTime  | 10000 | 4.751353 |
+------------------+-------+----------+-----------------+-------+----------+
like image 54
Matas Vaitkevicius Avatar answered Oct 20 '22 23:10

Matas Vaitkevicius