Performance test for AutoMapper #3781
Replies: 3 comments 3 replies
-
Only Mapper.Map should be in the benchmark. Everything else is one-time setup code, for both libraries.
… On Nov 22, 2021, at 8:33 PM, hippie ***@***.***> wrote:
when I use AutoMapper to map my entities, I'm having performance question.so I'm going to use BenchmarkDotNet with console app for a pressure measurement.The results did not live up to expectations.what should i do?
Env
OS:Windows 10.0.19042.1348 (20H2/October2020Update)
CPU:Intel Core i5-7500 CPU 3.40GHz (Kaby Lake), 1 CPU, 4 logical and 4 physical cores
SDK:NET SDK=6.0.100
Packages
BenchmarkDotNet --version 0.13.1
AutoMapper --version 10.1.1
Mapster --version 7.2.0
Source/destination types
public enum MyEnum
{
[Description("进行中")]
Doing,
[Description("完成")]
Done
}
// source types
public class Entity
{
public int Id { get; set; }
public Guid Oid { get; set; }
public string? NickName { get; set; }
public bool Created { get; set; }
public MyEnum State { get; set; }
}
// destination type
public class EntityDto
{
public int Id { get; set; }
public Guid Oid { get; set; }
public string? NickName { get; set; }
public bool Created { get; set; }
public MyEnum Enum { get; set; }
public string? EnumString { get; set; }
}
Mapping configuration
// AutoMapper profile
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Entity, EntityDto>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.Oid, opt => opt.MapFrom(src => src.Oid))
.ForMember(dest => dest.NickName, opt => opt.MapFrom(src => src.NickName))
.ForMember(dest => dest.Created, opt => opt.MapFrom(src => src.Created))
.ForMember(dest => dest.Enum, opt => opt.MapFrom(src => src.State))
.ForMember(dest => dest.EnumString, opt => opt.MapFrom(src => src.State.GetDescription()));
}
}
// Mapster profile
public class MapsterProfile : TypeAdapterConfig
{
public MapsterProfile()
{
ForType<Entity, EntityDto>()
.Map(dest => dest.Id, src => src.Id)
.Map(dest => dest.Oid, src => src.Oid)
.Map(dest => dest.NickName, src => src.NickName)
.Map(dest => dest.Created, src => src.Created)
.Map(dest => dest.Enum, src => src.State)
.Map(dest => dest.EnumString, src => src.State.GetDescription());
}
}
Test Code
_entities = Enumerable.Range(0, 1000000).Select(x => mocker.CreateInstance<Entity>()).ToList();
[Benchmark]
public void Constructor()
{
var dtos = _entities.Select(x => new EntityDto
{
Id = x.Id,
Oid = x.Oid,
NickName = x.NickName,
Created = x.Created,
Enum = x.State,
EnumString = x.State.GetDescription(),
});
}
[Benchmark]
public void AutoMapper()
{
var conf = new AutoMapper.MapperConfiguration(cfg => cfg.AddProfile<AutoMapperProfile>());
var mapper = conf.CreateMapper();
var dtos = mapper.Map<IEnumerable<EntityDto>>(_entities);
}
[Benchmark]
public void Mapster()
{
var conf = new MapsterMapper.Mapper(new MapsterProfile());
var dtos = conf.Map<IEnumerable<EntityDto>>(_entities);
}
Actual behavior
How I should improve performance ?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
The first four |
Beta Was this translation helpful? Give feedback.
-
Did you use the MyGet build as I requested? Your test probably just reflects compilation time, not the mapping itself. See #3753 for example. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
when I use
AutoMapper
to map my entities, I'm having performance question.so I'm going to useBenchmarkDotNet
with console app for a pressure measurement.The results did not live up to expectations.what should i do?Env
Packages
Source/destination types
Mapping configuration
Test Code
Actual behavior
How I should improve performance ?
Beta Was this translation helpful? Give feedback.
All reactions