Files
Umbraco-CMS/tests/Umbraco.Tests.Benchmarks/ModelToSqlExpressionHelperBenchmarks.cs
Nikolaj Geisle 7aeb400fce V10: fix build warnings in test projects (#12509)
* Run code cleanup

* Dotnet format benchmarks project

* Fix up Test.Common

* Run dotnet format + manual cleanup

* Run code cleanup for unit tests

* Run dotnet format

* Fix up errors

* Manual cleanup of Unit test project

* Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Fix according to review

* Fix after merge

* Fix errors

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
Co-authored-by: Zeegaan <nge@umbraco.dk>
2022-06-21 08:09:38 +02:00

82 lines
2.8 KiB
C#

using System;
using System.Linq.Expressions;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Options;
using Moq;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.Mappers;
using Umbraco.Cms.Infrastructure.Persistence.Querying;
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
using Umbraco.Cms.Persistence.SqlServer.Services;
namespace Umbraco.Tests.Benchmarks;
[MemoryDiagnoser]
public class ModelToSqlExpressionHelperBenchmarks
{
private readonly CachedExpression _cachedExpression;
private readonly IMapperCollection _mapperCollection;
private readonly ISqlSyntaxProvider _syntaxProvider =
new SqlServerSyntaxProvider(Options.Create(new GlobalSettings()));
public ModelToSqlExpressionHelperBenchmarks()
{
var contentMapper = new ContentMapper(MockSqlContext(), CreateMaps());
_cachedExpression = new CachedExpression();
var mapperCollection = new Mock<IMapperCollection>();
mapperCollection.Setup(x => x[It.IsAny<Type>()]).Returns(contentMapper);
_mapperCollection = mapperCollection.Object;
}
protected Lazy<ISqlContext> MockSqlContext()
{
var sqlContext = Mock.Of<ISqlContext>();
var syntax = new SqlServerSyntaxProvider(Options.Create(new GlobalSettings()));
Mock.Get(sqlContext).Setup(x => x.SqlSyntax).Returns(syntax);
return new Lazy<ISqlContext>(() => sqlContext);
}
protected MapperConfigurationStore CreateMaps() => new();
[Benchmark(Baseline = true)]
public void WithNonCached()
{
for (var 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);
var modelToSqlExpressionHelper =
new ModelToSqlExpressionVisitor<IContent>(_syntaxProvider, _mapperCollection);
var result = modelToSqlExpressionHelper.Visit(predicate);
}
}
[Benchmark]
public void WithCachedExpression()
{
for (var 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);
var modelToSqlExpressionHelper =
new ModelToSqlExpressionVisitor<IContent>(_syntaxProvider, _mapperCollection);
//wrap it!
_cachedExpression.Wrap(predicate);
var result = modelToSqlExpressionHelper.Visit(_cachedExpression);
}
}
}