Initial implementation of named exports
[nbd.git] / nbd-tester-client.c
index 9cc3aca..8572f29 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <glib.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/types.h>
 #include <sys/socket.h>
+#include <syslog.h>
+#include <unistd.h>
 #include "config.h"
 #include "lfs.h"
 #define MY_NAME "nbd-tester-client"
 #include "cliserv.h"
 
+#include <netinet/in.h>
+#include <glib.h>
+
 static gchar errstr[1024];
 const static int errstr_len=1024;
 
@@ -52,7 +55,7 @@ typedef enum {
 
 inline int read_all(int f, void *buf, size_t len) {
        ssize_t res;
-       int retval=0;
+       size_t retval=0;
 
        while(len>0) {
                if((res=read(f, buf, len)) <=0) {
@@ -176,12 +179,12 @@ int read_packet_check_header(int sock, size_t datasize, long long int curhandle)
        rep.magic=ntohl(rep.magic);
        rep.error=ntohl(rep.error);
        if(rep.magic!=NBD_REPLY_MAGIC) {
-               snprintf(errstr, errstr_len, "Received package with incorrect reply_magic. Index of sent packages is %lld (0x%llX), received handle is %lld (0x%llX). Received magic 0x%lX, expected 0x%lX", curhandle, curhandle, *((u64*)rep.handle), *((u64*)rep.handle), rep.magic, NBD_REPLY_MAGIC);
+               snprintf(errstr, errstr_len, "Received package with incorrect reply_magic. Index of sent packages is %lld (0x%llX), received handle is %lld (0x%llX). Received magic 0x%lX, expected 0x%lX", curhandle, curhandle, *((u64*)rep.handle), *((u64*)rep.handle), (long unsigned int)rep.magic, (long unsigned int)NBD_REPLY_MAGIC);
                retval=-1;
                goto end;
        }
        if(rep.error) {
-               snprintf(errstr, errstr_len, "Received error from server: %ld (0x%lX). Handle is %lld (0x%llX).", rep.error, *((u64*)rep.handle), *((u64*)rep.handle));
+               snprintf(errstr, errstr_len, "Received error from server: %ld (0x%lX). Handle is %lld (0x%llX).", (long int)rep.error, (long unsigned int)rep.error, (long long int)(*((u64*)rep.handle)), *((u64*)rep.handle));
                retval=-1;
                goto end;
        }
@@ -199,10 +202,17 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
        int requests=0;
        fd_set set;
        struct timeval tv;
+       struct timeval start;
+       struct timeval stop;
+       float timespan;
+       int speed;
+       char speedchar[2] = { '\0', '\0' };
        int retval=0;
        size_t tmp;
        signed int do_write=TRUE;
+       pid_t mypid = getpid();
 
+       size=0;
        if(!sock_is_open) {
                if((sock=setup_connection(hostname, port, CONNECTION_TYPE_CLISERV))<0) {
                        g_warning("Could not open socket: %s", errstr);
@@ -214,7 +224,6 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
                 * this way, but, well. */
                size=4096;
        }
-       size=0;
        if((tmp=read_all(sock, &size, sizeof(u64)))<0) {
                retval=-1;
                snprintf(errstr, errstr_len, "Could not read from socket: %s", strerror(errno));
@@ -230,12 +239,17 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
        req.magic=htonl(NBD_REQUEST_MAGIC);
        req.type=htonl(NBD_CMD_READ);
        req.len=htonl(1024);
+       if(gettimeofday(&start, NULL)<0) {
+               retval=-1;
+               snprintf(errstr, errstr_len, "Could not measure start time: %s", strerror(errno));
+               goto err_open;
+       }
        for(i=0;i+1024<=size;i+=1024) {
                if(do_write) {
-                       *((u64*)req.handle)=i;
+                       memcpy(&(req.handle),&i,sizeof(i));
                        req.from=htonll(i);
                        write(sock, &req, sizeof(req));
-                       printf("Requests(+): %d\n", ++requests);
+                       printf("%d: Requests(+): %d\n", (int)mypid, ++requests);
                }
                do {
                        FD_ZERO(&set);
@@ -250,7 +264,7 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
                                        retval=-1;
                                        goto err_open;
                                }
-                               printf("Requests(-): %d\n", --requests);
+                               printf("%d: Requests(-): %d\n", (int)mypid, --requests);
                        }
                } while FD_ISSET(sock, &set);
                /* Now wait until we can write again or until a second have
@@ -278,9 +292,29 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
                        /* Okay, there's something ready for
                         * reading here */
                        read_packet_check_header(sock, 1024, i);
-                       printf("Requests(-): %d\n", --requests);
+                       printf("%d: Requests(-): %d\n", (int)mypid, --requests);
                }
        } while (requests);
+       if(gettimeofday(&stop, NULL)<0) {
+               retval=-1;
+               snprintf(errstr, errstr_len, "Could not measure end time: %s", strerror(errno));
+               goto err_open;
+       }
+       timespan=(float)(stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec))/(float)1000000;
+       speed=(int)(size/timespan);
+       if(speed>1024) {
+               speed>>=10;
+               speedchar[0]='K';
+       }
+       if(speed>1024) {
+               speed>>=10;
+               speedchar[0]='M';
+       }
+       if(speed>1024) {
+               speed>>=10;
+               speedchar[0]='G';
+       }
+       g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%sB/s", (int)getpid(), timespan,speed,speedchar);
 
 err_open:
        if(close_sock) {
@@ -294,11 +328,11 @@ int main(int argc, char**argv) {
        gchar *hostname;
        long int p;
        int port;
-       int sock;
+       int sock=0;
 
        if(argc<3) {
-               g_message("Not enough arguments");
-               g_message("Usage: %s <hostname> <port>", argv[0]);
+               g_message("%d: Not enough arguments", (int)getpid());
+               g_message("%d: Usage: %s <hostname> <port>", (int)getpid(), argv[0]);
                exit(EXIT_FAILURE);
        }
        logging();