crpcsunroundtripsunrpc

SUN RPC (ONC/RPC): Calculating round trip time (or pinging) using null procedure in C


How can I calculate or estimate the RTT (Round Trip Time) between client and server?

A tutorial or sample addressing this can also help.


Solution

  • Here what I do:

    #include <rpc/rpc.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/times.h>
    #include <fcntl.h>
    #include <time.h>
    
    int main(int argc, char *argv[]) {
    
        enum clnt_stat status;
        CLIENT *handle;
        struct timeval t;
        clock_t rtime;
        struct tms dumm;
        int count = 100000;
        int i;
        time_t now;
        char stamp[27];
        int programm;
        int version;
    
        if (argc != 4) {
            printf("Usage: rpcping <host> <program> <version>\n");
            exit(1);
        }
    
        /*
         *   Create Client Handle
         */
        programm = atoi(argv[2]);
        version = atoi(argv[3]);
        handle = clnt_create(argv[1], programm, version, "tcp");
        if (handle == NULL) {
            printf("clnt failed\n");
            exit(1);
        }
    
        /*
         *   use 30 seconds timeout
         */
        t.tv_sec = 30;
        t.tv_usec = 0;
    
        while (1) {
            rtime = times(&dumm);
            for (i = 0; i < count; i++) {
                status = clnt_call(handle, 0, (xdrproc_t) xdr_void,
                    NULL, (xdrproc_t) xdr_void, NULL, t);
    
                if (status == RPC_SUCCESS) { /* NOP */ }
            }
            now = time(NULL);
            ctime_r(&now, stamp);
            stamp[strlen(stamp) - 1] = '\0';
            fprintf(stdout, "[%s]: Speed:  %2.4fs.\n", stamp,
                count / ((double) (times(&dumm) - rtime) / (double) sysconf(_SC_CLK_TCK)));
            fflush(stdout);
        }
    
        clnt_destroy(handle);
    }
    

    I have a multithread version as well

    https://gist.github.com/2401404

    tigran.