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