Introduction to the

Bean copies are heavily used in the work, which can greatly reduce the workload. This article stress tests common Bean copy tools so that you can choose one that is more suitable for you. This article is the continuation of the MICA Cglib Enhancement — [01] CGlib Bean Copy introduction. This column will be updated continuously, so please subscribe to us if you are interested.

Bean copy tool

  • MapStruct (Mapper implementation generated at compile time)
  • Selma (Mapper implementation generated at compile time)
  • Yangtu222-beanutils (first copy implementation bytecode)
  • Mica (first copy implementation bytecode)
  • Hutool (reflection)

model

Untyped conversion

/** * Source user **@author L.cm
 */
@Data
public class FormUser {
	private Long id;
	private String nickName;
	private Integer age;
	private String phone;
	private String email;
	private String password;
	private Integer gender;
	private String avatar;
}

/** * Convert user **@author L.cm
 */
@Data
public class ToUser {
	private String nickName;
	private String phone;
	private String email;
	private Integer gender;
	private String avatar;
}
Copy the code

Tape type conversion

/** * User model with type conversion **@author L.cm
 */
@Data
@Accessors(chain = true)
public class FormConvertUser {
	private Long id;
	private String nickName;
	private Integer age;
	private String phone;
	private String email;
	private String password;
	private Integer gender;
	private String avatar;
	@DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
	private LocalDateTime birthday;
}

/** * User model with type conversion **@author L.cm
 */
@Data
@Accessors(chain = true)
public class ToConvertUser {
	private String nickName;
	private Integer age;
	private String phone;
	private String email;
	private String password;
	private Integer gender;
	private String avatar;
	private String birthday;
}
Copy the code

Bean copy pressure test result

The environment

  • OS: macOS Mojave
  • CPU: 2.8 GHz Intel Core i5
  • RAM: 8 GB 1600 MHz DDR3
  • JVM: Oracle 1.8.0_201 64 bits

A simple model

Benchmark Score Error Units
hutool 1939.092 26.747 ops/ms
spring 3569.035 39.607 ops/ms
cglib 9112.785 560.503 ops/ms
mica 17753.409 393.245 ops/ms
yangtu222 18201.997 119.189 ops/ms
cglibMapper 37679.510 3544.624 ops/ms
mapStruct 50328.045 529.707 ops/ms
selma 200859.561 2370.531 ops/ms

With type conversion (date)

Benchmark Score Error Units
mica 1186.375 64.686 ops/ms
mapStruct 1623.478 13.894 ops/ms
selma 160020.595 2570.747 ops/ms

List model (100 items)

Benchmark Score Error Units
spring 35.974 0.555 ops/ms
mica 169.066 5.460 ops/ms

Map copy to bean

Benchmark Score Error Units
hutool 1338.551 16.746 ops/ms
mica 13577.056 27.795 ops/ms

conclusion

Java -object- Mapper – Benchmark test results are different.

Selma actually performs better than MapStruct, probably because the model is different.

Functional comparison

kit You have to write a Mapper Support the Map Supports List and Set Type conversion performance
Selma is no no Handwriting conversion is required Very high
MapStruct is no no Support for common types and complex expressions Very high
BeanUtils (yangtu222) no no is Handwriting conversion is required Very high
mica no is is Use Spring’s cast Very high
Spring no no no Does not support high
hutool no is no Does not support high

link

Source code of this project: github.com/lets-mica/m…

Open source is recommended

Pay attention to our

Scan the qr code above, more exciting content recommended every day!