Skip to content

Commit

Permalink
add more Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Jan 20, 2020
1 parent 6cf545c commit 2591627
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
<PackageReference Include="ExpressionDebugger" Version="2.1.1" />
<PackageReference Include="ExpressionTranslator" Version="2.1.0" />
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Expressmapper" Version="1.9.1" />
Expand Down
70 changes: 65 additions & 5 deletions src/Benchmark/Benchmarks/TestAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class TestAll
private Foo fooInstance;
private Customer customerInstance;

[Params(1000, 10_000, 100_000, 1_000_000)]
[Params(1000, 10_000, 100_000)]//, 1_000_000)]
public int Iterations { get; set; }

[Benchmark(Description = "Mapster 4.1.1")]
Expand All @@ -18,6 +18,20 @@ public void MapsterTest()
TestAdaptHelper.TestMapsterAdapter<Customer, CustomerDTO>(customerInstance, Iterations);
}

[Benchmark(Description = "Mapster 4.1.1 (Roslyn)")]
public void RoslynTest()
{
TestAdaptHelper.TestMapsterAdapter<Foo, Foo>(fooInstance, Iterations);
TestAdaptHelper.TestMapsterAdapter<Customer, CustomerDTO>(customerInstance, Iterations);
}

[Benchmark(Description = "Mapster 4.1.1 (FEC)")]
public void FecTest()
{
TestAdaptHelper.TestMapsterAdapter<Foo, Foo>(fooInstance, Iterations);
TestAdaptHelper.TestMapsterAdapter<Customer, CustomerDTO>(customerInstance, Iterations);
}

[Benchmark(Description = "Mapster 4.1.1 (Codegen)")]
public void CodegenTest()
{
Expand All @@ -39,13 +53,59 @@ public void AutoMapperTest()
TestAdaptHelper.TestAutoMapper<Customer, CustomerDTO>(customerInstance, Iterations);
}

[GlobalSetup]
public void Setup()
[GlobalSetup(Target = nameof(MapsterTest))]
public void SetupMapster()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureMapster(fooInstance, MapsterCompilerType.Default);
TestAdaptHelper.ConfigureMapster(customerInstance, MapsterCompilerType.Default);
}

[GlobalSetup(Target = nameof(RoslynTest))]
public void SetupRoslyn()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureMapster(fooInstance, MapsterCompilerType.Roslyn);
TestAdaptHelper.ConfigureMapster(customerInstance, MapsterCompilerType.Roslyn);
}

[GlobalSetup(Target = nameof(FecTest))]
public void SetupFec()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureMapster(fooInstance, MapsterCompilerType.FEC);
TestAdaptHelper.ConfigureMapster(customerInstance, MapsterCompilerType.FEC);
}

[GlobalSetup(Target = nameof(CodegenTest))]
public void SetupCodegen()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
customerInstance = TestAdaptHelper.SetupCustomerInstance();
FooMapper.Map(fooInstance);
CustomerMapper.Map(customerInstance);
}

[GlobalSetup(Target = nameof(ExpressMapperTest))]
public void SetupExpressMapper()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
TestAdaptHelper.Configure(fooInstance);
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.Configure(customerInstance);
TestAdaptHelper.ConfigureExpressMapper(fooInstance);
TestAdaptHelper.ConfigureExpressMapper(customerInstance);
}

[GlobalSetup(Target = nameof(AutoMapperTest))]
public void SetupAutoMapper()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureAutoMapper(fooInstance);
TestAdaptHelper.ConfigureAutoMapper(customerInstance);
}

}
}
53 changes: 50 additions & 3 deletions src/Benchmark/Benchmarks/TestComplexTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public void MapsterTest()
{
TestAdaptHelper.TestMapsterAdapter<Customer, CustomerDTO>(customerInstance, Iterations);
}

[Benchmark(Description = "Mapster 4.1.1 (Roslyn)")]
public void RoslynTest()
{
TestAdaptHelper.TestMapsterAdapter<Customer, CustomerDTO>(customerInstance, Iterations);
}

[Benchmark(Description = "Mapster 4.1.1 (FEC)")]
public void FecTest()
{
TestAdaptHelper.TestMapsterAdapter<Customer, CustomerDTO>(customerInstance, Iterations);
}

[Benchmark]
public void CodegenTest()
Expand All @@ -34,11 +46,46 @@ public void AutoMapperTest()
TestAdaptHelper.TestAutoMapper<Customer, CustomerDTO>(customerInstance, Iterations);
}

[GlobalSetup]
public void Setup()
[GlobalSetup(Target = nameof(MapsterTest))]
public void SetupMapster()
{
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureMapster(customerInstance, MapsterCompilerType.Default);
}

[GlobalSetup(Target = nameof(RoslynTest))]
public void SetupRoslyn()
{
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureMapster(customerInstance, MapsterCompilerType.Roslyn);
}

[GlobalSetup(Target = nameof(FecTest))]
public void SetupFec()
{
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureMapster(customerInstance, MapsterCompilerType.FEC);
}

[GlobalSetup(Target = nameof(CodegenTest))]
public void SetupCodegen()
{
customerInstance = TestAdaptHelper.SetupCustomerInstance();
CustomerMapper.Map(customerInstance);
}

