Search in sources :

Example 81 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class InprocLat method main.

public static void main(String[] argv) throws Exception {
    if (argv.length != 2) {
        printf("usage: inproc_lat <message-size> <roundtrip-count>\n");
        return;
    }
    int messageSize = atoi(argv[0]);
    int roundtripCount = atoi(argv[1]);
    Ctx ctx = ZMQ.init(1);
    if (ctx == null) {
        printf("error in init:");
        return;
    }
    SocketBase s = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    if (s == null) {
        printf("error in socket: ");
        return;
    }
    boolean rc = ZMQ.bind(s, "inproc://lat_test");
    if (!rc) {
        printf("error in bind: ");
        return;
    }
    Thread localThread = new Thread(new Worker(ctx, roundtripCount));
    localThread.start();
    Msg smsg = ZMQ.msgInitWithSize(messageSize);
    printf("message size: %d [B]\n", (int) messageSize);
    printf("roundtrip count: %d\n", (int) roundtripCount);
    long watch = ZMQ.startStopwatch();
    for (int i = 0; i != roundtripCount; i++) {
        int r = ZMQ.sendMsg(s, smsg, 0);
        if (r < 0) {
            printf("error in sendmsg: %s\n");
            return;
        }
        Msg msg = ZMQ.recvMsg(s, 0);
        if (msg == null) {
            printf("error in recvmsg: %s\n");
            return;
        }
        if (ZMQ.msgSize(msg) != messageSize) {
            printf("message of incorrect size received\n");
            return;
        }
    }
    long elapsed = ZMQ.stopStopwatch(watch);
    double latency = (double) elapsed / (roundtripCount * 2);
    localThread.join();
    printf("average latency: %.3f [us]\n", (double) latency);
    ZMQ.close(s);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx)

Aggregations

Ctx (zmq.Ctx)81 Test (org.junit.Test)73 SocketBase (zmq.SocketBase)63 Msg (zmq.Msg)31 AbstractSpecTest (zmq.socket.AbstractSpecTest)16 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 Ignore (org.junit.Ignore)4 ExecutorService (java.util.concurrent.ExecutorService)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Curve (zmq.io.mechanism.curve.Curve)2 InputStream (java.io.InputStream)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 Selector (java.nio.channels.Selector)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1