Last active
January 21, 2023 15:15
-
-
Save misha130/69ada16eee5e8981691b5826b887bcd7 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.UseInMemoryDatabase("database")); | |
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() | |
})); | |
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.ToListAsync(); | |
} | |
[Benchmark] | |
public Task<List<TodoItem>> AsyncToList() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.ToListAsync(); | |
} | |
[Benchmark] | |
public TodoItem First() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.First(); | |
} | |
[Benchmark] | |
public async Task<TodoItem> AsyncFirstAwait() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return await dbContext.TodoItems.FirstAsync(); | |
} | |
[Benchmark] | |
public Task<TodoItem> AsyncFirst() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.FirstAsync(); | |
} | |
[Benchmark] | |
public List<TodoItem> ToListNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.AsNoTracking().ToList(); | |
} | |
[Benchmark] | |
public async Task<List<TodoItem>> AsyncToListAwaitNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return await dbContext.TodoItems.AsNoTracking().ToListAsync(); | |
} | |
[Benchmark] | |
public Task<List<TodoItem>> AsyncToListNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.AsNoTracking().ToListAsync(); | |
} | |
[Benchmark] | |
public TodoItem FirstNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.AsNoTracking().First(); | |
} | |
[Benchmark] | |
public async Task<TodoItem> AsyncFirstAwaitNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return await dbContext.TodoItems.AsNoTracking().FirstAsync(); | |
} | |
[Benchmark] | |
public Task<TodoItem> AsyncFirstNoTracking() | |
{ | |
var dbContext = serviceProvider.GetService<ApplicationDbContext>(); | |
return dbContext.TodoItems.AsNoTracking().FirstAsync(); | |
} | |
} | |
public class TodoItem | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} | |
public class ApplicationDbContext : DbContext | |
{ | |
public ApplicationDbContext( | |
DbContextOptions<ApplicationDbContext> options) | |
: base(options) | |
{ | |
} | |
public DbSet<TodoItem> TodoItems => Set<TodoItem>(); | |
} | |
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