-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCopyDifferFieldTest.java
More file actions
259 lines (234 loc) · 9.35 KB
/
Copy pathCopyDifferFieldTest.java
File metadata and controls
259 lines (234 loc) · 9.35 KB
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
package benchmark;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.sondertara.common.bean.model.differ.ArpBaitEntity;
import com.sondertara.common.bean.model.differ.ArpBaitVo;
import com.sondertara.common.bean.model.differ.BaitTemplateEntity;
import com.sondertara.common.bean.model.differ.BaitTemplateVo;
import com.sondertara.common.bean.model.differ.FileBaitEntity;
import com.sondertara.common.bean.model.differ.FileBaitVo;
import com.sondertara.common.bean.model.differ.ProcessBaitEntity;
import com.sondertara.common.bean.model.differ.ProcessBaitVo;
import org.junit.jupiter.api.Test;
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.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.results.format.ResultFormatType;
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 java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @author sondertara
* @since 2022/4/29 下午 03:29
*/
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class CopyDifferFieldTest {
public static final int COUNT = 100;
public static Mapper mapper = DozerBeanMapperBuilder.create().build();
public static final String DEFAULT_DIR = "E:\\workspace\\java\\tara\\example\\result";
public static String filename = "";
static {
String property = System.getProperty("copy.count");
if (null != property) {
System.out.println("Copy count:" + property);
CopySameFieldTest.COUNT = Integer.parseInt(property);
}
Path result = Paths.get(DEFAULT_DIR, "differ-benchmark-" + CopySameFieldTest.COUNT + ".csv");
filename = result.toString();
}
@Benchmark
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 10, time = 10)
@Test
public void localTest() {
// 90ms 93ms 106ms 101ms 100ms
BaitTemplateVo baitTemplateVo = getBaitTemplateVo();
for (int i = 0; i < COUNT; i++) {
BaitTemplateEntity entity = new BaitTemplateEntity();
com.sondertara.common.util.BeanUtils.copyProperties(baitTemplateVo, entity);
for (ArpBaitEntity bait : entity.getArpBaitList()) {
System.out.println(bait.getIp());
}
}
}
@Benchmark
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 10, time = 10)
public void nativeTest() {
BaitTemplateVo baitTemplateVo = getBaitTemplateVo();
for (int i = 0; i < COUNT; i++) {
convertToBaitTemplateEntity(baitTemplateVo);
}
}
@Benchmark
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 10, time = 10)
public void springBeanUtilsTest() {
BaitTemplateVo baitTemplateVo = getBaitTemplateVo();
for (int i = 0; i < COUNT; i++) {
BaitTemplateEntity baitTemplateEntity = new BaitTemplateEntity();
BeanUtils.copyProperties(baitTemplateVo, baitTemplateEntity);
}
}
/**
* cant copy if the fields type is different
*
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
//@Benchmark
public void apacheBeanUtilsTest() throws InvocationTargetException, IllegalAccessException {
BaitTemplateVo baitTemplateVo = getBaitTemplateVo();
for (int i = 0; i < COUNT; i++) {
BaitTemplateEntity baitTemplateEntity = new BaitTemplateEntity();
org.apache.commons.beanutils.BeanUtils.copyProperties(baitTemplateEntity, baitTemplateVo);
}
}
@Benchmark
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 10, time = 10)
public void hutoolBeanUtilTest() {
BaitTemplateVo baitTemplateVo = getBaitTemplateVo();
for (int i = 0; i < COUNT; i++) {
BeanUtil.copyProperties(baitTemplateVo, BaitTemplateEntity.class);
}
}
@Benchmark
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 10, time = 10)
public void hutoolConvertTest() {
BaitTemplateVo baitTemplateVo = getBaitTemplateVo();
for (int i = 0; i < COUNT; i++) {
Convert.convert(BaitTemplateEntity.class, baitTemplateVo);
}
}
@Benchmark
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 10, time = 10)
public void dozerTest() {
BaitTemplateVo baitTemplateVo = getBaitTemplateVo();
for (int i = 0; i < COUNT; i++) {
BaitTemplateEntity baitTemplateEntity = new BaitTemplateEntity();
mapper.map(baitTemplateVo, baitTemplateEntity);
}
}
public static void main(String[] args) throws RunnerException {
Options build = new OptionsBuilder().include(CopyDifferFieldTest.class.getSimpleName()).resultFormat(ResultFormatType.CSV).result(filename).build();
new Runner(build).run();
}
private BaitTemplateVo getBaitTemplateVo() {
BaitTemplateVo baitTemplateVo = new BaitTemplateVo();
baitTemplateVo.setName("123");
baitTemplateVo.setOsType("windows");
baitTemplateVo.setId(123456L);
baitTemplateVo.setArpBaitList(new ArrayList<ArpBaitVo>() {{
ArpBaitVo arpBaitVo1 = new ArpBaitVo();
arpBaitVo1.setId(123);
arpBaitVo1.setIp("1.2.3.4");
arpBaitVo1.setMac("dd-dd-dd-dd-dd-dd");
add(arpBaitVo1);
ArpBaitVo arpBaitVo2 = new ArpBaitVo();
arpBaitVo2.setId(1234);
arpBaitVo2.setIp("1.2.3.4");
arpBaitVo2.setMac("dd-dd-dd-dd-dd-dd");
add(arpBaitVo2);
}});
baitTemplateVo.setFileBaitList(new ArrayList<FileBaitVo>() {{
FileBaitVo fileBaitVo1 = new FileBaitVo();
fileBaitVo1.setId(123);
fileBaitVo1.setPath("/test");
add(fileBaitVo1);
FileBaitVo fileBaitVo2 = new FileBaitVo();
fileBaitVo2.setId(1234);
fileBaitVo2.setPath("/test");
add(fileBaitVo2);
}});
baitTemplateVo.setProcessBaitList(new ArrayList<ProcessBaitVo>() {{
ProcessBaitVo processBaitVo1 = new ProcessBaitVo();
processBaitVo1.setId(123);
processBaitVo1.setCommand("df -h");
add(processBaitVo1);
ProcessBaitVo processBaitVo2 = new ProcessBaitVo();
processBaitVo2.setId(123);
processBaitVo2.setCommand("df -h");
add(processBaitVo2);
}});
return baitTemplateVo;
}
public static BaitTemplateEntity convertToBaitTemplateEntity(BaitTemplateVo item) {
if (item == null) {
return null;
}
BaitTemplateEntity result = new BaitTemplateEntity();
result.setId(item.getId().intValue());
result.setName(item.getName());
result.setOsType(item.getOsType());
List<FileBaitVo> fileBaitList = item.getFileBaitList();
if (fileBaitList == null) {
result.setFileBaitList(null);
} else {
result.setFileBaitList(fileBaitList.stream().map(CopyDifferFieldTest::convertToFileBaitEntity).collect(Collectors.toList()));
}
List<ArpBaitVo> arpBaitList = item.getArpBaitList();
if (arpBaitList == null) {
result.setArpBaitList(null);
} else {
result.setArpBaitList(arpBaitList.stream().map(CopyDifferFieldTest::convertToArpBaitEntity).collect(Collectors.toList()));
}
List<ProcessBaitVo> processBaitList = item.getProcessBaitList();
if (processBaitList == null) {
result.setProcessBaitList(null);
} else {
result.setProcessBaitList(processBaitList.stream().map(CopyDifferFieldTest::convertToProcessBaitEntity).collect(Collectors.toList()));
}
return result;
}
public static ProcessBaitEntity convertToProcessBaitEntity(ProcessBaitVo item) {
if (item == null) {
return null;
}
ProcessBaitEntity result = new ProcessBaitEntity();
result.setId(item.getId());
result.setCommand(item.getCommand());
return result;
}
public static ArpBaitEntity convertToArpBaitEntity(ArpBaitVo item) {
if (item == null) {
return null;
}
ArpBaitEntity result = new ArpBaitEntity();
result.setId(item.getId());
result.setIp(item.getIp());
result.setMac(item.getMac());
return result;
}
public static FileBaitEntity convertToFileBaitEntity(FileBaitVo item) {
if (item == null) {
return null;
}
FileBaitEntity result = new FileBaitEntity();
result.setId(item.getId());
result.setPath(item.getPath());
return result;
}
}