2016-10-27 17:36:08 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
using BenchmarkDotNet.Attributes;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using Moq;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
using Umbraco.Cms.Core.Models;
|
2021-02-12 13:36:50 +01:00
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Mappers;
|
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Querying;
|
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
|
2021-02-10 10:37:57 +01:00
|
|
|
|
using Umbraco.Cms.Persistence.SqlCe;
|
2016-10-27 17:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Benchmarks
|
|
|
|
|
|
{
|
2018-03-27 16:42:52 +02:00
|
|
|
|
[MemoryDiagnoser]
|
2016-10-27 17:36:08 +02:00
|
|
|
|
public class ModelToSqlExpressionHelperBenchmarks
|
|
|
|
|
|
{
|
2019-04-02 13:35:01 +02:00
|
|
|
|
protected Lazy<ISqlContext> MockSqlContext()
|
2019-03-29 08:30:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sqlContext = Mock.Of<ISqlContext>();
|
|
|
|
|
|
var syntax = new SqlCeSyntaxProvider();
|
|
|
|
|
|
Mock.Get(sqlContext).Setup(x => x.SqlSyntax).Returns(syntax);
|
2019-04-02 13:35:01 +02:00
|
|
|
|
return new Lazy<ISqlContext>(() => sqlContext);
|
2019-03-29 08:30:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-26 10:47:14 +00:00
|
|
|
|
protected MapperConfigurationStore CreateMaps()
|
|
|
|
|
|
=> new MapperConfigurationStore();
|
2019-03-29 08:30:51 +01:00
|
|
|
|
|
2016-10-27 17:36:08 +02:00
|
|
|
|
public ModelToSqlExpressionHelperBenchmarks()
|
|
|
|
|
|
{
|
2019-03-29 08:30:51 +01:00
|
|
|
|
var contentMapper = new ContentMapper(MockSqlContext(), CreateMaps());
|
2016-10-27 17:36:08 +02:00
|
|
|
|
_cachedExpression = new CachedExpression();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
var mapperCollection = new Mock<IMapperCollection>();
|
|
|
|
|
|
mapperCollection.Setup(x => x[It.IsAny<Type>()]).Returns(contentMapper);
|
|
|
|
|
|
_mapperCollection = mapperCollection.Object;
|
2016-10-27 17:36:08 +02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2016-10-27 17:36:08 +02:00
|
|
|
|
private readonly ISqlSyntaxProvider _syntaxProvider = new SqlCeSyntaxProvider();
|
|
|
|
|
|
private readonly CachedExpression _cachedExpression;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
private readonly IMapperCollection _mapperCollection;
|
|
|
|
|
|
|
2016-10-27 17:36:08 +02:00
|
|
|
|
[Benchmark(Baseline = true)]
|
|
|
|
|
|
public void WithNonCached()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var a = i;
|
2018-03-27 16:42:52 +02:00
|
|
|
|
var b = i * 10;
|
2016-10-27 17:36:08 +02:00
|
|
|
|
Expression<Func<IContent, bool>> predicate = content =>
|
|
|
|
|
|
content.Path.StartsWith("-1") && content.Published && (content.ContentTypeId == a || content.ContentTypeId == b);
|
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
var modelToSqlExpressionHelper = new ModelToSqlExpressionVisitor<IContent>(_syntaxProvider, _mapperCollection);
|
2016-10-27 17:36:08 +02:00
|
|
|
|
var result = modelToSqlExpressionHelper.Visit(predicate);
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
2016-10-27 17:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
[Benchmark]
|
2016-10-27 17:36:08 +02:00
|
|
|
|
public void WithCachedExpression()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var a = i;
|
|
|
|
|
|
var b = i * 10;
|
|
|
|
|
|
Expression<Func<IContent, bool>> predicate = content =>
|
|
|
|
|
|
content.Path.StartsWith("-1") && content.Published && (content.ContentTypeId == a || content.ContentTypeId == b);
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
var modelToSqlExpressionHelper = new ModelToSqlExpressionVisitor<IContent>(_syntaxProvider, _mapperCollection);
|
2016-10-27 17:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
//wrap it!
|
|
|
|
|
|
_cachedExpression.Wrap(predicate);
|
|
|
|
|
|
|
|
|
|
|
|
var result = modelToSqlExpressionHelper.Visit(_cachedExpression);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|