mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-18 14:27:47 +08:00
54 lines
1.5 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|