Created
January 21, 2023 16:29
-
-
Save misha130/791e1f9f24084d0cc3d4a75703d2cbb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Jobs; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.EntityFrameworkCore; | |
using BenchmarkDotNet.Running; | |
[SimpleJob(RuntimeMoniker.Net60)] | |
[MemoryDiagnoser] | |
[ThreadingDiagnoser] | |
public class AsyncVsNonAsyncEF | |
{ | |
public IServiceProvider? serviceProvider = null; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
var serviceCollection = new ServiceCollection().AddDbContext<ApplicationDbContext>(o => | |
o.UseSqlServer("Data Source=localhost\\SQLEXPRESS;Initial Catalog=benchmark;Integrated Security=SSPI;TrustServerCertificate=True;Encrypt=False;")); | |
serviceProvider = serviceCollection.BuildServiceProvider(); | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
dbContext.TodoItems.AddRange(Enumerable.Repeat<object>(null, 10000).Select((x, i) => new TodoItem | |
{ | |
Name = Guid.NewGuid().ToString(), | |
Description = Guid.NewGuid().ToString(), | |
ToDoList = new ToDoList | |
{ | |
Name = Guid.NewGuid().ToString() | |
} | |
})); | |
dbContext.SaveChanges(); | |
} | |
[GlobalCleanup] | |
public void Cleanup() | |
{ | |
var serviceCollection = new ServiceCollection().AddDbContext<ApplicationDbContext>(o => | |
o.UseSqlServer("Data Source=localhost\\SQLEXPRESS;Initial Catalog=benchmark;Integrated Security=SSPI;TrustServerCertificate=True;Encrypt=False;")); | |
serviceProvider = serviceCollection.BuildServiceProvider(); | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
dbContext.TodoLists.RemoveRange(dbContext.TodoLists); | |
dbContext.TodoItems.RemoveRange(dbContext.TodoItems); | |
dbContext.SaveChanges(); | |
} | |
[Benchmark] | |
public List<TodoItem> ToList() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.ToList(); | |
} | |
[Benchmark] | |
public async Task<List<TodoItem>> AsyncToListAwait() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return await dbContext.TodoItems.Include(d => d.ToDoList).ToListAsync(); | |
} | |
[Benchmark] | |
public Task<List<TodoItem>> AsyncToList() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.Include(d => d.ToDoList).ToListAsync(); | |
} | |
[Benchmark] | |
public TodoItem First() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.Include(d => d.ToDoList).First(); | |
} | |
[Benchmark] | |
public async Task<TodoItem> AsyncFirstAwait() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return await dbContext.TodoItems.Include(d => d.ToDoList).FirstAsync(); | |
} | |
[Benchmark] | |
public Task<TodoItem> AsyncFirst() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.Include(d => d.ToDoList).FirstAsync(); | |
} | |
[Benchmark] | |
public List<TodoItem> ToListNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.Include(d => d.ToDoList).AsNoTracking().ToList(); | |
} | |
[Benchmark] | |
public async Task<List<TodoItem>> AsyncToListAwaitNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return await dbContext.TodoItems.Include(d => d.ToDoList).AsNoTracking().ToListAsync(); | |
} | |
[Benchmark] | |
public Task<List<TodoItem>> AsyncToListNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.Include(d => d.ToDoList).AsNoTracking().ToListAsync(); | |
} | |
[Benchmark] | |
public TodoItem FirstNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.Include(d => d.ToDoList).AsNoTracking().First(); | |
} | |
[Benchmark] | |
public async Task<TodoItem> AsyncFirstAwaitNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return await dbContext.TodoItems.Include(d => d.ToDoList).AsNoTracking().FirstAsync(); | |
} | |
[Benchmark] | |
public Task<TodoItem> AsyncFirstNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.Include(d => d.ToDoList).AsNoTracking().FirstAsync(); | |
} | |
} | |
public class TodoItem | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
public ToDoList ToDoList { get; set; } | |
} | |
public class ToDoList | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public List<TodoItem> Items { get; set; } | |
} | |
public class ApplicationDbContext : DbContext | |
{ | |
public ApplicationDbContext( | |
DbContextOptions<ApplicationDbContext> options) | |
: base(options) | |
{ | |
} | |
public DbSet<TodoItem> TodoItems => Set<TodoItem>(); | |
public DbSet<ToDoList> TodoLists => Set<ToDoList>(); | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<AsyncVsNonAsyncEF>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment