Skip to content

Commit 694a216

Browse files
author
Dmitry Panyushkin
committed
Added manual mapping to benchmarks
1 parent c1e72f0 commit 694a216

5 files changed

Lines changed: 187 additions & 36 deletions

File tree

src/AutoMapper.ExtendedConverters.Benchmarks/AutoMapper.ExtendedConverters.Benchmarks.csproj

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,13 @@
4141
<HintPath>..\packages\BenchmarkDotNet.0.9.3\lib\net45\BenchmarkDotNet.dll</HintPath>
4242
<Private>True</Private>
4343
</Reference>
44-
<Reference Include="Jil, Version=2.13.0.0, Culture=neutral, processorArchitecture=MSIL">
45-
<HintPath>..\packages\Jil.2.13.0\lib\net45\Jil.dll</HintPath>
46-
<Private>True</Private>
47-
</Reference>
4844
<Reference Include="Microsoft.Build" />
4945
<Reference Include="Microsoft.Build.Framework" />
5046
<Reference Include="Microsoft.Build.Utilities.v4.0" />
5147
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
5248
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
5349
<Private>True</Private>
5450
</Reference>
55-
<Reference Include="Sigil, Version=4.5.0.0, Culture=neutral, PublicKeyToken=2d06c3494341c8ab, processorArchitecture=MSIL">
56-
<HintPath>..\packages\Sigil.4.5.0\lib\net45\Sigil.dll</HintPath>
57-
<Private>True</Private>
58-
</Reference>
5951
<Reference Include="System" />
6052
<Reference Include="System.Core" />
6153
<Reference Include="Microsoft.CSharp" />
@@ -64,6 +56,7 @@
6456
</ItemGroup>
6557
<ItemGroup>
6658
<Compile Include="Benchmark.cs" />
59+
<Compile Include="ManualMapper.cs" />
6760
<Compile Include="Program.cs" />
6861
<Compile Include="Properties\AssemblyInfo.cs" />
6962
<Compile Include="SampleBuilder.cs" />

src/AutoMapper.ExtendedConverters.Benchmarks/Benchmark.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ namespace AutoMapper.ExtendedConverters.Benchmarks
77

88
public class Benchmark
99
{
10-
public static readonly object Data = SampleBuilder.DepartmentsAggregate();
10+
public static readonly object Data = SampleBuilder.DepartmentAggregate();
1111

1212
private readonly IMapper AutoMapper;
1313
private readonly IMapper AutoMapperCompiledConverter;
1414
private readonly IMapper AutoMapperCompiledAndListConverter;
15+
private readonly ManualMapper ManualMapper;
1516

1617
public Benchmark()
1718
{
@@ -62,56 +63,51 @@ public Benchmark()
6263
});
6364
config.AssertConfigurationIsValid();
6465
AutoMapperCompiledAndListConverter = config.CreateMapper();
66+
67+
ManualMapper = new ManualMapper();
6568
}
6669

6770
[Benchmark(Description = "Building sample data")]
6871
public object BuildSampleDataTest()
6972
{
70-
return SampleBuilder.DepartmentsAggregate();
73+
return SampleBuilder.DepartmentAggregate();
74+
}
75+
76+
[Benchmark(Description = "Manual Mapping")]
77+
public object ManualMapperTest()
78+
{
79+
return ManualMapper.Map((Department)Data);
7180
}
7281

7382
[Benchmark(Description = "Vanilla AutoMapper")]
7483
public object AutoMapperTest()
7584
{
76-
return AutoMapper.Map<List<Department>>(Data);
85+
return AutoMapper.Map<Department>(Data);
7786
}
7887

7988
[Benchmark(Description = "AutoMapper + CompiledConverter")]
8089
public object AutoMapperCompiledConverterTest()
8190
{
82-
return AutoMapperCompiledConverter.Map<List<Department>>(Data);
91+
return AutoMapperCompiledConverter.Map<Department>(Data);
8392
}
8493

8594
[Benchmark(Description = "AutoMapper + CompiledConverter + ListConverter")]
8695
public object AutoMapperCompiledAndListConverterTest()
8796
{
88-
return AutoMapperCompiledAndListConverter.Map<List<Department>>(Data);
97+
return AutoMapperCompiledAndListConverter.Map<Department>(Data);
8998
}
9099

91100
[Benchmark(Description = "Serialization of sample data by Json.NET")]
92-
public object NewtonsoftJsonSerializationTest()
101+
public string NewtonsoftJsonSerializationTest()
93102
{
94103
return Newtonsoft.Json.JsonConvert.SerializeObject(Data);
95104
}
96-
97-
[Benchmark(Description = "Serialization of sample data by Jil serializer")]
98-
public object JilSerializationTest()
99-
{
100-
return Jil.JSON.Serialize(Data);
101-
}
102-
105+
103106
[Benchmark(Description = "Serialization and deserialization of sample data by Json.NET")]
104107
public object NewtonsoftJsonCloningTest()
105108
{
106109
string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(Data);
107-
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<Department>>(serialized);
108-
}
109-
110-
[Benchmark(Description = "Serialization and deserialization of sample data by Jil serializer")]
111-
public object JilCloningTest()
112-
{
113-
string serialized = Jil.JSON.Serialize(Data);
114-
return Jil.JSON.Deserialize<List<Department>>(serialized);
110+
return Newtonsoft.Json.JsonConvert.DeserializeObject<Department>(serialized);
115111
}
116112
}
117113
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace AutoMapper.ExtendedConverters.Benchmarks
5+
{
6+
using SampleClasses;
7+
8+
public class ManualMapper
9+
{
10+
public Address Map(Address src, Address dest = null)
11+
{
12+
if (src == null) { return null; }
13+
if (dest == null) { dest = new Address(); }
14+
dest.Id = src.Id;
15+
dest.ZipCode = src.ZipCode;
16+
dest.Country = src.Country;
17+
dest.State = src.State;
18+
dest.City = src.City;
19+
dest.Street = src.Street;
20+
dest.Building = src.Building;
21+
dest.Latitude = src.Latitude;
22+
dest.Longitude = src.Longitude;
23+
return dest;
24+
}
25+
26+
public Customer Map(Customer src, Customer dest = null)
27+
{
28+
if (src == null) { return null; }
29+
if (dest == null) { dest = new Customer(); }
30+
dest.Id = src.Id;
31+
dest.FirstName = src.FirstName;
32+
dest.LastName = src.LastName;
33+
dest.Email = src.Email;
34+
dest.Phones = Map(src.Phones);
35+
dest.RegistrationDate = src.RegistrationDate;
36+
dest.Addresses = Map(src.Addresses);
37+
dest.Orders = Map(src.Orders);
38+
return dest;
39+
}
40+
41+
public Department Map(Department src, Department dest = null)
42+
{
43+
if (src == null) { return null; }
44+
if (dest == null) { dest = new Department(); }
45+
dest.Id = src.Id;
46+
dest.Name = src.Name;
47+
dest.Email = src.Email;
48+
dest.Phones = Map(src.Phones);
49+
dest.Address = Map(src.Address);
50+
dest.Director = Map(src.Director);
51+
dest.Salesmans = Map(src.Salesmans);
52+
return dest;
53+
}
54+
55+
public Employee Map(Employee src, Employee dest = null)
56+
{
57+
if (src == null) { return null; }
58+
if (dest == null) { dest = new Employee(); }
59+
dest.Id = src.Id;
60+
dest.FirstName = src.FirstName;
61+
dest.LastName = src.LastName;
62+
dest.Email = src.Email;
63+
dest.Phones = Map(src.Phones);
64+
dest.DepartmentId = src.DepartmentId;
65+
dest.Department = Map(src.Department);
66+
dest.Position = src.Position;
67+
dest.Address = Map(src.Address);
68+
dest.Orders = Map(src.Orders);
69+
return dest;
70+
}
71+
72+
public Order Map(Order src, Order dest = null)
73+
{
74+
if (src == null) { return null; }
75+
if (dest == null) { dest = new Order(); }
76+
dest.Id = src.Id;
77+
dest.CustomerId = src.CustomerId;
78+
dest.Customer = Map(src.Customer);
79+
dest.EmployeeId = src.EmployeeId;
80+
dest.Employee = Map(src.Employee);
81+
dest.ProductId = src.ProductId;
82+
dest.Product = Map(src.Product);
83+
dest.Amount = src.Amount;
84+
dest.CreationDate = src.CreationDate;
85+
dest.IsShipped = src.IsShipped;
86+
dest.ShippingDate = src.ShippingDate;
87+
dest.ShippingAddress = Map(src.ShippingAddress);
88+
return dest;
89+
}
90+
91+
public Product Map(Product src, Product dest = null)
92+
{
93+
if (src == null) { return null; }
94+
if (dest == null) { dest = new Product(); }
95+
dest.Id = src.Id;
96+
dest.ProductCategoryId = src.ProductCategoryId;
97+
dest.ProductCategory = Map(src.ProductCategory);
98+
dest.Name = src.Name;
99+
dest.ShortDescription = src.ShortDescription;
100+
dest.Description = src.Description;
101+
dest.Location = src.Location;
102+
dest.Weight = src.Weight;
103+
dest.Price = src.Price;
104+
dest.Count = src.Count;
105+
dest.Rating = src.Rating;
106+
return dest;
107+
}
108+
109+
public ProductCategory Map(ProductCategory src, ProductCategory dest = null)
110+
{
111+
if (src == null) { return null; }
112+
if (dest == null) { dest = new ProductCategory(); }
113+
dest.Id = src.Id;
114+
dest.Name = src.Name;
115+
dest.Products = src.Products.Select(x => Map(x)).ToList();
116+
return dest;
117+
}
118+
119+
public List<string> Map(List<string> src)
120+
{
121+
return src == null ? null : src.ToList();
122+
}
123+
124+
public List<T> Map<T>(List<T> src)
125+
where T : struct
126+
{
127+
return src == null ? null : src.ToList();
128+
}
129+
130+
public List<Address> Map(List<Address> src)
131+
{
132+
return src == null ? null : src.Select(x => Map(x)).ToList();
133+
}
134+
135+
public List<Customer> Map(List<Customer> src)
136+
{
137+
return src == null ? null : src.Select(x => Map(x)).ToList();
138+
}
139+
140+
public List<Department> Map(List<Department> src)
141+
{
142+
return src == null ? null : src.Select(x => Map(x)).ToList();
143+
}
144+
145+
public List<Employee> Map(List<Employee> src)
146+
{
147+
return src == null ? null : src.Select(x => Map(x)).ToList();
148+
}
149+
150+
public List<Order> Map(List<Order> src)
151+
{
152+
return src == null ? null : src.Select(x => Map(x)).ToList();
153+
}
154+
155+
public List<Product> Map(List<Product> src)
156+
{
157+
return src == null ? null : src.Select(x => Map(x)).ToList();
158+
}
159+
160+
public List<ProductCategory> Map(List<ProductCategory> src)
161+
{
162+
return src == null ? null : src.Select(x => Map(x)).ToList();
163+
}
164+
}
165+
}

src/AutoMapper.ExtendedConverters.Benchmarks/SampleBuilder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ public static List<string> RandomStringList(int count, int stringSize, bool digi
6060

6161
#region Aggregates
6262

63-
public static List<Department> DepartmentsAggregate()
63+
public static Department DepartmentAggregate()
6464
{
65-
List<Department> departments = RandomList(5, Department.Create);
66-
List<Order> orders = RandomList(100, Order.Create);
65+
Department department = Department.Create();
66+
List<Order> orders = RandomList(300, Order.Create);
6767

6868
foreach (Order order in orders) {
69-
Department department = departments.Choice();
7069
Employee employee = department.Salesmans.Choice();
7170
employee.Orders = employee.Orders ?? new List<Order>();
7271
employee.Orders.Add(order);
@@ -79,7 +78,7 @@ public static List<Department> DepartmentsAggregate()
7978
order.Product = product;
8079
}
8180

82-
return departments;
81+
return department;
8382
}
8483

8584
#endregion

src/AutoMapper.ExtendedConverters.Benchmarks/packages.config

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22
<packages>
33
<package id="AutoMapper" version="4.2.1" targetFramework="net452" />
44
<package id="BenchmarkDotNet" version="0.9.3" targetFramework="net452" />
5-
<package id="Jil" version="2.13.0" targetFramework="net452" />
65
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net452" />
7-
<package id="Sigil" version="4.5.0" targetFramework="net452" />
86
</packages>

0 commit comments

Comments
 (0)