remote_lat.cpp 3.24 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
8 9 10 11 12 13
    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
14
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
15

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

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

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

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

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

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

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

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

74
    watch = zmq_stopwatch_start ();
75

Martin Sustrik's avatar
Martin Sustrik committed
76
    for (i = 0; i != roundtrip_count; i++) {
77 78 79
        rc = zmq_sendmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
80 81
            return -1;
        }
82 83 84
        rc = zmq_recvmsg (s, &msg, 0);
        if (rc < 0) {
            printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno));
85 86 87 88 89 90
            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
91 92
    }

93
    elapsed = zmq_stopwatch_stop (watch);
94

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

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

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

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

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

    return 0;
}