EF Extensions - BulkUpdate - Performance Comparison

CSharp · Console

/Program.cs

// Entity Framework Extensions
// Doc: https://entityframework-extensions.net/bulk-update

// @nuget: Z.EntityFramework.Extensions
// @nuget: EntityFramework

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data.Entity;
using System.Linq;

public class Program
{
	public static List<BenchmarkResult> BenchmarkResults = new List<BenchmarkResult>();
	
	public static void Main()
	{
		JustInTime_Compile();
		
		// Generate X customers
		GenerateCustomers(2000);
		
		var clockSaveChanges = new Stopwatch();
		var clockBulkUpdate = new Stopwatch();
		
		using (var context = new EntityContext())
		{
			// SaveChanges
			var customers = context.Customers.ToList();			
			customers.ForEach(x => x.FirstName += "_z");

			clockSaveChanges.Start();
			context.SaveChanges();
			clockSaveChanges.Stop();

			BenchmarkResults.Add(new BenchmarkResult() { Action = "SaveChanges (EF)", Entities = customers.Count, Performance = clockSaveChanges.ElapsedMilliseconds + " ms" });
		}
		
		using (var context = new EntityContext())
		{
			// BulkUpdate
			var customers = context.Customers.ToList();			
			customers.ForEach(x => x.LastName += "_z");

			clockBulkUpdate.Start();
			context.BulkUpdate(customers);
			clockBulkUpdate.Stop();

			var timeFaster = Math.Round((double)clockSaveChanges.ElapsedMilliseconds / clockBulkUpdate.ElapsedMilliseconds, 2);
			var reducedPercent = Math.Round((double)(clockSaveChanges.ElapsedMilliseconds - clockBulkUpdate.ElapsedMilliseconds) / clockSaveChanges.ElapsedMilliseconds, 2) * 100;
			BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkUpdate", Entities = customers.Count, Performance = string.Format("{0} ms", clockBulkUpdate.ElapsedMilliseconds), TimeFaster = string.Format("{0}x faster than SaveChanges", timeFaster), ReducedPercent = string.Format("Time reduced by {0}% compared to SaveChanges", reducedPercent) });
		}
		
		FiddleHelper.WriteTable(BenchmarkResults);
	}
		 
	public static void JustInTime_Compile()
	{
		// SaveChanges
		using (var context = new EntityContext())
		{
			var customers = context.Customers.ToList();			
			customers.ForEach(x => x.FirstName += "_z");
			
			context.SaveChanges();
		}

		// BulkUpdate
		using (var context = new EntityContext())
		{
			var customers = context.Customers.ToList();			
			customers.ForEach(x => x.LastName += "_z");
			
			context.BulkUpdate(customers);
		}
		
		// Clean
		using (var context = new EntityContext())
		{
			context.BulkDelete(context.Customers);
		}
	}
	
	public static List<Customer> GenerateCustomers(int count)
	{
		var list = new List<Customer>();
		
		for(int i = 0; i < count; i++)
		{
			list.Add(new Customer() { Code = "Code_" + i, FirstName = "FirstName_" + i, LastName = "LastName_" + i });
		}

		using (var context = new EntityContext())
		{
			context.BulkInsert(list, options => options.AutoMapOutputDirection = false);
		}
		
		return list;
	}

	public class EntityContext : DbContext
	{
		public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
		{
		}
		
		public DbSet<Customer> Customers { get; set; }
	}

	public class Customer
	{
		public int CustomerID { get; set; }
		public string Code { get; set; }
		public string FirstName { get; set; }
		public string LastName { get; set; }
	}
	
	public class BenchmarkResult
	{
		public string Action { get; set; }
		public int Entities { get; set; }
		public string Performance { get; set; }		
		public string TimeFaster { get; set; }
		public string ReducedPercent { get; set; }
	}
}
;