Skip to content

Commit

Permalink
Performance fixes (#62)
Browse files Browse the repository at this point in the history
* Updated packages
* Streamlined build chain usage
* Updated DefaultExecuteStrategy.BuildChain to use IBuildChain instead of building a linked list on each call.
* Updated TimeZoneValueGenerator to reduce evaluations of test data.

+semver: breaking
  • Loading branch information
roryprimrose authored Feb 9, 2019
1 parent cce3b7a commit 3e18c2e
Show file tree
Hide file tree
Showing 167 changed files with 2,599 additions and 2,741 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="fluentassertions" Version="4.19.1" />
<PackageReference Include="nsubstitute" Version="2.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="fluentassertions" Version="5.6.0" />
<PackageReference Include="nsubstitute" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
74 changes: 25 additions & 49 deletions ModelBuilder.Synchronous.UnitTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void BuildStrategyThrowsExceptionWithNullStrategyTest()
{
Action action = () => Model.BuildStrategy = null;

action.ShouldThrow<ArgumentNullException>();
action.Should().Throw<ArgumentNullException>();
}

[Fact]
Expand Down Expand Up @@ -65,13 +65,10 @@ public void CreateTUsesBuildStrategyToCreateInstanceTest()

var build = Substitute.For<IBuildStrategy>();
var generator = Substitute.For<IValueGenerator>();
var generators = new List<IValueGenerator>
{
generator
}.AsReadOnly();
var generators = new List<IValueGenerator> {generator}.AsReadOnly();

build.ValueGenerators.Returns(generators);
generator.IsSupported(typeof(Guid), null, Arg.Any<LinkedList<object>>()).Returns(true);
generator.IsSupported(typeof(Guid), null, Arg.Any<IBuildChain>()).Returns(true);
generator.Generate(typeof(Guid), null, Arg.Any<IExecuteStrategy>()).Returns(value);

try
Expand All @@ -95,13 +92,10 @@ public void CreateUsesBuildStrategyToCreateInstanceTest()

var build = Substitute.For<IBuildStrategy>();
var generator = Substitute.For<IValueGenerator>();
var generators = new List<IValueGenerator>
{
generator
}.AsReadOnly();
var generators = new List<IValueGenerator> {generator}.AsReadOnly();

build.ValueGenerators.Returns(generators);
generator.IsSupported(typeof(Guid), null, Arg.Any<LinkedList<object>>()).Returns(true);
generator.IsSupported(typeof(Guid), null, Arg.Any<IBuildChain>()).Returns(true);
generator.Generate(typeof(Guid), null, Arg.Any<IExecuteStrategy>()).Returns(value);

try
Expand All @@ -119,28 +113,26 @@ public void CreateUsesBuildStrategyToCreateInstanceTest()
}

[Fact]
public void CreateWithUsesBuildStrategyToCreateInstanceWithParametersTest()
public void CreateWithTUsesBuildStrategyToCreateInstanceWithParametersTest()
{
var value = Guid.NewGuid();
var expected = new ReadOnlyModel(value);

var build = Substitute.For<IBuildStrategy>();
var creator = Substitute.For<ITypeCreator>();
var creators = new List<ITypeCreator>
{
creator
}.AsReadOnly();
var creators = new List<ITypeCreator> {creator}.AsReadOnly();

build.TypeCreators.Returns(creators);
creator.CanCreate(typeof(ReadOnlyModel), null, Arg.Any<LinkedList<object>>()).Returns(true);
creator.CanCreate(typeof(ReadOnlyModel), null, Arg.Any<IBuildChain>()).Returns(true);
creator.CanPopulate(typeof(ReadOnlyModel), null, Arg.Any<IBuildChain>()).Returns(true);
creator.Create(typeof(ReadOnlyModel), null, Arg.Any<IExecuteStrategy>(), value).Returns(expected);
creator.Populate(expected, Arg.Any<IExecuteStrategy>()).Returns(expected);

try
{
Model.BuildStrategy = build;

var actual = (ReadOnlyModel)Model.CreateWith(typeof(ReadOnlyModel), value);
var actual = Model.CreateWith<ReadOnlyModel>(value);

actual.Value.Should().Be(value);
}
Expand All @@ -151,29 +143,25 @@ public void CreateWithUsesBuildStrategyToCreateInstanceWithParametersTest()
}

