性能压测
# 引入Maven依赖
<dependency>
<groupId>io.github.smartboot.socket</groupId>
<artifactId>aio-pro</artifactId>
<version>1.5.52</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 启动服务端
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.buffer.BufferPagePool;
import org.smartboot.socket.extension.plugins.MonitorPlugin;
import org.smartboot.socket.extension.processor.AbstractMessageProcessor;
import org.smartboot.socket.extension.protocol.StringProtocol;
import org.smartboot.socket.transport.AioQuickServer;
import org.smartboot.socket.transport.AioSession;
import org.smartboot.socket.transport.WriteBuffer;
import java.io.IOException;
/**
* @author 三刀
* @version V1.0 , 2018/11/23
*/
public class StringServer {
public static void main(String[] args) throws IOException {
AbstractMessageProcessor<String> processor = new AbstractMessageProcessor<String>() {
@Override
public void process0(AioSession session, String msg) {
WriteBuffer outputStream = session.writeBuffer();
try {
byte[] bytes = msg.getBytes();
outputStream.writeInt(bytes.length);
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void stateEvent0(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
}
};
BufferPagePool bufferPagePool = new BufferPagePool(1024 * 1024 * 16, Runtime.getRuntime().availableProcessors() + 1, true);
AioQuickServer server = new AioQuickServer(8888, new StringProtocol(), processor);
server.setReadBufferSize(1024 * 1024)
.setThreadNum(Runtime.getRuntime().availableProcessors() + 1)
.setBufferFactory(() -> bufferPagePool)
.setWriteBuffer(4096, 512);
processor.addPlugin(new MonitorPlugin<>(5));
server.start();
}
}
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
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
# 启动客户端
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.buffer.BufferPagePool;
import org.smartboot.socket.enhance.EnhanceAsynchronousChannelProvider;
import org.smartboot.socket.extension.plugins.MonitorPlugin;
import org.smartboot.socket.extension.processor.AbstractMessageProcessor;
import org.smartboot.socket.extension.protocol.StringProtocol;
import org.smartboot.socket.transport.AioQuickClient;
import org.smartboot.socket.transport.AioSession;
import org.smartboot.socket.transport.WriteBuffer;
import java.io.IOException;
import java.nio.channels.AsynchronousChannelGroup;
import java.util.concurrent.ExecutionException;
/**
* @author 三刀
* @version V1.0 , 2018/11/23
*/
public class StringClient {
public static void main(String[] args) throws IOException {
BufferPagePool bufferPagePool = new BufferPagePool(1024 * 1024 * 32, 10, true);
AbstractMessageProcessor<String> processor = new AbstractMessageProcessor<String>() {
@Override
public void process0(AioSession session, String msg) {
}
@Override
public void stateEvent0(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
}
};
processor.addPlugin(new MonitorPlugin<>(5));
AsynchronousChannelGroup asynchronousChannelGroup = new EnhanceAsynchronousChannelProvider().openAsynchronousChannelGroup(Runtime.getRuntime().availableProcessors(), r -> new Thread(r, "ClientGroup"));
for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
new StringClient().test(asynchronousChannelGroup, bufferPagePool, processor);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
public void test(AsynchronousChannelGroup asynchronousChannelGroup, BufferPagePool bufferPagePool, AbstractMessageProcessor<String> processor) throws InterruptedException, ExecutionException, IOException {
AioQuickClient client = new AioQuickClient("localhost", 8888, new StringProtocol(), processor);
client.setBufferPagePool(bufferPagePool);
client.setWriteBuffer(1024 * 1024, 10);
AioSession session = client.start(asynchronousChannelGroup);
WriteBuffer outputStream = session.writeBuffer();
byte[] data = "smart-socket".getBytes();
while (true) {
int num = (int) (Math.random() * 10) + 1;
outputStream.writeInt(data.length * num);
while (num-- > 0) {
outputStream.write(data);
}
}
}
}
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
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
# 压测结果
[Quick Timer] INFO org.smartboot.socket.extension.plugins.MonitorPlugin -
-----5seconds ----
inflow: 2265.0299911499023(MB)
outflow: 2270.6433296203613(MB)
process fail: 0
process count: 33939250
process total: 135912618
read count: 194613 write count: 698880
connect count: 0
disconnect count: 0
online count: 10
connected total: 10
Requests/sec: 6787850.0
Transfer/sec: 453.0059982299805(MB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14