mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-30 06:22:15 +00:00
test[signal]: SignalBridge的Test并发测试用例
This commit is contained in:
@@ -28,7 +28,7 @@ public class SignalAttachment implements IAttachment {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 0;
|
||||
|
||||
private static final AtomicInteger ATOMIC_ID = new AtomicInteger(0);
|
||||
public static final AtomicInteger ATOMIC_ID = new AtomicInteger(0);
|
||||
|
||||
/**
|
||||
* 唯一标识一个packet, 唯一表示一个Attachment,hashcode() and equals() 也通过signalId计算
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.router;
|
||||
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
/**
|
||||
* 同步或异步的调用控制器
|
||||
*
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class PacketSignalArray {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PacketSignalArray.class);
|
||||
|
||||
// equal with 16383
|
||||
private static final int SIGNAL_MASK = 0B00000000_00000000_01111111_11111111;
|
||||
|
||||
private static final AtomicReferenceArray<Integer> signalPacketArray = new AtomicReferenceArray<>(SIGNAL_MASK + 1);
|
||||
|
||||
/**
|
||||
* Session控制同步或异步的附加包,key:signalId
|
||||
*/
|
||||
private static final Map<Integer, Integer> signalAttachmentMap = new ConcurrentHashMap<>(1000);
|
||||
|
||||
public static void addSignalAttachment(int signalId) {
|
||||
var hash = signalId & SIGNAL_MASK;
|
||||
|
||||
if (signalPacketArray.compareAndSet(hash, null, signalId)) {
|
||||
return;
|
||||
}
|
||||
signalAttachmentMap.put(signalId, signalId);
|
||||
}
|
||||
|
||||
|
||||
public static void removeSignalAttachment(int signalId) {
|
||||
var hash = signalId & SIGNAL_MASK;
|
||||
var oldSignalId = signalPacketArray.get(hash);
|
||||
|
||||
if (oldSignalId != null && oldSignalId == signalId && signalPacketArray.compareAndSet(hash, oldSignalId, null)) {
|
||||
return;
|
||||
}
|
||||
signalAttachmentMap.remove(signalId);
|
||||
}
|
||||
|
||||
public static void status() {
|
||||
var count = 0;
|
||||
for (int i = 0; i < SIGNAL_MASK + 1; i++) {
|
||||
var value = signalPacketArray.get(i);
|
||||
if (value != null) {
|
||||
logger.info("signalPacketArray has attachment [index:{}][count:{}][value:{}]", i, ++count, JsonUtils.object2String(value));
|
||||
}
|
||||
}
|
||||
|
||||
signalAttachmentMap.forEach((key, value) -> {
|
||||
logger.info("signalAttachmentMap has attachment [key:{}][value:{}]", key, JsonUtils.object2String(value));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.router;
|
||||
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 同步或异步的调用控制器
|
||||
*
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class PacketSignalMap {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PacketSignalMap.class);
|
||||
|
||||
private static final Map<Integer, Integer> signalAttachmentMap = new ConcurrentHashMap<>(1_0000);
|
||||
|
||||
public static void addSignalAttachment(int signalId) {
|
||||
signalAttachmentMap.put(signalId, signalId);
|
||||
}
|
||||
|
||||
|
||||
public static void removeSignalAttachment(int signalId) {
|
||||
signalAttachmentMap.remove(signalId);
|
||||
}
|
||||
|
||||
public static void status() {
|
||||
signalAttachmentMap.forEach((key, value) -> {
|
||||
logger.info("signalAttachmentMap has attachment [key:{}][value:{}]", key, JsonUtils.object2String(value));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,16 +13,14 @@
|
||||
package com.zfoo.net.router;
|
||||
|
||||
import com.zfoo.event.manager.EventBus;
|
||||
import com.zfoo.net.router.attachment.SignalAttachment;
|
||||
import com.zfoo.net.router.route.SignalBridge;
|
||||
import com.zfoo.scheduler.util.TimeUtils;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -31,24 +29,17 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
@Ignore
|
||||
public class SignalBridgeTest {
|
||||
|
||||
private final AtomicInteger atomicInteger = new AtomicInteger(0);
|
||||
|
||||
private final int executorSize = EventBus.EXECUTORS_SIZE / 2;
|
||||
private final int executorSize = EventBus.EXECUTORS_SIZE;
|
||||
private final int count = 100_0000;
|
||||
private final int totalIndex = 10;
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
// 预热
|
||||
arrayTest();
|
||||
mapTest();
|
||||
|
||||
ThreadUtils.sleep(3000);
|
||||
|
||||
arrayTest();
|
||||
mapTest();
|
||||
Assert.assertEquals(executorSize * count * totalIndex * 4, atomicInteger.get());
|
||||
System.out.println(atomicInteger.get());
|
||||
for (int i = 0; i < 10; i++) {
|
||||
arrayTest();
|
||||
}
|
||||
SignalBridge.status();
|
||||
System.out.println(SignalAttachment.ATOMIC_ID.get());
|
||||
}
|
||||
|
||||
public void arrayTest() throws InterruptedException {
|
||||
@@ -65,57 +56,22 @@ public class SignalBridgeTest {
|
||||
});
|
||||
}
|
||||
countDownLatch.await();
|
||||
SignalBridge.status();
|
||||
System.out.println(TimeUtils.currentTimeMillis() - startTime);
|
||||
}
|
||||
|
||||
public void mapTest() throws InterruptedException {
|
||||
var startTime = TimeUtils.currentTimeMillis();
|
||||
|
||||
var countDownLatch = new CountDownLatch(executorSize);
|
||||
for (int i = 0; i < executorSize; i++) {
|
||||
EventBus.execute(i, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
addAndRemoveMap();
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
countDownLatch.await();
|
||||
PacketSignalMap.status();
|
||||
System.out.println(TimeUtils.currentTimeMillis() - startTime);
|
||||
}
|
||||
|
||||
public void addAndRemoveArray() {
|
||||
var list = new ArrayList<Integer>(totalIndex);
|
||||
var signalList = new ArrayList<Integer>(totalIndex);
|
||||
for (var i = 0; i < count; i++) {
|
||||
list.clear();
|
||||
signalList.clear();
|
||||
for (var j = 0; j < totalIndex; j++) {
|
||||
var index = atomicInteger.incrementAndGet();
|
||||
list.add(index);
|
||||
PacketSignalArray.addSignalAttachment(index);
|
||||
var signalAttachment = new SignalAttachment();
|
||||
SignalBridge.addSignalAttachment(signalAttachment);
|
||||
signalList.add(signalAttachment.getSignalId());
|
||||
}
|
||||
|
||||
for (var index : list) {
|
||||
PacketSignalArray.removeSignalAttachment(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void addAndRemoveMap() {
|
||||
var list = new ArrayList<Integer>(totalIndex);
|
||||
for (var i = 0; i < count; i++) {
|
||||
list.clear();
|
||||
for (var j = 0; j < totalIndex; j++) {
|
||||
var index = atomicInteger.incrementAndGet();
|
||||
list.add(index);
|
||||
PacketSignalMap.addSignalAttachment(index);
|
||||
}
|
||||
|
||||
for (var index : list) {
|
||||
PacketSignalMap.removeSignalAttachment(index);
|
||||
for (var signalId : signalList) {
|
||||
SignalBridge.removeSignalAttachment(signalId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user