smartboot 开源组织 smartboot 开源组织
首页
  • smart-socket
  • smart-http
  • smart-servlet
  • smart-mqtt
  • smart-license
  • feat
❤️开源捐赠
💰付费服务
🏠加入社区
  • Gitee (opens new window)
  • Github (opens new window)
首页
  • smart-socket
  • smart-http
  • smart-servlet
  • smart-mqtt
  • smart-license
  • feat
❤️开源捐赠
💰付费服务
🏠加入社区
  • Gitee (opens new window)
  • Github (opens new window)
  • smart-socket 首页
  • 概要

    • 关于 smart-socket
    • 我们的用户
  • 快速上手

    • 🚩五分钟上手
    • 🚩通信协议
    • 性能压测
      • 引入Maven依赖
      • 启动服务端
      • 启动客户端
      • 压测结果
    • 新手常见问题
  • 高级进阶

    • 1.状态机
    • 2.服务端绑定网卡
  • 插件

    • 1. 关于插件💬
    • 2. 心跳插件
    • 2. 闲置超时插件
    • 3. 通信调参插件
    • 4. 黑名单插件🛡
    • 5. 加密通信插件🛡
    • 6. 流量防控插件🛡
    • 7. 码流监测插件🛡
    • 8. proxy-protocol插件
  • 公众号

    • 💰单机百万长连接背后的故事
    • 💰揭秘百万长连接背后的黑科技
    • 💰让通信数据无所遁形
    • 性能分析的一柄利刃
    • 💰smart-socket的那点事之内存池
  • smart-socket
  • 快速上手
三刀
2022-10-29
目录

性能压测

# 引入Maven依赖

<dependency>
    <groupId>io.github.smartboot.socket</groupId>
    <artifactId>aio-pro</artifactId>
    <version>1.5.52</version>
</dependency>
1
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

# 启动客户端

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

# 压测结果

[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
🚩通信协议
新手常见问题

← 🚩通信协议 新手常见问题→

Theme by Vdoing | Copyright © 2017-2025 三刀
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式