java-performance-mapping-frameworks
Performance of Java Mapping Frameworks
1. Introduction
Creating large Java applications composed of multiple layers require using multiple models such as persistence model, domain model or so-called DTOs. Using multiple models for different application layers will require us to provide a way of mapping between beans.
Doing this manually can quickly create much boilerplate code and consume a lot of time. Luckily for us, there are multiple object mapping frameworks for Java.
In this tutorial we’re going to compare the performance of the most popular Java mapping frameworks.
2. Mapping Frameworks
Dozer is a mapping framework that uses recursion to copy data from one object to another. The framework is able not only to copy properties between the beans, but it can also automatically convert between different types.
To use the Dozer framework we need to add such dependency to our project:
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
More information about the usage of the Dozer framework can be found in this article.
The documentation of the framework can be found here.
2.2. Orika
The general principle of work of the Orika is similar to Dozer. The main difference between the two is the fact that Orika uses bytecode generation. This allows generating faster mappers with the minimal overhead.
To use it, we need to add such dependency to our project:
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.2</version>
</dependency>
More detailed information about the usage of the Orika can be found in this article.
The actual documentation of the framework can be found here.
2.3. MapStruct
MapStruct also has the ability to convert between different data types. More information on how to use it can be found in this article.
To add MapStruct to our project we need to include the following dependency :
<dependency>3
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.Final</version>
</dependency>
The documentation of the framework can be found here.
2.4. ModelMapper
ModelMapper is a framework that aims to simplify object mapping, by determining how objects map to each other basing on conventions. It provides type-safe and refactoring-safe API.
More information about the framework can be found in the documentation.
To include the ModelMapper to our project we need to add the following dependency:
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>1.1.0</version>
</dependency>
2.5. JMapper
JMapper is the mapping framework that aims to provide easy-to-use, high-performance mapping between Java Beans.
The framework aims to apply DRY principle using Annotations and relational mapping.
The framework allows for different ways of configuration: annotation-based, XML or API-based.
More information about the framework can be found in its documentation.
To include the JMapper in our project we need to add its dependency:
<dependency>
<groupId>com.googlecode.jmapper-framework</groupId>
<artifactId>jmapper-core</artifactId>
<version>1.6.0.1</version>
</dependency>
3. Testing Model
To be able to test mapping properly we need to have a source and target models. We’ve created two testing models.
First one is just a simple POJO with one String field, this allowed us to compare frameworks in simpler cases and check whether anything changes if we use more complicated beans.
The simple source model looks like below:
public class SourceCode {
String code;
// getter and setter
}
And its destination is quite similar:
public class DestinationCode {
String code;
// getter and setter
}
The real-life example of source bean looks like that:
public class SourceOrder {
private String orderFinishDate;
private PaymentType paymentType;
private Discount discount;
private DeliveryData deliveryData;
private User orderingUser;
private List<Product> orderedProducts;
private Shop offeringShop;
private int orderId;
private OrderStatus status;
private LocalDate orderDate;
// standard getters and setters
}
And the target class looks like below:
public class Order {
private User orderingUser;
private List<Product> orderedProducts;
private OrderStatus orderStatus;
private LocalDate orderDate;
private LocalDate orderFinishDate;
private PaymentType paymentType;
private Discount discount;
private int shopId;
private DeliveryData deliveryData;
private Shop offeringShop;
// standard getters and setters
}
The whole model structure can be found here.
4. Converters
To simplify the design of the testing setup, we’ve created the Converter interface which looks like below:
public interface Converter {
Order convert(SourceOrder sourceOrder);
DestinationCode convert(SourceCode sourceCode);
}
And all our custom mappers will implement this interface.
4.1. OrikaConverter
public class OrikaConverter implements Converter{
private MapperFacade mapperFacade;
public OrikaConverter() {
MapperFactory mapperFactory = new DefaultMapperFactory
.Builder().build();
mapperFactory.classMap(Order.class, SourceOrder.class)
.field("orderStatus", "status").byDefault().register();
mapperFacade = mapperFactory.getMapperFacade();
}
@Override
public Order convert(SourceOrder sourceOrder) {
return mapperFacade.map(sourceOrder, Order.class);
}
@Override
public DestinationCode convert(SourceCode sourceCode) {
return mapperFacade.map(sourceCode, DestinationCode.class);
}
}
4.2. DozerConverter
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<mapping>
<class-a>com.baeldung.performancetests.model.source.SourceOrder</class-a>
<class-b>com.baeldung.performancetests.model.destination.Order</class-b>
<field>
<a>status</a>
<b>orderStatus</b>
</field>
</mapping>
<mapping>
<class-a>com.baeldung.performancetests.model.source.SourceCode</class-a>
<class-b>com.baeldung.performancetests.model.destination.DestinationCode</class-b>
</mapping>
</mappings>
After defining the XML mapping, we can use it from code:
public class DozerConverter implements Converter {
private final Mapper mapper;
public DozerConverter() {
DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(
DozerConverter.class.getResourceAsStream("/dozer-mapping.xml"));
this.mapper = mapper;
}
@Override
public Order convert(SourceOrder sourceOrder) {
return mapper.map(sourceOrder,Order.class);
}
@Override
public DestinationCode convert(SourceCode sourceCode) {
return mapper.map(sourceCode, DestinationCode.class);
}
}
4.3. MapStructConverter
@Mapper
public interface MapStructConverter extends Converter {
MapStructConverter MAPPER = Mappers.getMapper(MapStructConverter.class);
@Mapping(source = "status", target = "orderStatus")
@Override
Order convert(SourceOrder sourceOrder);
@Override
DestinationCode convert(SourceCode sourceCode);
}
4.4. JMapperConverter
public class JMapperConverter implements Converter {
JMapper realLifeMapper;
JMapper simpleMapper;
public JMapperConverter() {
JMapperAPI api = new JMapperAPI()
.add(JMapperAPI.mappedClass(Order.class));
realLifeMapper = new JMapper(Order.class, SourceOrder.class, api);
JMapperAPI simpleApi = new JMapperAPI()
.add(JMapperAPI.mappedClass(DestinationCode.class));
simpleMapper = new JMapper(
DestinationCode.class, SourceCode.class, simpleApi);
}
@Override
public Order convert(SourceOrder sourceOrder) {
return (Order) realLifeMapper.getDestination(sourceOrder);
}
@Override
public DestinationCode convert(SourceCode sourceCode) {
return (DestinationCode) simpleMapper.getDestination(sourceCode);
}
}
We also need to add @JMap annotations to each field of the target class. Also, JMapper can’t convert between enum types on its own and it requires us to create custom mapping functions :
@JMapConversion(from = "paymentType", to = "paymentType")
public PaymentType conversion(com.baeldung.performancetests.model.source.PaymentType type) {
PaymentType paymentType = null;
switch(type) {
case CARD:
paymentType = PaymentType.CARD;
break;
case CASH:
paymentType = PaymentType.CASH;
break;
case TRANSFER:
paymentType = PaymentType.TRANSFER;
break;
}
return paymentType;
}
4.5. ModelMapperConverter
public class ModelMapperConverter implements Converter {
private ModelMapper modelMapper;
public ModelMapperConverter() {
modelMapper = new ModelMapper();
}
@Override
public Order convert(SourceOrder sourceOrder) {
return modelMapper.map(sourceOrder, Order.class);
}
@Override
public DestinationCode convert(SourceCode sourceCode) {
return modelMapper.map(sourceCode, DestinationCode.class);
}
}
5. Simple Model Testing
For the performance testing, we can use Java Microbenchmark Harness, more information about how to use it can be found in this article.
We’ve created a separate benchmark for each Converter with specifying BenchmarkMode to Mode.All.
5.1. AverageTime
This benchmark shows clearly that both MapStruct and JMapper have the best average working times.
5.2. Throughput
In this mode, the benchmark returns the number of operations per second. We have received the following results (more is better) :
In throughput mode, MapStruct was the fastest of the tested frameworks, with JMapper a close second.
5.3. SingleShotTime
This mode allows measuring the time of single operation from it’s beginning to the end. The benchmark gave the following result (less is better):
Here, we see that JMapper returns a significantly better result than MapStruct.
5.4. SampleTime
This mode allows sampling of the time of each operation. The results for three different percentiles look like below:
All benchmarks have shown that MapStruct and JMapper are both good choices depending on the scenario, although MapStruct gave significantly worse results for SingleShotTime.
6. Real-Life Model Testing
For the performance testing, we can use Java Microbenchmark Harness, more information about how to use it can be found in this article.
We have created a separate benchmark for each Converter with specifying BenchmarkMode to Mode.All.
6.2. Throughput
6.3. SingleShotTime
6.4. SampleTime
This mode allows sampling of the time of each operation. Sampling results are split into percentiles, we’ll present results for three different percentiles p0.90, p0.999, and p1.00:
While the exact results of the simple example and the real-life example were clearly different, but they do follow the same trend. Both examples gave similar results in terms of which algorithm is the fastest and which is the slowest one.
7. Summary
In this article, we’ve conducted performance tests of five popular Java bean mapping frameworks: ModelMapper, MapStruct, Orika, Dozer, and JMapper.
As always, code samples can be found over on GitHub.