Skip to content

Commit a745416

Browse files
committed
Testing temp config
1 parent bf15fa4 commit a745416

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

src/Benchmark.Development/Benchmarks/TestAll.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class TestAll
88
private Foo _fooInstance;
99
private Customer _customerInstance;
1010

11-
[Params(100_000)]//, 1_000_000)]
11+
[Params(100)]//, 1_000_000)]
1212
public int Iterations { get; set; }
1313

1414
[Benchmark]
@@ -18,6 +18,14 @@ public void MapsterTest()
1818
TestAdaptHelper.TestMapsterAdapter<Customer, CustomerDTO>(_customerInstance, Iterations);
1919
}
2020

21+
[Benchmark]
22+
public void MapsterLocalConfig()
23+
{
24+
TestAdaptHelperLocal.TestMapsterAdapter<Foo, Foo>(_fooInstance, Iterations);
25+
TestAdaptHelperLocal.TestMapsterAdapter<Customer, CustomerDTO>(_customerInstance, Iterations);
26+
}
27+
28+
2129
[GlobalSetup(Target = nameof(MapsterTest))]
2230
public void SetupMapster()
2331
{
@@ -26,5 +34,14 @@ public void SetupMapster()
2634
TestAdaptHelper.ConfigureMapster(_fooInstance, MapsterCompilerType.Default);
2735
TestAdaptHelper.ConfigureMapster(_customerInstance, MapsterCompilerType.Default);
2836
}
37+
38+
[GlobalSetup(Target = nameof(MapsterLocalConfig))]
39+
public void SetupMapsterLocal()
40+
{
41+
_fooInstance = TestAdaptHelperLocal.SetupFooInstance();
42+
_customerInstance = TestAdaptHelperLocal.SetupCustomerInstance();
43+
TestAdaptHelperLocal.ConfigureMapster(_fooInstance, MapsterCompilerType.Default);
44+
TestAdaptHelperLocal.ConfigureMapster(_customerInstance, MapsterCompilerType.Default);
45+
}
2946
}
3047
}

src/Benchmark.Development/TestAdaptHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ private static void Loop<T>(T item, Action<T> action, int iterations)
9494
{
9595
for (var i = 0; i < iterations; i++) action(item);
9696
}
97+
98+
99+
100+
101+
97102
}
98103

99104
public enum MapsterCompilerType
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using Benchmark.Classes;
2+
using Mapster;
3+
using System.Linq.Expressions;
4+
5+
namespace Benchmark
6+
{
7+
public static class TestAdaptHelperLocal
8+
{
9+
10+
public static Customer SetupCustomerInstance()
11+
{
12+
return new Customer
13+
{
14+
Address = new Address { City = "istanbul", Country = "turkey", Id = 1, Street = "istiklal cad." },
15+
HomeAddress = new Address { City = "istanbul", Country = "turkey", Id = 2, Street = "istiklal cad." },
16+
Id = 1,
17+
Name = "Eduardo Najera",
18+
Credit = 234.7m,
19+
WorkAddresses = new List<Address>
20+
{
21+
new Address {City = "istanbul", Country = "turkey", Id = 5, Street = "istiklal cad."},
22+
new Address {City = "izmir", Country = "turkey", Id = 6, Street = "konak"}
23+
},
24+
Addresses = new[]
25+
{
26+
new Address {City = "istanbul", Country = "turkey", Id = 3, Street = "istiklal cad."},
27+
new Address {City = "izmir", Country = "turkey", Id = 4, Street = "konak"}
28+
}
29+
};
30+
}
31+
32+
public static Foo SetupFooInstance()
33+
{
34+
return new Foo
35+
{
36+
Name = "foo",
37+
Int32 = 12,
38+
Int64 = 123123,
39+
NullInt = 16,
40+
DateTime = DateTime.Now,
41+
Doublen = 2312112,
42+
Foo1 = new Foo { Name = "foo one" },
43+
Foos = new List<Foo>
44+
{
45+
new Foo {Name = "j1", Int64 = 123, NullInt = 321},
46+
new Foo {Name = "j2", Int32 = 12345, NullInt = 54321},
47+
new Foo {Name = "j3", Int32 = 12345, NullInt = 54321}
48+
},
49+
FooArr = new[]
50+
{
51+
new Foo {Name = "a1"},
52+
new Foo {Name = "a2"},
53+
new Foo {Name = "a3"}
54+
},
55+
IntArr = new[] { 1, 2, 3, 4, 5 },
56+
Ints = new[] { 7, 8, 9 }
57+
};
58+
}
59+
60+
private static readonly Func<LambdaExpression, Delegate> _defaultCompiler = TypeAdapterConfig.GlobalSettings.Compiler;
61+
62+
private static void SetupCompiler(MapsterCompilerType type)
63+
{
64+
TypeAdapterConfig.GlobalSettings.Compiler = type switch
65+
{
66+
MapsterCompilerType.Default => _defaultCompiler,
67+
// MapsterCompilerType.Roslyn => exp => exp.CompileWithDebugInfo(),
68+
// MapsterCompilerType.FEC => exp => exp.CompileFast(),
69+
_ => throw new ArgumentOutOfRangeException(nameof(type)),
70+
};
71+
}
72+
public static void ConfigureMapster(Foo fooInstance, MapsterCompilerType type)
73+
{
74+
SetupCompiler(type);
75+
TypeAdapterConfig.GlobalSettings.Compile(typeof(Foo), typeof(Foo)); //recompile
76+
fooInstance.Adapt<Foo, Foo>(); //exercise
77+
}
78+
79+
public static void ConfigureMapster(Customer customerInstance, MapsterCompilerType type)
80+
{
81+
SetupCompiler(type);
82+
TypeAdapterConfig.GlobalSettings.Compile(typeof(Customer), typeof(CustomerDTO)); //recompile
83+
customerInstance.Adapt<Customer, CustomerDTO>(); //exercise
84+
}
85+
86+
public static void TestMapsterAdapter<TSrc, TDest>(TSrc item, int iterations)
87+
where TSrc : class
88+
where TDest : class, new()
89+
{
90+
Loop(item, get => get.Adapt<TSrc, TDest>(setter =>
91+
{
92+
setter.Ignore("Hello");
93+
}),
94+
95+
iterations);
96+
97+
}
98+
99+
private static TDestination Adapt<TSource, TDestination>(this TSource source, Action<TypeAdapterSetter<TSource, TDestination>> configAction)
100+
{
101+
var config = TypeAdapterConfig.GlobalSettings.Clone();
102+
var setter = config.ForType<TSource, TDestination>();
103+
configAction(setter);
104+
setter.Settings.Resolvers.Reverse();
105+
return source.Adapt<TSource,TDestination>(config);
106+
}
107+
108+
109+
110+
private static void Loop<T>(T item, Action<T> action, int iterations)
111+
{
112+
for (var i = 0; i < iterations; i++) action(item);
113+
}
114+
115+
116+
}
117+
}

0 commit comments

Comments
 (0)