r229: Turn this into a slightly better Makefile, as suggested by Mike Frysinger
[nbd.git] / nbd-tester-client.c
index 719c439..3266135 100644 (file)
@@ -29,6 +29,8 @@
 #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"
@@ -52,6 +54,8 @@ typedef enum {
 
 inline int read_all(int f, void *buf, size_t len) {
        ssize_t res;
+       size_t retval=0;
+
        while(len>0) {
                if((res=read(f, buf, len)) <=0) {
                        snprintf(errstr, errstr_len, "Read failed: %s", strerror(errno));
@@ -59,7 +63,9 @@ inline int read_all(int f, void *buf, size_t len) {
                }
                len-=res;
                buf+=res;
+               retval+=res;
        }
+       return retval;
 }
 
 int setup_connection(gchar *hostname, int port, CONNECTION_TYPE ctype) {
@@ -195,10 +201,16 @@ 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;
 
+       size=0;
        if(!sock_is_open) {
                if((sock=setup_connection(hostname, port, CONNECTION_TYPE_CLISERV))<0) {
                        g_warning("Could not open socket: %s", errstr);
@@ -210,26 +222,30 @@ 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));
                goto err_open;
        }
-       read_all(sock,&buf,128);
        if(tmp==0) {
                retval=-1;
                snprintf(errstr, errstr_len, "Server closed connection unexpectedly when trying to read size of device in throughput test");
                goto err;
        }
+       read_all(sock,&buf,128);
        size=ntohll(size);
        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;
-                       req.from=htonl(i);
+                       req.from=htonll(i);
                        write(sock, &req, sizeof(req));
                        printf("Requests(+): %d\n", ++requests);
                }
@@ -242,7 +258,10 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
                        if(FD_ISSET(sock, &set)) {
                                /* Okay, there's something ready for
                                 * reading here */
-                               read_packet_check_header(sock, 1024, i);
+                               if(read_packet_check_header(sock, 1024, i)<0) {
+                                       retval=-1;
+                                       goto err_open;
+                               }
                                printf("Requests(-): %d\n", --requests);
                        }
                } while FD_ISSET(sock, &set);
@@ -274,6 +293,26 @@ int throughput_test(gchar* hostname, int port, int sock, char sock_is_open, char
                        printf("Requests(-): %d\n", --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=stop.tv_sec-start.tv_sec+(stop.tv_usec-start.tv_usec)/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("Throughput test complete. Took %.3f seconds to complete, %d%sB/s",timespan,speed,speedchar);
 
 err_open:
        if(close_sock) {