Quick tool for benchmarking write latency.
authorMichael Vrable <mvrable@cs.ucsd.edu>
Mon, 11 Jan 2010 22:50:10 +0000 (14:50 -0800)
committerMichael Vrable <mvrable@cs.ucsd.edu>
Mon, 11 Jan 2010 22:50:10 +0000 (14:50 -0800)
microbench/bench.c [new file with mode: 0644]

diff --git a/microbench/bench.c b/microbench/bench.c
new file mode 100644 (file)
index 0000000..e0adab8
--- /dev/null
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <errno.h>
+#include <time.h>
+
+int64_t now_hires()
+{
+    struct timespec time;
+
+    if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
+        perror("clock_gettime");
+        return 0;
+    }
+
+    return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
+}
+
+void bench_write()
+{
+    FILE *f = fopen("writetest", "wb");
+    if (f == NULL) {
+        perror("fopen");
+        return;
+    }
+
+    char buf[4096];
+    for (int i = 0; i < 16; i++) {
+        int64_t start, end;
+        start = now_hires();
+        rewind(f);
+        fwrite(buf, 1, sizeof(buf), f);
+        fflush(f);
+        end = now_hires();
+        printf("Pass %d: Time = %"PRIi64"\n", i, end - start);
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    bench_write();
+}