2021-07-05 14:38:40 -06:00
|
|
|
using BenchmarkDotNet.Attributes;
|
2017-09-24 18:54:31 +02:00
|
|
|
using BenchmarkDotNet.Configs;
|
|
|
|
|
using BenchmarkDotNet.Diagnosers;
|
2021-03-05 15:36:27 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2017-09-24 18:54:31 +02:00
|
|
|
using NPoco;
|
2021-03-05 15:36:27 +01:00
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Mappers;
|
2021-07-05 14:38:40 -06:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Extensions;
|
2017-09-24 18:54:31 +02:00
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Benchmarks
|
|
|
|
|
{
|
|
|
|
|
// seeing this kind of results - templates are gooood
|
|
|
|
|
//
|
|
|
|
|
// Method | Mean | Error | StdDev | Scaled | Gen 0 | Allocated |
|
|
|
|
|
// ---------------- |-----------:|----------:|----------:|-------:|---------:|----------:|
|
|
|
|
|
// WithoutTemplate | 2,895.0 us | 64.873 us | 99.068 us | 1.00 | 183.5938 | 762.23 KB |
|
|
|
|
|
// WithTemplate | 263.2 us | 4.581 us | 4.285 us | 0.09 | 50.2930 | 207.13 KB |
|
|
|
|
|
//
|
|
|
|
|
// though the difference might not be so obvious in case of WhereIn which requires parsing
|
|
|
|
|
|
|
|
|
|
[Config(typeof(Config))]
|
|
|
|
|
public class SqlTemplatesBenchmark
|
|
|
|
|
{
|
|
|
|
|
private class Config : ManualConfig
|
|
|
|
|
{
|
|
|
|
|
public Config()
|
|
|
|
|
{
|
2020-10-07 14:38:15 +02:00
|
|
|
Add(MemoryDiagnoser.Default);
|
2017-09-24 18:54:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SqlTemplatesBenchmark()
|
|
|
|
|
{
|
2022-01-18 15:23:53 +00:00
|
|
|
var mappers = new NPoco.MapperCollection();
|
|
|
|
|
var factory = new FluentPocoDataFactory((type, iPocoDataFactory) => new PocoDataBuilder(type, mappers).Init(), mappers);
|
2017-09-24 18:54:31 +02:00
|
|
|
|
2021-07-05 14:38:40 -06:00
|
|
|
SqlContext = new SqlContext(new SqlServerSyntaxProvider(Options.Create(new GlobalSettings())), DatabaseType.SQLCe, factory);
|
2017-09-24 18:54:31 +02:00
|
|
|
SqlTemplates = new SqlTemplates(SqlContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ISqlContext SqlContext { get; }
|
|
|
|
|
private SqlTemplates SqlTemplates { get; }
|
|
|
|
|
|
|
|
|
|
[Benchmark(Baseline = true)]
|
|
|
|
|
public void WithoutTemplate()
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < 100; i++)
|
|
|
|
|
{
|
|
|
|
|
var sql = Sql.BuilderFor(SqlContext)
|
2020-10-22 14:12:07 +02:00
|
|
|
.Select<Thing1Dto>()
|
|
|
|
|
.From<Thing1Dto>()
|
|
|
|
|
.Where<Thing1Dto>(x => x.Name == "yada");
|
2017-09-24 18:54:31 +02:00
|
|
|
|
|
|
|
|
var sqlString = sql.SQL; // force-build the SQL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
|
public void WithTemplate()
|
|
|
|
|
{
|
|
|
|
|
SqlTemplates.Clear();
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 100; i++)
|
|
|
|
|
{
|
|
|
|
|
var template = SqlTemplates.Get("test", s => s
|
2020-10-22 14:12:07 +02:00
|
|
|
.Select<Thing1Dto>()
|
|
|
|
|
.From<Thing1Dto>()
|
|
|
|
|
.Where<Thing1Dto>(x => x.Name == SqlTemplate.Arg<string>("name")));
|
2017-09-24 18:54:31 +02:00
|
|
|
|
|
|
|
|
var sql = template.Sql(new { name = "yada" });
|
|
|
|
|
|
|
|
|
|
var sqlString = sql.SQL; // force-build the SQL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 14:12:07 +02:00
|
|
|
[TableName("zbThing1")]
|
|
|
|
|
[PrimaryKey("id", AutoIncrement = false)]
|
|
|
|
|
[ExplicitColumns]
|
|
|
|
|
public class Thing1Dto
|
|
|
|
|
{
|
|
|
|
|
[Column("id")]
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[Column("name")]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-24 18:54:31 +02:00
|
|
|
}
|
|
|
|
|
}
|