Skip to content

Commit

Permalink
test: 使用 1000 条数据简单测试耗时问题
Browse files Browse the repository at this point in the history
  • Loading branch information
houkunlin committed Jul 1, 2024
1 parent 01e498c commit b63d7ea
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/test/java/test/application/common/MyProvider.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package test.application.common;

import com.houkunlin.system.dict.starter.bean.DictTypeVo;
import com.houkunlin.system.dict.starter.bean.DictValueVo;
import com.houkunlin.system.dict.starter.provider.DictProvider;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
* @author HouKunLin
Expand All @@ -19,10 +24,28 @@ public boolean isStoreDictType() {

@Override
public Iterator<DictTypeVo> dictTypeIterator() {
final DictTypeVo typeVo = DictTypeVo.newBuilder("name", "测试字典")
.add("1", "测试1")
.add("2", "测试2")
long startTime = System.nanoTime();
List<DictUser> users = new ArrayList<>();
for (int i = 0; i < 1586; i++) {
users.add(new DictUser(i, "名称" + i));
}
System.out.println("加载用户数据耗时:" + (System.nanoTime() - startTime) / 100_0000.0 + "ms");
startTime = System.nanoTime();
final DictTypeVo typeVo = DictTypeVo.newBuilder("DictUser", "DictUser")
.build();
List<DictValueVo> children = typeVo.getChildren();
users.forEach(dictUser -> {
DictValueVo valueVo = new DictValueVo("DictUser", dictUser.getId(), dictUser.getName(), 0);
children.add(valueVo);
});
System.out.println("转换用户数据耗时:" + (System.nanoTime() - startTime) / 100_0000.0 + "ms");
return Collections.singletonList(typeVo).iterator();
}
}

@Data
@AllArgsConstructor
class DictUser {
private int id;
private String name;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package test.application.common;

import com.houkunlin.system.dict.starter.bean.DictValueVo;
import com.houkunlin.system.dict.starter.notice.RefreshDictEvent;
import com.houkunlin.system.dict.starter.notice.RefreshDictValueEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;

/**
* @author HouKunLin
*/
Expand All @@ -24,5 +29,45 @@ public void refreshDicEvent() {
logger.info("开始定时刷新事件");
publisher.publishEvent(new RefreshDictEvent("定时刷新字典:" + System.currentTimeMillis(), true, false));
logger.info("结束定时刷新事件");


long startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
publisher.publishEvent(new RefreshDictValueEvent(DictValueVo.builder().dictType("DictUser").value("" + i).title("昵称" + i).build(), false));
}
logger.info("单条数据刷新1000次耗时:{} ms", (System.nanoTime() - startTime) / 100_0000.0);


startTime = System.nanoTime();
List<DictValueVo> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
list.add(DictValueVo.builder().dictType("DictUser").value("" + i).title("昵称" + i).build());
}
publisher.publishEvent(new RefreshDictValueEvent(list, false));
logger.info("批量数据刷新1000次耗时:{} ms", (System.nanoTime() - startTime) / 100_0000.0);


list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(DictValueVo.builder().dictType("DictUser").value("" + i).title("昵称" + i).build());
}
startTime = System.nanoTime();
int x = 1000 / list.size() + 1;
for (int i = 0; i < x; i++) {
publisher.publishEvent(new RefreshDictValueEvent(list, false));
}
logger.info("批量数据刷新 {} 条数据耗时(默认):{} ms", list.size(), (System.nanoTime() - startTime) / 100_0000.0);


for (int i = 10; i < 20; i++) {
list.add(DictValueVo.builder().dictType("DictUser").value("" + i).title("昵称" + i).build());
}
startTime = System.nanoTime();
for (int i = 0; i < x; i++) {
publisher.publishEvent(new RefreshDictValueEvent(list, false));
}
logger.info("批量数据刷新 {} 条数据耗时(批量):{} ms", list.size(), (System.nanoTime() - startTime) / 100_0000.0);

logger.info("结束定时刷新事件");
}
}

0 comments on commit b63d7ea

Please sign in to comment.