Files
2022-01-04 08:54:19 -05:00

54 lines
1.5 KiB
C#

using BenchmarkDotNet.Attributes;
using Dapper.Tests.Performance.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel;
using System.Linq;
namespace Dapper.Tests.Performance
{
[Description("EF Core")]
public class EFCoreBenchmarks : BenchmarkBase
{
private EFCoreContext Context;
private static readonly Func<EFCoreContext, int, Post> compiledQuery =
EF.CompileQuery((EFCoreContext ctx, int id) => ctx.Posts.First(p => p.Id == id));
[GlobalSetup]
public void Setup()
{
BaseSetup();
Context = new EFCoreContext(ConnectionString);
}
[Benchmark(Description = "First")]
public Post First()
{
Step();
return Context.Posts.First(p => p.Id == i);
}
[Benchmark(Description = "First (Compiled)")]
public Post Compiled()
{
Step();
return compiledQuery(Context, i);
}
[Benchmark(Description = "SqlQuery")]
public Post SqlQuery()
{
Step();
return Context.Posts.FromSqlRaw("select * from Posts where Id = {0}", i).First();
}
[Benchmark(Description = "First (No Tracking)")]
public Post NoTracking()
{
Step();
return Context.Posts.AsNoTracking().First(p => p.Id == i);
}
}
}