【问题标题】:is Eclipse/Californium CoAP observer much slower than Aiocoap observer?Eclipse/Californium CoAP 观察者是否比 Aiocoap 观察者慢得多?
【发布时间】:2022-09-25 15:01:42
【问题描述】:

我正在尝试构建一个系统,在该系统中我可以通过互联网将一些设备连接到服务器。 我想通过 CoAP (10-30FPS) 传输一些数据,帧大小 = 3KB。 首先,我使用了 Aiocoap,它可以发送高达 100FPS 但占用过多 CPU, 请求是 NON,在 Aiocoap 中的丢失率很低, 而 Eclipse/Californium 不能发送超过 3FPS, 当我使用更高的 FPS 时,我要么只收到每条消息的第一块,要么什么也没收到,大多数时候也没有订购。

我想知道这是否是 Californium 的真实性能,还是我以错误的方式使用它?

我将分享一些代码:

服务器.java

static class CoapObserverServer extends CoapResource {
                int i = -1;
                public CoapObserverServer() {
                        super(\"alarm\");
                        setObservable(true); // enable observing
                        setObserveType(Type.NON); // configure the notification type to CONs
                        getAttributes().setObservable(); // mark observable in the Link-Format

                        System.out.println(this);
                        // schedule a periodic update task, otherwise let events call changed()
                        //new Timer().schedule(new UpdateTask(), 0, 1000/2);
                }
                private class UpdateTask extends TimerTask {
                        @Override
                        public void run() {
                                changed(); // notify all observers
                        }
                }

                @Override
                public void handleGET(CoapExchange exchange) {
                        // the Max-Age value should match the update interval
                        exchange.setMaxAge(1);
                        //++i;
                        int leng = 2000;
                        String s = \"\" + i + \"-\" + fillString(\'X\', leng - 1 - Integer.toString(i).len>
                        exchange.respond(s);
                }
                public static String fillString(char fillChar, int count){
                        // creates a string of \'x\' repeating characters
                        char[] chars = new char[count];
                        while (count>0) chars[--count] = fillChar;
                        return new String(chars);
                }

                @Override
                public void handleDELETE(CoapExchange exchange) {
                        delete(); // will also call clearAndNotifyObserveRelations(ResponseCode.NOT_>
                        exchange.respond(ResponseCode.DELETED);
                }

                @Override
                public void handlePUT(CoapExchange exchange) {
                        exchange.accept();

                        int format = exchange.getRequestOptions().getContentFormat();
                        if (format == MediaTypeRegistry.TEXT_PLAIN) {
                                 // ...
                                        String plain = exchange.getRequestText();
                                try{
                                        i = Integer.valueOf(plain);
                                } catch(NumberFormatException ex){
                                        System.out.println(\"error converting string\"+ plain);
                                }
                                exchange.respond(ResponseCode.CHANGED);
                                changed(); // notify all observers
                        }
                }

观察者.java

private static final File CONFIG_FILE = new File(\"Californium3.properties\");
    private static final String CONFIG_HEADER = \"Californium CoAP Properties file for client\";
    private static final int DEFAULT_MAX_RESOURCE_SIZE = 2 * 1024 * 1024; // 2 MB
    private static final int DEFAULT_BLOCK_SIZE = 512;

    static {
        CoapConfig.register();
        UdpConfig.register();
    }

    private static DefinitionsProvider DEFAULTS = new DefinitionsProvider() {

        @Override
        public void applyDefinitions(Configuration config) {
            config.set(CoapConfig.MAX_RESOURCE_BODY_SIZE, DEFAULT_MAX_RESOURCE_SIZE);
            config.set(CoapConfig.MAX_MESSAGE_SIZE, DEFAULT_BLOCK_SIZE);
            config.set(CoapConfig.PREFERRED_BLOCK_SIZE, DEFAULT_BLOCK_SIZE);
        }
    };

    private static class AsynchListener implements CoapHandler {
        @Override
        public void onLoad(CoapResponse response) {
            System.out.println( response.getResponseText() );
        }

        @Override
        public void onError() {
            System.err.println(\"Error\");
        }
    }

    /*
     * Application entry point.
     */ 
    public static void main(String args[]) {
        Configuration config = Configuration.createWithFile(CONFIG_FILE, CONFIG_HEADER, DEFAULTS);
        Configuration.setStandard(config);

        URI uri = null; // URI parameter of the request
        if (args.length > 0) {

            // input URI from command line arguments
            try {
                uri = new URI(args[0]);
            } catch (URISyntaxException e) {
                System.err.println(\"Invalid URI: \" + e.getMessage());
                System.exit(-1);
            }

            CoapClient client = new CoapClient(uri);
            client.useNONs();
            // observe
            AsynchListener asynchListener = new AsynchListener();
            CoapObserveRelation observation = client.observe(asynchListener);
            
            // User presses ENTER to exit
            System.out.println(\"Press ENTER to exit...\");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try { br.readLine(); } catch (IOException e) { }
            System.out.println(\"Exiting...\");
            observation.proactiveCancel();
            
        } 

因此,我通过向具有计数器 0-50 的服务器发送 PUT 请求来控制 FPS。

    标签: java udp network-protocols coap californium


    【解决方案1】:

    不确定,你在做什么。 这似乎是有线的,与 RFC7252 或 RFC7641 无关。

    CoAP 是为 REST 设计的,我认为将它用于视频流没有任何好处。

    在具有 16GB RAM 的 Intel n6005 上使用 Eclipse/Californium,CoAP/DTLS 服务器以大约 60000 个请求/秒的速度运行。该基准测试并行使用 2000 个客户端。

    另见Eclipse/Californium - Benchmarks j5005

    仅使用一个带有 CON 请求的客户端,性能主要受 RTT 限制。如果 RTT 相应较小,那么每秒 30 个请求应该可以工作。

    使用 NON 请求并没有真正帮助。 CoAP RFC7252 定义了两个层,一个消息层和一个应用层。 NON 仅影响消息传递层,但如果应使用 NSTART-1,则 NON 请求将等待其响应。

    如果您的 RTT 是问题所在,您可以尝试使用“无服务器响应”(RFC7967) 或多个 NON 响应 (RFC7641) 的请求来逃避该问题。第一个不适用于快速请求,第二个更多是最初声明的变通方法,即 CoAP 是 REST 而不是视频流。

    那么,您的 RTT 是多少?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 2016-02-20
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多