remote_lat.cpp 3.33 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2012 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3
    Copyright (c) 2009-2011 250bpm s.r.o.
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

18
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
19 20 21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

22
#include "../include/zmq.h"
23
#include "../include/zmq_utils.h"
Martin Sustrik's avatar
Martin Sustrik committed
24 25
#include <stdio.h>
#include <stdlib.h>
26
#include <string.h>
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29 30 31

int main (int argc, char *argv [])
{
    const char *connect_to;
    int roundtrip_count;
32
    size_t message_size;
Martin Sustrik's avatar
Martin Sustrik committed
33 34 35 36
    void *ctx;
    void *s;
    int rc;
    int i;
37
    zmq_msg_t msg;
38 39
    void *watch;
    unsigned long elapsed;
Martin Sustrik's avatar
Martin Sustrik committed
40 41 42
    double latency;

    if (argc != 4) {
43 44
        printf ("usage: remote_lat <connect-to> <message-size> "
            "<roundtrip-count>\n");
Martin Sustrik's avatar
Martin Sustrik committed
45 46 47
        return 1;
    }
    connect_to = argv [1];
48 49
    message_size = atoi (argv [2]);
    roundtrip_count = atoi (argv [3]);
Martin Sustrik's avatar
Martin Sustrik committed
50

51
    ctx = zmq_init (1);
52 53 54 55
    if (!ctx) {
        printf ("error in zmq_init: %s\n", zmq_strerror (errno));
        return -1;
    }
Martin Sustrik's avatar
Martin Sustrik committed
56 57

    s = zmq_socket (ctx, ZMQ_REQ);
58 59 60 61
    if (!s) {
        printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
        return -1;
    }
Martin Sustrik's avatar
Martin Sustrik committed
62 63

    rc = zmq_connect (s, connect_to);
64 65 66 67
    if (rc != 0) {
        printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
        return -1;
    }
Martin Sustrik's avatar
Martin Sustrik committed
68

69
    rc = zmq_msg_init_size (&msg, message_size);
70 71 72 73
    if (rc != 0) {
        printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
        return -1;
    }
74 75
    memset (zmq_msg_data (&msg), 0, message_size);

76
    watch = zmq_stopwatch_start ();
77

Martin Sustrik's avatar
Martin Sustrik committed
78
    for (i = 0; i != roundtrip_count; i++) {
79 80 81
        rc = zmq_sendmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
82 83
            return -1;
        }
84 85 86
        rc = zmq_recvmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
87 88 89 90 91 92
            return -1;
        }
        if (zmq_msg_size (&msg) != message_size) {
            printf ("message of incorrect size received\n");
            return -1;
        }
Martin Sustrik's avatar
Martin Sustrik committed
93 94
    }

95
    elapsed = zmq_stopwatch_stop (watch);
96

97
    rc = zmq_msg_close (&msg);
98 99 100 101
    if (rc != 0) {
        printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
        return -1;
    }
102

103
    latency = (double) elapsed / (roundtrip_count * 2);
Martin Sustrik's avatar
Martin Sustrik committed
104 105 106

    printf ("message size: %d [B]\n", (int) message_size);
    printf ("roundtrip count: %d\n", (int) roundtrip_count);
107 108
    printf ("average latency: %.3f [us]\n", (double) latency);

109
    rc = zmq_close (s);
110 111 112 113
    if (rc != 0) {
        printf ("error in zmq_close: %s\n", zmq_strerror (errno));
        return -1;
    }
114

115
    rc = zmq_term (ctx);
116 117 118 119
    if (rc != 0) {
        printf ("error in zmq_term: %s\n", zmq_strerror (errno));
        return -1;
    }
Martin Sustrik's avatar
Martin Sustrik committed
120 121 122

    return 0;
}