EF Extensions - Home - Insert Benchmark

CSharp · Console

/Program.cs

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

// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Extensions.EFCore

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

public class Program
{
	public static List<BenchmarkResult> BenchmarkResults = new List<BenchmarkResult>();
	
	public static void Main()
	{
		using (var context = new EntityContext())
		{
			context.Database.EnsureCreated();
		}
		JustInTime_Compile();
		
		// Generate X customers
		var customers = GenerateCustomers(5000);
		
		var clockSaveChanges = new Stopwatch();
		var clockBulkInsert = new Stopwatch();
		var clockBulkInsertNoOutput = new Stopwatch();
		var clockBulkInsertOptimized = new Stopwatch();
		
		using (var context = new EntityContext())
		{
			// SaveChanges
			context.Customers.AddRange(customers);
			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())
		{
			// BulkInsert
			clockBulkInsert.Start();
			context.BulkInsert(customers); // performance can be improved with options
			clockBulkInsert.Stop();

			var timeFaster = Math.Round((double)clockSaveChanges.ElapsedMilliseconds / clockBulkInsert.ElapsedMilliseconds, 2);
			var reducedPercent = Math.Round((double)(clockSaveChanges.ElapsedMilliseconds - clockBulkInsert.ElapsedMilliseconds) / clockSaveChanges.ElapsedMilliseconds, 2) * 100;
			BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkInsert (Outputting values)", Entities = customers.Count, Performance = $"{clockBulkInsert.ElapsedMilliseconds} ms", TimeFaster = $"{timeFaster}x faster than SaveChanges", ReducedPercent = $"Time reduced by {reducedPercent}% compared to SaveChanges" });
		}
		
		using (var context = new EntityContext())
		{
			// BulkInsert (without outputting)
			clockBulkInsertNoOutput.Start();
			context.BulkInsert(customers, options => options.AutoMapOutputDirection = false);
			clockBulkInsertNoOutput.Stop();

			var timeFaster = Math.Round((double)clockSaveChanges.ElapsedMilliseconds / clockBulkInsertNoOutput.ElapsedMilliseconds, 2);
			var reducedPercent = Math.Round((double)(clockSaveChanges.ElapsedMilliseconds - clockBulkInsertNoOutput.ElapsedMilliseconds) / clockSaveChanges.ElapsedMilliseconds, 2) * 100;
			BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkInsert (Not Outputting values)", Entities = customers.Count, Performance = $"{clockBulkInsertNoOutput.ElapsedMilliseconds} ms", TimeFaster = $"{timeFaster}x faster than SaveChanges", ReducedPercent = $"Time reduced by {reducedPercent}% compared to SaveChanges" });
		}
		
		using (var context = new EntityContext())
		{
			// BulkInsertOptimized
			clockBulkInsertOptimized.Start();			
			context.BulkInsertOptimized(customers);			
			clockBulkInsertOptimized.Stop();

			var timeFaster = Math.Round((double)clockSaveChanges.ElapsedMilliseconds / clockBulkInsertOptimized.ElapsedMilliseconds, 2);
			var reducedPercent = Math.Round((double)(clockSaveChanges.ElapsedMilliseconds - clockBulkInsertOptimized.ElapsedMilliseconds) / clockSaveChanges.ElapsedMilliseconds, 2) * 100;
			BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkInsertOptimized", Entities = customers.Count, Performance = $"{clockBulkInsertOptimized.ElapsedMilliseconds} ms", TimeFaster = $"{timeFaster}x faster than SaveChanges", ReducedPercent = $"Time reduced by {reducedPercent}% compared to SaveChanges" });
		}

		FiddleHelper.WriteTable(BenchmarkResults);
	}
		 
	public static void JustInTime_Compile()
	{
		var customers = GenerateCustomers(20);
		
		// SaveChanges
		using (var context = new EntityContext())
		{
			context.Customers.AddRange(customers);
			context.SaveChanges();
			
			// Clean
			context.BulkDelete(customers);
		}
		
		// BulkInsert
		using (var context = new EntityContext())
		{
			context.BulkInsert(customers);
			
			// Clean
			context.BulkDelete(customers);
		}

		// BulkInsertOptimized
		using (var context = new EntityContext())
		{
			context.BulkInsertOptimized(customers);
			
			// Clean
			context.BulkDelete(customers);
		}
	}
	
	public static List<Customer> GenerateCustomers(int count)
	{
		var list = new List<Customer>();
		
		for(int i = 0; i < count; i++)
		{
			list.Add(new Customer() { Name = "Customer_" + i, Description = "Description_" +  i, IsActive = i % 2 == 0 });
		}

		return list;
	}

	public class EntityContext : DbContext
	{
		public EntityContext() 
		{
		}

		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));

			base.OnConfiguring(optionsBuilder);
		}
		
		public DbSet<Customer> Customers { get; set; }
	}

	public class Customer
	{
		public int CustomerID { get; set; }
		public string Name { get; set; }
		public string Description { get; set; }
		public Boolean IsActive { get; set; }		
		public string Column1 { get; set; }
		public string Column2 { get; set; }
		public string Column3 { get; set; }
		public string Column4 { get; set; }
		public string Column5 { get; set; }
		public string Column6 { get; set; }
		public string Column7 { get; set; }
		public string Column8 { get; set; }
		public string Column9 { 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; }
	}
}
;