All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.dogp.sample.DOGPTestClient Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.dogp.sample;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.dogp.client.NettyDOGPUDPClientChannel;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.endpoint.EndpointFactory;
import org.bdware.doip.endpoint.client.ClientConfig;
import org.bdware.doip.endpoint.client.DoipClientImpl;
import org.bdware.doip.endpoint.client.DoipMessageCallback;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

public class DOGPTestClient {
    private final int port;
    private final String id;
    private final String ip;
    DoipClientImpl client;
    static Logger LOGGER = LogManager.getLogger(DOGPTestClient.class);

    public DOGPTestClient(String ip, int port, String id) {
        this.ip = ip;
        this.port = port;
        this.id = id;
        LOGGER.info(String.format("client configuration, ip:%s port:%d id:%s", ip, port, id));
    }

    public void init() {
        ClientConfig config;
        config = ClientConfig.fromUrl(String.format("udpdogp://%s:%d", ip, port));
        client = new DoipClientImpl();
        EndpointFactory.addClientChannel("udpdogp", new NettyDOGPUDPClientChannel());
        client.connect(config);
    }

    public static void main(String[] args) {
        String ip = "127.0.0.1";
        int port = 21046;
        String id = "defaulttest/id/1";
        if (args.length > 0) {
            String[] ipAndPort = args[0].split(":");
            ip = ipAndPort[0];
            port = Integer.valueOf(ipAndPort[1]);
        }
        if (args.length > 1)
            id = args[1];
        DOGPTestClient client1 = new DOGPTestClient(ip, port, id);
        client1.init();
        client1.sendMsg(5000000);
        //  client1.sendMsg();
    }

    ExecutorService es = Executors.newFixedThreadPool(20, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });

    public void sendMsg(long total) {
        long start = System.currentTimeMillis();
        AtomicLong i = new AtomicLong(0);
        int threadCount = 4;
        for (int j = 0; j < threadCount; j++)
            es.execute(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < total / threadCount; j++) {
                        client.hello(id, new DoipMessageCallback() {
                            @Override
                            public void onResult(DoipMessage msg) {
//                    LOGGER.info(String.format("header:0x%x doid:%s operation:%s req:%d bodyDataLen:%d bd:%s",
                                //                          msg.header.getFlag(), msg.header.parameters.id, msg.header.parameters.operation,
                                //                        msg.requestID, msg.body.encodedData.length, msg.body.encodedData.length > 0 ? msg.body.getDataAsJsonString() : "-empty-"));
                                i.incrementAndGet();
                            }
                        });
                    }
                }
            });

        long previsPrint = System.currentTimeMillis();
        for (; i.get() < total; ) {
            Thread.yield();
            long now = System.currentTimeMillis();
            if (now - previsPrint > 5000) {
                long dur = System.currentTimeMillis() - start;
                LOGGER.info("recv:" + i.get() + " dur:" + dur + " qps: " + (i.get() * 1000L / dur));
                previsPrint = System.currentTimeMillis();
            }
        }
        long dur = System.currentTimeMillis() - start;
        LOGGER.info("dur:" + dur + " recv:" + i.get() + " qps: " + (total * 1000L / dur));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy