LoadCenter/src/test/java/LoadTest.java

173 lines
5.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import com.lyh.LoadCenterApplication;
import com.lyh.common.redis.service.RedisService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
/**
* @ProjectName: LoadCenter
* @Author: LiuYunHu
* @CreateTime: 2024/4/14
* @Description: 负载测试
*/
@SpringBootTest(classes = LoadCenterApplication.class)
@Log4j2
public class LoadTest {
@Autowired
private RedisService redisService;
@Test
public void load() {
//初始化序列
redisService.setCacheObject("cursor", 0);
//初始化缓存
redisService.deleteObject("work:node:gateway");
//模拟三台节点服务器 的ID或IP 权重值
ArrayList<WorkGatewayNode> nodeIdList = new ArrayList<>() {{
add(new WorkGatewayNode("node-A", 8));
add(new WorkGatewayNode("node-B", 12));
add(new WorkGatewayNode("node-C", 2));
add(new WorkGatewayNode("node-D", 39));
add(new WorkGatewayNode("node-E", 39));
}};
//100
ArrayList<String> loadNodeList = new ArrayList<>();
long count = nodeIdList.stream().mapToInt(WorkGatewayNode::getWeight).sum();//求和
if (count < 100) {
List<WorkGatewayNode> list = nodeIdList.stream().sorted((o1, o2) -> o2.getWeight() - o1.getWeight()).toList();
int countWeight = 0;//给权重高的节点 权重再加一个
for (long i = count; i < 100; i++) {
WorkGatewayNode workGatewayNode = list.get(countWeight++ % list.size());
workGatewayNode.setWeight(workGatewayNode.getWeight() + 1);
}
}
whFor:
while (true) {
for (WorkGatewayNode workGatewayNode : nodeIdList) {
int weight = workGatewayNode.getWeight();
if (weight > 0) {
loadNodeList.add(
workGatewayNode.getNodeId()
);
workGatewayNode.setWeight(weight - 1);
}
}
int sum = nodeIdList.stream()
.mapToInt(WorkGatewayNode::getWeight)
.sum();
if (sum <= 0) {
break whFor;
}
}
redisService.setCacheList("work:node:gateway", loadNodeList);
CountDownLatch countDownLatch = new CountDownLatch(3000);
new Thread(() -> {
for (int i = 0; i < 1000; i++) {
Long cursor = redisService.increment("cursor", 1L);//游标
String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
log.info(Thread.currentThread().getName() + "" + cursor + "---" + nodeId);
//每次调通,进行统计
stiNode.sti(nodeId);
countDownLatch.countDown();
}
}).start();
new Thread(() -> {
for (int i = 0; i < 1000; i++) {
Long cursor = redisService.increment("cursor", 1L);//游标
String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
log.info(Thread.currentThread().getName() + "" + cursor + "---" + nodeId);
//每次调通,进行统计
stiNode.sti(nodeId);
countDownLatch.countDown();
}
}).start();
new Thread(() -> {
for (int i = 0; i < 1000; i++) {
Long cursor = redisService.increment("cursor", 1L);//游标
String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
log.info(Thread.currentThread().getName() + "" + cursor + "---" + nodeId);
//每次调通,进行统计
stiNode.sti(nodeId);
countDownLatch.countDown();
}
}).start();
try {
countDownLatch.await();
stiNode.show();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// for (int i = 0; i < 1000; i++) {
// Long cursor = redisService.increment("cursor", 1L);//游标
// String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
// log.info(cursor + "---" + nodeId);
// }
log.info(nodeIdList);
log.info(loadNodeList);
}
}
@Log4j2
class stiNode {
private static Map<String, Integer> stiNodeMap = new HashMap<>();
public synchronized static void sti(String nodeId) {
Integer stiCount = stiNodeMap.getOrDefault(nodeId, 0);
stiNodeMap.put(nodeId, stiCount + 1);
}
public static void show() {
stiNodeMap.forEach((key,val)->{
log.info(key+"----"+val);
});
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class WorkGatewayNode {
private String nodeId;
private int weight;
}