Skip to content

Commit 460c801

Browse files
author
邢鹏成
committed
0.9.6版本
新增性能测试
1 parent 42216ca commit 460c801

File tree

4 files changed

+221
-4
lines changed

4 files changed

+221
-4
lines changed

easy-es-sample/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<groupId>org.springframework.boot</groupId>
3636
<artifactId>spring-boot-starter-test</artifactId>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.apache.commons</groupId>
40+
<artifactId>commons-lang3</artifactId>
41+
<scope>test</scope>
42+
</dependency>
3843
<dependency>
3944
<groupId>junit</groupId>
4045
<artifactId>junit</artifactId>

easy-es-sample/src/main/java/com/xpc/easyes/sample/entity/Document.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.xpc.easyes.sample.entity;
22

33
import com.xpc.easyes.core.anno.TableField;
4-
import com.xpc.easyes.core.anno.TableId;
54
import com.xpc.easyes.core.enums.FieldStrategy;
6-
import com.xpc.easyes.core.enums.IdType;
75
import lombok.Data;
86

97
import java.time.LocalDateTime;
@@ -18,9 +16,7 @@ public class Document {
1816
/**
1917
* es中的唯一id
2018
*/
21-
@TableId(type = IdType.CUSTOMIZE)
2219
private String id;
23-
2420
/**
2521
* 文档标题
2622
*/
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package com.xpc.easyes.sample.test.performance;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.xpc.easyes.core.conditions.LambdaEsQueryWrapper;
5+
import com.xpc.easyes.sample.entity.Document;
6+
import com.xpc.easyes.sample.mapper.DocumentMapper;
7+
import com.xpc.easyes.sample.test.TestEasyEsApplication;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.apache.commons.lang3.time.StopWatch;
10+
import org.elasticsearch.action.bulk.BulkRequest;
11+
import org.elasticsearch.action.delete.DeleteRequest;
12+
import org.elasticsearch.action.index.IndexRequest;
13+
import org.elasticsearch.action.search.SearchRequest;
14+
import org.elasticsearch.action.search.SearchResponse;
15+
import org.elasticsearch.action.update.UpdateRequest;
16+
import org.elasticsearch.client.RequestOptions;
17+
import org.elasticsearch.client.RestHighLevelClient;
18+
import org.elasticsearch.common.xcontent.XContentType;
19+
import org.elasticsearch.index.query.BoolQueryBuilder;
20+
import org.elasticsearch.index.query.QueryBuilders;
21+
import org.elasticsearch.search.builder.SearchSourceBuilder;
22+
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.test.context.junit4.SpringRunner;
27+
28+
import javax.annotation.Resource;
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import java.util.List;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.stream.Collectors;
35+
36+
import static com.xpc.easyes.core.constants.BaseEsConstants.DEFAULT_SIZE;
37+
38+
/**
39+
* 性能测试
40+
* 测试机器:i7 8核16G 1.8GHZ
41+
*
42+
* <p>
43+
* Copyright © 2021 xpc1024 All Rights Reserved
44+
**/
45+
@Slf4j
46+
@RunWith(SpringRunner.class)
47+
@SpringBootTest(classes = TestEasyEsApplication.class)
48+
public class PerformanceTest {
49+
@Resource
50+
private DocumentMapper documentMapper;
51+
52+
@Resource
53+
private RestHighLevelClient client;
54+
55+
@Test
56+
public void testSelectByEE() {
57+
StopWatch stopwatch = StopWatch.createStarted();
58+
// 测试时需将Document实体上加上注解@TableName("索引名")
59+
// 将查询索引指定为和通过RestHighLevelClient查询时一样的索引名称
60+
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
61+
wrapper.match(Document::getTitle, "茶叶")
62+
.match(Document::getContent, "茶叶")
63+
.highLight(Document::getTitle);
64+
List<Document> documents = documentMapper.selectList(wrapper);
65+
66+
// 查询总条数
67+
Long count = documentMapper.selectCount(new LambdaEsQueryWrapper<>());
68+
stopwatch.stop();
69+
log.info("本次查询从:{}条数据中共命中:{}条数据,耗时总计:{}毫秒", count, documents.size(), stopwatch.getTime(TimeUnit.MILLISECONDS));
70+
// 本次查询从:5135条数据中共命中:15条数据,耗时总计:434毫秒 多次测试均值维持440毫秒左右
71+
// 对比下面直接使用RestHighLevelClient查询,查询仅慢10毫秒左右, 除去注释,节省了5倍代码,且查询越复杂,节省越多
72+
}
73+
74+
@Test
75+
public void testSelectByRestHighLevelClient() {
76+
// 构建查询条件
77+
StopWatch stopwatch = StopWatch.createStarted();
78+
SearchSourceBuilder builder = new SearchSourceBuilder();
79+
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
80+
boolQueryBuilder.must(QueryBuilders.matchQuery("title", "茶叶"));
81+
boolQueryBuilder.must(QueryBuilders.matchQuery("content", "茶叶"));
82+
builder.query(boolQueryBuilder);
83+
HighlightBuilder highlightBuilder = new HighlightBuilder();
84+
highlightBuilder.field("title");
85+
highlightBuilder.preTags("<em>");
86+
highlightBuilder.postTags("</em>");
87+
builder.highlighter(highlightBuilder);
88+
builder.size(DEFAULT_SIZE);
89+
SearchRequest request = new SearchRequest("kiplatform_library").source(builder);
90+
SearchResponse search = null;
91+
try {
92+
search = client.search(request, RequestOptions.DEFAULT);
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
}
96+
List<Document> documents = Arrays.stream(search.getHits().getHits())
97+
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Document.class))
98+
.collect(Collectors.toList());
99+
100+
SearchRequest countRequest = new SearchRequest("kiplatform_library");
101+
SearchResponse countResponse = null;
102+
try {
103+
countResponse = client.search(countRequest, RequestOptions.DEFAULT);
104+
} catch (IOException e) {
105+
e.printStackTrace();
106+
}
107+
Long count = countResponse.getHits().getTotalHits().value;
108+
stopwatch.stop();
109+
log.info("本次查询从:{}条数据中共命中:{}条数据,耗时总计:{}毫秒", count, documents.size(), stopwatch.getTime(TimeUnit.MILLISECONDS));
110+
// 本次查询从:5135条数据中共命中:15条数据,耗时总计:428毫秒 多次测试 均值维持430毫秒左右
111+
}
112+
113+
114+
@Test
115+
public void testInsertByEE() {
116+
StopWatch stopwatch = StopWatch.createStarted();
117+
List<Document> documents = new ArrayList<>();
118+
for (int i = 0; i < 100; i++) {
119+
Document document = new Document();
120+
document.setTitle("测试新增性能" + i);
121+
document.setContent("测试新增性能内容" + i);
122+
documents.add(document);
123+
}
124+
documentMapper.insertBatch(documents);
125+
stopwatch.stop();
126+
log.info("本次共插入:{}条数据,耗时:{}毫秒", documents.size(), stopwatch.getTime(TimeUnit.MILLISECONDS));
127+
// 本次共插入:100条数据,耗时:343毫秒 多次测试稳定在340毫秒左右
128+
// 对比下面直接使用RestHighLevelClient 并无明显差异
129+
}
130+
131+
@Test
132+
public void testInsertByRestHighLevelClient() {
133+
StopWatch stopWatch = StopWatch.createStarted();
134+
BulkRequest bulkRequest = new BulkRequest();
135+
int total = 100;
136+
for (int i = 0; i < total; i++) {
137+
Document document = new Document();
138+
document.setTitle("测试新增性能" + i);
139+
document.setContent("测试新增性能内容" + i);
140+
IndexRequest indexRequest = new IndexRequest("document");
141+
indexRequest.source(JSON.toJSON(document), XContentType.JSON);
142+
bulkRequest.add(indexRequest);
143+
}
144+
try {
145+
client.bulk(bulkRequest, RequestOptions.DEFAULT);
146+
} catch (IOException e) {
147+
e.printStackTrace();
148+
}
149+
stopWatch.stop();
150+
log.info("本次共插入:{}条数据,耗时:{}毫秒", total, stopWatch.getTime(TimeUnit.MILLISECONDS));
151+
// 本次共插入:100条数据,耗时:342毫秒 多次测试稳定在340毫秒左右
152+
}
153+
154+
@Test
155+
public void testUpdateByEE() {
156+
StopWatch stopWatch = StopWatch.createStarted();
157+
Document document = new Document();
158+
document.setId("PmF0SH8B0E2Rzy0qcFBz");
159+
document.setTitle("哈哈哈");
160+
document.setContent("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
161+
documentMapper.updateById(document);
162+
stopWatch.stop();
163+
log.info("耗时:{}毫秒", stopWatch.getTime(TimeUnit.MILLISECONDS));
164+
// 多次测试平均耗时:330毫秒
165+
// 对比下面直接使用RestHighLevelClient 并无明显差异
166+
}
167+
168+
@Test
169+
public void testUpdateByRestHighLevelClient() {
170+
StopWatch stopWatch = StopWatch.createStarted();
171+
Document document = new Document();
172+
document.setTitle("哈哈哈");
173+
document.setContent("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
174+
UpdateRequest updateRequest = new UpdateRequest("document", "RWF0SH8B0E2Rzy0qcFBz");
175+
updateRequest.doc(JSON.toJSONString(document), XContentType.JSON);
176+
try {
177+
client.update(updateRequest, RequestOptions.DEFAULT);
178+
} catch (IOException e) {
179+
e.printStackTrace();
180+
}
181+
stopWatch.stop();
182+
log.info("耗时:{}毫秒", stopWatch.getTime(TimeUnit.MILLISECONDS));
183+
// 多次测试平均耗时:323毫秒
184+
}
185+
186+
@Test
187+
public void testDeleteByEE() {
188+
StopWatch stopWatch = StopWatch.createStarted();
189+
documentMapper.deleteById("TWF0SH8B0E2Rzy0qcFBz");
190+
stopWatch.stop();
191+
log.info("耗时:{}毫秒", stopWatch.getTime(TimeUnit.MILLISECONDS));
192+
// 多次测试平均耗时耗时:131毫秒
193+
// 对比下面直接使用RestHighLevelClient 并无明显差异
194+
}
195+
196+
@Test
197+
public void testDeleteByRestHighLevelClient() {
198+
StopWatch stopWatch = StopWatch.createStarted();
199+
DeleteRequest deleteRequest = new DeleteRequest("document", "RWF0SH8B0E2Rzy0qcFBz");
200+
try {
201+
client.delete(deleteRequest, RequestOptions.DEFAULT);
202+
} catch (IOException e) {
203+
e.printStackTrace();
204+
}
205+
stopWatch.stop();
206+
log.info("耗时:{}毫秒", stopWatch.getTime(TimeUnit.MILLISECONDS));
207+
// 多次测试平均耗时耗时:130毫秒
208+
}
209+
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<properties>
2020
<junit.version>4.13.2</junit.version>
2121
<spring-boot.version>2.5.4</spring-boot.version>
22+
<commons-lang3>3.9</commons-lang3>
2223
</properties>
2324
<dependencyManagement>
2425
<dependencies>
@@ -28,6 +29,12 @@
2829
<version>${junit.version}</version>
2930
</dependency>
3031

32+
<dependency>
33+
<groupId>org.apache.commons</groupId>
34+
<artifactId>commons-lang3</artifactId>
35+
<version>${commons-lang3}</version>
36+
</dependency>
37+
3138
<dependency>
3239
<groupId>org.springframework.boot</groupId>
3340
<artifactId>spring-boot-starter-web</artifactId>

0 commit comments

Comments
 (0)