[Fact]
public void CreateWithTUsesBuildStrategyToCreateInstanceWithParametersTest()
public void CreateWithUsesBuildStrategyToCreateInstanceWithParametersTest()
{
var value = Guid.NewGuid();
var expected = new ReadOnlyModel(value);

var build = Substitute.For<IBuildStrategy>();
var creator = Substitute.For<ITypeCreator>();
var creators = new List<ITypeCreator>
{
creator
}.AsReadOnly();
var creators = new List<ITypeCreator> {creator}.AsReadOnly();

build.TypeCreators.Returns(creators);
creator.CanCreate(typeof(ReadOnlyModel), null, Arg.Any<LinkedList<object>>()).Returns(true);
creator.CanPopulate(typeof(ReadOnlyModel), null, Arg.Any<LinkedList<object>>()).Returns(true);
creator.CanCreate(typeof(ReadOnlyModel), null, Arg.Any<IBuildChain>()).Returns(true);
creator.Create(typeof(ReadOnlyModel), null, Arg.Any<IExecuteStrategy>(), value).Returns(expected);
creator.Populate(expected, Arg.Any<IExecuteStrategy>()).Returns(expected);

try
{
Model.BuildStrategy = build;

var actual = Model.CreateWith<ReadOnlyModel>(value);
var actual = (ReadOnlyModel) Model.CreateWith(typeof(ReadOnlyModel), value);

actual.Value.Should().Be(value);
}
Expand All @@ -188,15 +176,9 @@ public void ForReturnsDefaultExecuteStrategyWithDefaultBuildStrategyConfiguratio
{
var build = Substitute.For<IBuildStrategy>();
var generator = Substitute.For<IValueGenerator>();
var generators = new List<IValueGenerator>
{
generator
}.AsReadOnly();
var generators = new List<IValueGenerator> {generator}.AsReadOnly();
var creator = Substitute.For<ITypeCreator>();
var creators = new List<ITypeCreator>
{
creator
}.AsReadOnly();
var creators = new List<ITypeCreator> {creator}.AsReadOnly();
var ignoreRules = new List<IgnoreRule>().AsReadOnly();
var resolver = Substitute.For<IConstructorResolver>();

Expand Down Expand Up @@ -229,29 +211,23 @@ public void PopulateUsesBuildStrategyToPopulateInstanceTest()
var creator = Substitute.For<ITypeCreator>();
var propertyResolver = Substitute.For<IPropertyResolver>();

var creators = new List<ITypeCreator>
{
creator
}.AsReadOnly();
var creators = new List<ITypeCreator> {creator}.AsReadOnly();
var generator = Substitute.For<IValueGenerator>();
var generators = new List<IValueGenerator>
{
generator
}.AsReadOnly();
var generators = new List<IValueGenerator> {generator}.AsReadOnly();

build.PropertyResolver.Returns(propertyResolver);
build.TypeCreators.Returns(creators);
build.ValueGenerators.Returns(generators);
propertyResolver.CanPopulate(Arg.Any<PropertyInfo>()).Returns(true);
propertyResolver.ShouldPopulateProperty(
Arg.Any<IBuildConfiguration>(),
Arg.Any<object>(),
Arg.Any<PropertyInfo>(),
Arg.Any<object[]>()).Returns(true);
creator.CanPopulate(typeof(SlimModel), null, Arg.Any<LinkedList<object>>()).Returns(true);
propertyResolver.ShouldPopulateProperty(Arg.Any<IBuildConfiguration>(),
Arg.Any<object>(),
Arg.Any<PropertyInfo>(),
Arg.Any<object[]>())
.Returns(true);
creator.CanPopulate(typeof(SlimModel), null, Arg.Any<IBuildChain>()).Returns(true);
creator.Populate(expected, Arg.Any<IExecuteStrategy>()).Returns(expected);
creator.AutoPopulate.Returns(true);
generator.IsSupported(typeof(Guid), "Value", Arg.Any<LinkedList<object>>()).Returns(true);
generator.IsSupported(typeof(Guid), "Value", Arg.Any<IBuildChain>()).Returns(true);
generator.Generate(typeof(Guid), "Value", Arg.Any<IExecuteStrategy>()).Returns(value);

try
Expand Down
21 changes: 10 additions & 11 deletions ModelBuilder.UnitTests/AddressValueGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace ModelBuilder.UnitTests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentAssertions;
Expand All @@ -20,22 +19,22 @@ public class AddressValueGeneratorTests
[InlineData("addressline5")]
public void GenerateReturnsNullForAddressLinesBeyondSecondTest(string referenceName)
{
var buildChain = new LinkedList<object>();
var buildChain = new BuildHistory();
var executeStrategy = Substitute.For<IExecuteStrategy>();

executeStrategy.BuildChain.Returns(buildChain);

var target = new AddressValueGenerator();

var actual = (string)target.Generate(typeof(string), referenceName, executeStrategy);
var actual = (string) target.Generate(typeof(string), referenceName, executeStrategy);

actual.Should().BeNullOrEmpty();
}

[Fact]
public void GenerateReturnsRandomAddressTest()
{
var buildChain = new LinkedList<object>();
var buildChain = new BuildHistory();
var executeStrategy = Substitute.For<IExecuteStrategy>();

executeStrategy.BuildChain.Returns(buildChain);
Expand Down Expand Up @@ -72,7 +71,7 @@ public void GenerateReturnsRandomAddressTest()
[InlineData("Addressline2")]
public void GenerateReturnsStreetAddressTest(string propertyName)
{
var buildChain = new LinkedList<object>();
var buildChain = new BuildHistory();
var executeStrategy = Substitute.For<IExecuteStrategy>();

executeStrategy.BuildChain.Returns(buildChain);
Expand All @@ -98,7 +97,7 @@ public void GenerateReturnsStreetAddressTest(string propertyName)
[InlineData("Addressline1")]
public void GenerateReturnsUnitFloorLocationForSecondLineTest(string propertyName)
{
var buildChain = new LinkedList<object>();
var buildChain = new BuildHistory();
var executeStrategy = Substitute.For<IExecuteStrategy>();

executeStrategy.BuildChain.Returns(buildChain);
Expand Down Expand Up @@ -135,14 +134,14 @@ public void GenerateReturnsUnitFloorLocationForSecondLineTest(string propertyNam
[InlineData(typeof(string), "Addressline3", false)]
public void GenerateReturnsValuesForSeveralNameFormatsTest(Type type, string referenceName, bool expected)
{
var buildChain = new LinkedList<object>();
var buildChain = new BuildHistory();
var executeStrategy = Substitute.For<IExecuteStrategy>();

executeStrategy.BuildChain.Returns(buildChain);

var target = new AddressValueGenerator();

var actual = (string)target.Generate(type, referenceName, executeStrategy);
var actual = (string) target.Generate(type, referenceName, executeStrategy);

if (expected)
{
Expand All @@ -161,7 +160,7 @@ public void GenerateReturnsValuesForSeveralNameFormatsTest(Type type, string ref
[InlineData(typeof(string), "Stuff")]
public void GenerateThrowsExceptionWithInvalidParametersTest(Type type, string referenceName)
{
var buildChain = new LinkedList<object>();
var buildChain = new BuildHistory();
var executeStrategy = Substitute.For<IExecuteStrategy>();

executeStrategy.BuildChain.Returns(buildChain);
Expand All @@ -170,7 +169,7 @@ public void GenerateThrowsExceptionWithInvalidParametersTest(Type type, string r

Action action = () => target.Generate(type, referenceName, executeStrategy);

action.ShouldThrow<NotSupportedException>();
action.Should().Throw<NotSupportedException>();
}

[Fact]
Expand Down Expand Up @@ -226,7 +225,7 @@ public void IsSupportedThrowsExceptionWithNullTypeTest()

Action action = () => target.IsSupported(null, null, null);

action.ShouldThrow<ArgumentNullException>();
action.Should().Throw<ArgumentNullException>();
}
}
}
Loading

0 comments on commit 3e18c2e

Please sign in to comment.