mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2026-05-18 01:10:23 +00:00
Added a basic benchmark
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables.Generator\EntityFrameworkCore.Projectables.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables\EntityFrameworkCore.Projectables.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using EntityFrameworkCore.Projectables.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Benchmarks
|
||||
{
|
||||
public class PlainOverhead
|
||||
{
|
||||
class TestEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
class TestDbContext : DbContext
|
||||
{
|
||||
readonly bool _useProjectables;
|
||||
|
||||
public TestDbContext(bool useProjectables)
|
||||
{
|
||||
_useProjectables = useProjectables;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ReadmeSample;Trusted_Connection=True");
|
||||
|
||||
if (_useProjectables)
|
||||
{
|
||||
optionsBuilder.UseProjectables();
|
||||
}
|
||||
}
|
||||
|
||||
public DbSet<TestEntity> Entities => Set<TestEntity>();
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public void WithoutProjectables()
|
||||
{
|
||||
using var dbContext = new TestDbContext(false);
|
||||
|
||||
for (int i = 0; i < 10000; i++)
|
||||
{
|
||||
dbContext.Entities.Select(x => x.Id).ToQueryString();
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void WithProjectables()
|
||||
{
|
||||
using var dbContext = new TestDbContext(true);
|
||||
|
||||
for (int i = 0; i < 10000; i++)
|
||||
{
|
||||
dbContext.Entities.Select(x => x.Id).ToQueryString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Running;
|
||||
using EntityFrameworkCore.Projectables.Benchmarks;
|
||||
|
||||
BenchmarkSwitcher.FromAssembly(typeof(PlainOverhead).Assembly).Run(args, DefaultConfig.Instance.WithOption(ConfigOptions.DisableOptimizationsValidator, true));
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using EntityFrameworkCore.Projectables.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Benchmarks
|
||||
{
|
||||
public class ProjectableProperties
|
||||
{
|
||||
public class TestEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public int IdPlus1 => Id + 1;
|
||||
}
|
||||
|
||||
class TestDbContext : DbContext
|
||||
{
|
||||
readonly bool _useProjectables;
|
||||
|
||||
public TestDbContext(bool useProjectables)
|
||||
{
|
||||
_useProjectables = useProjectables;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ReadmeSample;Trusted_Connection=True");
|
||||
|
||||
if (_useProjectables)
|
||||
{
|
||||
optionsBuilder.UseProjectables();
|
||||
}
|
||||
}
|
||||
|
||||
public DbSet<TestEntity> Entities => Set<TestEntity>();
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public void WithoutProjectables()
|
||||
{
|
||||
using var dbContext = new TestDbContext(false);
|
||||
|
||||
for (int i = 0; i < 10000; i++)
|
||||
{
|
||||
dbContext.Entities.Select(x => x.Id + 1).ToQueryString();
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void WithProjectables()
|
||||
{
|
||||
using var dbContext = new TestDbContext(true);
|
||||
|
||||
for (int i = 0; i < 10000; i++)
|
||||
{
|
||||
dbContext.Entities.Select(x => x.IdPlus1).ToQueryString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user