[GlobalSetup(Target = nameof(ExpressMapperTest))]
public void SetupExpressMapper()
{
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.ConfigureExpressMapper(customerInstance);
}

[GlobalSetup(Target = nameof(AutoMapperTest))]
public void SetupAutoMapper()
{
customerInstance = TestAdaptHelper.SetupCustomerInstance();
TestAdaptHelper.Configure(customerInstance);
TestAdaptHelper.ConfigureAutoMapper(customerInstance);
}
}
}
54 changes: 51 additions & 3 deletions src/Benchmark/Benchmarks/TestSimpleTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public void MapsterTest()
{
TestAdaptHelper.TestMapsterAdapter<Foo, Foo>(fooInstance, Iterations);
}

[Benchmark(Description = "Mapster 4.1.1 (Roslyn)")]
public void RoslynTest()
{
TestAdaptHelper.TestMapsterAdapter<Foo, Foo>(fooInstance, Iterations);
}

[Benchmark(Description = "Mapster 4.1.1 (FEC)")]
public void FecTest()
{
TestAdaptHelper.TestMapsterAdapter<Foo, Foo>(fooInstance, Iterations);
}

[Benchmark]
public void CodegenTest()
Expand All @@ -34,11 +46,47 @@ public void AutoMapperTest()
TestAdaptHelper.TestAutoMapper<Foo, Foo>(fooInstance, Iterations);
}

[GlobalSetup]
public void Setup()

[GlobalSetup(Target = nameof(MapsterTest))]
public void SetupMapster()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
TestAdaptHelper.ConfigureMapster(fooInstance, MapsterCompilerType.Default);
}

[GlobalSetup(Target = nameof(RoslynTest))]
public void SetupRoslyn()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
TestAdaptHelper.ConfigureMapster(fooInstance, MapsterCompilerType.Roslyn);
}

[GlobalSetup(Target = nameof(FecTest))]
public void SetupFec()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
TestAdaptHelper.ConfigureMapster(fooInstance, MapsterCompilerType.FEC);
}

[GlobalSetup(Target = nameof(CodegenTest))]
public void SetupCodegen()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
FooMapper.Map(fooInstance);
}

[GlobalSetup(Target = nameof(ExpressMapperTest))]
public void SetupExpressMapper()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
TestAdaptHelper.ConfigureExpressMapper(fooInstance);
}

[GlobalSetup(Target = nameof(AutoMapperTest))]
public void SetupAutoMapper()
{
fooInstance = TestAdaptHelper.SetupFooInstance();
TestAdaptHelper.Configure(fooInstance);
TestAdaptHelper.ConfigureAutoMapper(fooInstance);
}
}
}
51 changes: 45 additions & 6 deletions src/Benchmark/TestAdaptHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using AutoMapper;
using Benchmark.Classes;
using ExpressionDebugger;
using FastExpressionCompiler;
using Mapster;

namespace Benchmark
Expand Down Expand Up @@ -66,23 +69,52 @@ public static Foo SetupFooInstance()
};
}

public static void Configure(Foo fooInstance)
private static readonly Func<LambdaExpression, Delegate> _defaultCompiler = TypeAdapterConfig.GlobalSettings.Compiler;

private static void SetupCompiler(MapsterCompilerType type)
{
switch (type)
{
case MapsterCompilerType.Default:
TypeAdapterConfig.GlobalSettings.Compiler = _defaultCompiler;
break;
case MapsterCompilerType.Roslyn:
TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileWithDebugInfo();
break;
case MapsterCompilerType.FEC:
TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileFast();
break;
default:
throw new ArgumentOutOfRangeException(nameof(type));
}
}
public static void ConfigureMapster(Foo fooInstance, MapsterCompilerType type)
{
SetupCompiler(type);
TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
fooInstance.Adapt<Foo, Foo>(); //exercise

}
public static void ConfigureExpressMapper(Foo fooInstance)
{
ExpressMapper.Mapper.Map<Foo, Foo>(fooInstance); //exercise

}
public static void ConfigureAutoMapper(Foo fooInstance)
{
_mapper.Map<Foo, Foo>(fooInstance); //exercise
}

public static void Configure(Customer customerInstance)
public static void ConfigureMapster(Customer customerInstance, MapsterCompilerType type)
{
SetupCompiler(type);
TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
customerInstance.Adapt<Customer, CustomerDTO>(); //exercise

}
public static void ConfigureExpressMapper(Customer customerInstance)
{
ExpressMapper.Mapper.Map<Customer, CustomerDTO>(customerInstance); //exercise

}
public static void ConfigureAutoMapper(Customer customerInstance)
{
_mapper.Map<Customer, CustomerDTO>(customerInstance); //exercise
}

Expand Down Expand Up @@ -122,4 +154,11 @@ private static void Loop<T>(T item, Action<T> action, int iterations)
for (var i = 0; i < iterations; i++) action(item);
}
}

public enum MapsterCompilerType
{
Default,
Roslyn,
FEC,
}
}

0 comments on commit 2591627

Please sign in to comment.