From 5802eb0f5d931d25a7c8f486aaf1398df924f98a Mon Sep 17 00:00:00 2001
From: Alberto Bertogli <albertito@blitiri.com.ar>
Date: Fri, 3 Apr 2009 01:31:00 -0300
Subject: [PATCH 09/38] tests/performance: Add a random writes test

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
---
 .gitignore                                    |    1 +
 tests/performance/Makefile                    |    6 +++++-
 tests/performance/{performance.c => random.c} |   11 +++++++----
 3 files changed, 13 insertions(+), 5 deletions(-)
 copy tests/performance/{performance.c => random.c} (90%)

diff --git a/.gitignore b/.gitignore
index 2f09c27..2a82b46 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,5 +8,6 @@ samples/jio1
 samples/jio2
 samples/jio3
 tests/performance/performance
+tests/performance/random
 *.pyc
 *.pyo
diff --git a/tests/performance/Makefile b/tests/performance/Makefile
index 11ddd33..8dae8a5 100644
--- a/tests/performance/Makefile
+++ b/tests/performance/Makefile
@@ -5,16 +5,20 @@ LIBS = -ljio
 
 default: all
 
-all: performance
+all: performance random
 
 performance: performance.o
 	$(CC) $(LIBS) performance.o -o performance
 
+random: random.o
+	$(CC) $(LIBS) random.o -o random
+
 .c.o:
 	$(CC) $(CFLAGS) -c $< -o $@
 
 clean:
 	rm -f performance.o performance
+	rm -f random.o random
 	rm -f *.bb *.bbg *.da *.gcov gmon.out
 	rm -f test_file
 	rm -rf .test_file.jio
diff --git a/tests/performance/performance.c b/tests/performance/random.c
similarity index 90%
copy from tests/performance/performance.c
copy to tests/performance/random.c
index 849cd60..4af4bed 100644
--- a/tests/performance/performance.c
+++ b/tests/performance/random.c
@@ -1,6 +1,6 @@
 
 /*
- * performance.c - A program to test speed of parallel writes using libjio.
+ * random.c - A program to test speed of random writes using libjio.
  * Alberto Bertogli (albertito@blitiri.com.ar)
  *
  * It creates a big file, extends it using truncate, and forks N threads which
@@ -39,7 +39,7 @@ static void *worker(void *tno)
 	void *buf;
 	unsigned long tid;
 	ssize_t work_done, rv;
-	off_t localoffset;
+	off_t localoffset, offset;
 	long secs, usecs;
 	double seconds, mb_per_sec;
 	struct timeval tv1, tv2;
@@ -56,10 +56,13 @@ static void *worker(void *tno)
 
 	work_done = 0;
 
+	srandom(time(NULL));
+
 	gettimeofday(&tv1, NULL);
 
 	while (work_done < towrite) {
-		rv = jpwrite(&fs, buf, blocksize, localoffset + work_done);
+		offset = random() % (towrite - blocksize);
+		rv = jpwrite(&fs, buf, blocksize, localoffset + offset);
 		if (rv != blocksize) {
 			perror("jpwrite()");
 			break;
@@ -130,7 +133,7 @@ int main(int argc, char **argv)
 	if (ckres.total != 0) {
 		fprintf(stderr, "There were %d errors during the test\n",
 				ckres.total);
-		fprintf(stderr, "jfsck() was used to fix them, but that");
+		fprintf(stderr, "jfsck() was used to fix them, but that ");
 		fprintf(stderr, "shouldn't happen.\n");
 		return 1;
 	}
-- 
1.6.2.2.646.gb214

