-
Notifications
You must be signed in to change notification settings - Fork 1
/
BeanCopyTest.java
281 lines (225 loc) · 8.39 KB
/
BeanCopyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package org.gy.demo.log;
import com.alibaba.fastjson.JSON;
import com.github.houbb.bean.mapping.asm.util.AsmBeanUtil;
import com.github.houbb.bean.mapping.core.util.BeanUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.assertj.core.util.Lists;
import org.dozer.DozerBeanMapper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
/**
* 功能描述:
*
* @author gy
* @version 1.0.0
* @date 2023/5/12 10:53
*/
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
@Fork(value = 1)
@Threads(2)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
public class BeanCopyTest {
@Param({"1000"})
private int loop;
private List<SourceBean> sources;
private static final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
private static final ModelMapper modelMapper = new ModelMapper();
private static final DozerBeanMapper dozerMapper = new DozerBeanMapper();
private static final BeanCopier beanCopier = BeanCopier.create(SourceBean.class, TargetBean.class, false);
static {
mapperFactory.classMap(SourceBean.class, TargetBean.class)
// .field("source", "target") // 字段不一致时可以指定
.byDefault().register();
modelMapper.addMappings(new PropertyMap<SourceBean, TargetBean>() {
@Override
protected void configure() {
// 属性值不一样可以自己操作
// map().setTarget(source.getSource());
}
});
}
@Setup
public void setup() {
sources = buildSources(loop);
}
@TearDown
public void tearDown() {
sources = null;
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(BeanCopyTest.class.getSimpleName()).build();
new Runner(opt).run();
// List<SourceBean> sources = buildSources(5);
// List<TargetBean> targetBeans = doCopy(sources, (s, t) -> beanCopier.copy(s, t, null));
// targetBeans.forEach(System.out::println);
}
@Benchmark
public void apacheBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, s -> {
TargetBean targetBean = new TargetBean();
try {
org.apache.commons.beanutils.BeanUtils.copyProperties(targetBean, s);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
return targetBean;
});
bh.consume(targetBeans);
}
@Benchmark
public void beanCopierBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, (s, t) -> beanCopier.copy(s, t, null));
bh.consume(targetBeans);
}
@Benchmark
public void beanMappingBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, BeanUtil::copyProperties);
bh.consume(targetBeans);
}
@Benchmark
public void beanAsmBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, AsmBeanUtil::copyProperties);
bh.consume(targetBeans);
}
@Benchmark
public void dozerBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, s -> dozerMapper.map(s, TargetBean.class));
bh.consume(targetBeans);
}
@Benchmark
public void fastJsonBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, s -> JSON.parseObject(JSON.toJSONString(s), TargetBean.class));
bh.consume(targetBeans);
}
@Benchmark
public void mapStructBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, BeanMapping.INSTANCE::map);
bh.consume(targetBeans);
}
@Benchmark
public void modelMapperBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, modelMapper::map);
bh.consume(targetBeans);
}
@Benchmark
public void orikaBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, mapperFactory.getMapperFacade()::map);
bh.consume(targetBeans);
}
@Benchmark
public void springBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, BeanUtils::copyProperties);
bh.consume(targetBeans);
}
@Benchmark
public void getSetBeanCopy(Blackhole bh) {
List<TargetBean> targetBeans = doCopy(sources, BeanCopyTest::getSetMapping);
bh.consume(targetBeans);
}
private static List<TargetBean> doCopy(List<SourceBean> sources, Function<SourceBean, TargetBean> beanCopy) {
return sources.stream().map(beanCopy).collect(Collectors.toList());
}
private static List<TargetBean> doCopy(List<SourceBean> sources, BiConsumer<SourceBean, TargetBean> beanCopy) {
return sources.stream().map(s -> {
TargetBean targetBean = new TargetBean();
beanCopy.accept(s, targetBean);
return targetBean;
}).collect(Collectors.toList());
}
private static List<SourceBean> buildSources(Integer num) {
List<SourceBean> sources = Lists.newArrayList();
for (int j = 0; j < num; j++) {
int index = ThreadLocalRandom.current().nextInt();
boolean flag = ThreadLocalRandom.current().nextBoolean();
SourceBean sourceBean = new SourceBean();
sourceBean.setAge(index);
sourceBean.setTitle("title" + index);
sourceBean.setSource("source" + index);
sourceBean.setEat(flag);
sourceBean.setStart(new Date());
sourceBean.setItem(new Item(System.currentTimeMillis(), System.currentTimeMillis()));
sources.add(sourceBean);
}
return sources;
}
public static TargetBean getSetMapping(SourceBean source) {
TargetBean targetBean = new TargetBean();
targetBean.setAge(source.getAge());
targetBean.setTitle(source.getTitle());
targetBean.setEat(source.getEat());
targetBean.setStart(source.getStart());
targetBean.setItem(source.getItem());
return targetBean;
}
@Mapper
interface BeanMapping {
BeanMapping INSTANCE = Mappers.getMapper(BeanMapping.class);
TargetBean map(SourceBean source);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class SourceBean {
private Integer age;
private String title;
private String source;
private Boolean eat;
private Date start;
private Item item;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class TargetBean {
private Integer age;
private String title;
private String target;
private Boolean eat;
private Date start;
private Item item;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Item {
private Long startTime;
private Long endTime;
}
}