Skip to content

Instantly share code, notes, and snippets.

@sa-es-ir
Created May 2, 2024 16:46
Show Gist options
  • Select an option

  • Save sa-es-ir/75d679ce28404650e9ee60430300161a to your computer and use it in GitHub Desktop.

Select an option

Save sa-es-ir/75d679ce28404650e9ee60430300161a to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Reports;
using EFQueryOptimization.Context;
using EFQueryOptimization.Entities;
using Microsoft.EntityFrameworkCore;
namespace EFQueryOptimization;
[Config(typeof(Config))]
[HideColumns(Column.RatioSD, Column.AllocRatio)]
[MemoryDiagnoser(false)]
public class EFNoTrackingBenchmark
{
private class Config : ManualConfig
{
public Config()
{
SummaryStyle = SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend);
}
}
[Benchmark(Baseline = true)]
public List<Role> Tracking()
{
var context = new ApplicationDbContext();
return context.Roles
.Include(x => x.UserRoles)
.ThenInclude(x => x.User)
.Take(10)
.ToList();
}
[Benchmark]
public List<Role> NoTracking()
{
var context = new ApplicationDbContext();
return context.Roles
.Include(x => x.UserRoles)
.ThenInclude(x => x.User)
.AsNoTracking()
.Take(10)
.ToList();
}
[Benchmark]
public List<Role> NoTrackingWithIdentityResolution()
{
var context = new ApplicationDbContext();
return context.Roles
.Include(x => x.UserRoles)
.ThenInclude(x => x.User)
.AsNoTrackingWithIdentityResolution()
.Take(10)
.ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment