X-Git-Url: http://git.alex.org.uk diff --git a/nbd-tester-client.c b/nbd-tester-client.c index f97a4ee..9f7714e 100644 --- a/nbd-tester-client.c +++ b/nbd-tester-client.c @@ -56,7 +56,36 @@ typedef enum { CONNECTION_CLOSE_FAST, } CLOSE_TYPE; -inline int read_all(int f, void *buf, size_t len) { +#define TEST_WRITE (1<<0) +#define TEST_FLUSH (1<<1) + +int timeval_subtract (struct timeval *result, struct timeval *x, + struct timeval *y) { + if (x->tv_usec < y->tv_usec) { + int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; + y->tv_usec -= 1000000 * nsec; + y->tv_sec += nsec; + } + + if (x->tv_usec - y->tv_usec > 1000000) { + int nsec = (x->tv_usec - y->tv_usec) / 1000000; + y->tv_usec += 1000000 * nsec; + y->tv_sec -= nsec; + } + + result->tv_sec = x->tv_sec - y->tv_sec; + result->tv_usec = x->tv_usec - y->tv_usec; + + return x->tv_sec < y->tv_sec; +} + +double timeval_diff_to_double (struct timeval * x, struct timeval * y) { + struct timeval r; + timeval_subtract(&r, x, y); + return r.tv_sec * 1.0 + r.tv_usec/1000000.0; +} + +static inline int read_all(int f, void *buf, size_t len) { ssize_t res; size_t retval=0; @@ -72,7 +101,29 @@ inline int read_all(int f, void *buf, size_t len) { return retval; } -int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype) { +static inline int write_all(int f, void *buf, size_t len) { + ssize_t res; + size_t retval=0; + + while(len>0) { + if((res=write(f, buf, len)) <=0) { + snprintf(errstr, errstr_len, "Write failed: %s", strerror(errno)); + return -1; + } + len-=res; + buf+=res; + retval+=res; + } + return retval; +} + +#define READ_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; } +#define READ_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((read_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; } + +#define WRITE_ALL_ERRCHK(f, buf, len, whereto, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); goto whereto; } +#define WRITE_ALL_ERR_RT(f, buf, len, whereto, rval, errmsg...) if((write_all(f, buf, len))<=0) { snprintf(errstr, errstr_len, ##errmsg); retval = rval; goto whereto; } + +int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE ctype, int* serverflags) { int sock; struct hostent *host; struct sockaddr_in addr; @@ -102,11 +153,7 @@ int setup_connection(gchar *hostname, int port, gchar* name, CONNECTION_TYPE cty } if(ctype1024) { - speed>>=10; + speed=speed/1024.0; speedchar[0]='K'; } if(speed>1024) { - speed>>=10; + speed=speed/1024.0; speedchar[0]='M'; } if(speed>1024) { - speed>>=10; + speed=speed/1024.0; speedchar[0]='G'; } - g_message("%d: Throughput test complete. Took %.3f seconds to complete, %d%siB/s", (int)getpid(), timespan,speed,speedchar); + g_message("%d: Throughput %s test complete. Took %.3f seconds to complete, %.3f%sib/s", (int)getpid(), (testflags & TEST_WRITE)?"write":"read", timespan, speed, speedchar); err_open: if(close_sock) { @@ -394,16 +483,17 @@ err: return retval; } -typedef int (*testfunc)(gchar*, int, char*, int, char, char); +typedef int (*testfunc)(gchar*, int, char*, int, char, char, int); int main(int argc, char**argv) { gchar *hostname; long int p = 0; char* name = NULL; int sock=0; - char c; + int c; bool want_port = TRUE; int nonopt=0; + int testflags=0; testfunc test = throughput_test; if(argc<3) { @@ -413,7 +503,7 @@ int main(int argc, char**argv) { exit(EXIT_FAILURE); } logging(); - while((c=getopt(argc, argv, "-N:o"))>=0) { + while((c=getopt(argc, argv, "-N:owf"))>=0) { switch(c) { case 1: switch(nonopt) { @@ -439,10 +529,16 @@ int main(int argc, char**argv) { case 'o': test=oversize_test; break; + case 'w': + testflags|=TEST_WRITE; + break; + case 'f': + testflags|=TEST_FLUSH; + break; } } - if(test(hostname, (int)p, name, sock, FALSE, TRUE)<0) { + if(test(hostname, (int)p, name, sock, FALSE, TRUE, testflags)<0) { g_warning("Could not run test: %s", errstr); exit(EXIT_FAILURE); }