[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[Nbd] [PATCH 3 of 3] Fix timing calculations in test suite



There are various problems with the timing calculations in the throughput
test. Firstly it was subtracting tv_secs and tv_usecs and simply adding
the result together. This caused a negative speed when tv_secs wrapped.
Secondly it doesn't cope well with a wrapping tv_secs. Thirdly, the rounding
is poor as calculations are performed on integers, with floor rounding.
This patch makes the calculation more accurate, and displays throughput to
three decimal places.

Also at:
http://www.alex.org.uk/nbd-testsuite-fix-timing.patch

--
Alex Bligh

Signed-Off-By: Alex Bligh <alex@...872...>
commit 17e57167ef20f39598cbaaaa12472be7469fe756
Author: Alex Bligh <alex@...872...>
Date:   Tue May 17 09:28:36 2011 +0100

Improve timing of tests; avoid wrapping tv_usec and tv_sec problems; better rounding; display speed with decimal places

diff --git a/nbd-tester-client.c b/nbd-tester-client.c
index 1ead4f1..7c1cb75 100644
--- a/nbd-tester-client.c
+++ b/nbd-tester-client.c
@@ -56,6 +56,32 @@ typedef enum {
	CONNECTION_CLOSE_FAST,
} CLOSE_TYPE;

+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;
@@ -311,8 +337,8 @@ int throughput_test(gchar* hostname, int port, char* name, int sock,
	struct timeval tv;
	struct timeval start;
	struct timeval stop;
-	float timespan;
-	int speed;
+	double timespan;
+	double speed;
	char speedchar[2] = { '\0', '\0' };
	int retval=0;
	size_t tmp;
@@ -401,21 +427,21 @@ int throughput_test(gchar* hostname, int port, char* name, int sock, 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);
+	timespan=timeval_diff_to_double(&stop, &start);
+	speed=size/timespan;
	if(speed>1024) {
-		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 %s test complete. Took %.3f seconds to complete, %d%siB/s", (int)getpid(), write?"write":"read", timespan, speed, speedchar); + g_message("%d: Throughput %s test complete. Took %.3f seconds to complete, %.3f%sib/s", (int)getpid(), write?"write":"read", timespan, speed, speedchar);

err_open:
	if(close_sock) {
diff --git a/simple_test b/simple_test
index fcb402f..53efb07 100755
--- a/simple_test
+++ b/simple_test
@@ -6,7 +6,7 @@ tmpnam=`mktemp`
ulimit -c unlimited

# Create a one-meg device
-dd if=/dev/zero of=$tmpnam bs=1024 count=4096
+dd if=/dev/zero of=$tmpnam bs=1024 count=4096 >/dev/null 2>&1

echo $1





Reply to: