From: Michael Vrable Date: Fri, 29 Jan 2010 22:20:41 +0000 (-0800) Subject: Work on a parallel read benchmark. X-Git-Url: https://git.vrable.net/?a=commitdiff_plain;h=9882dff622103ee844eb1af5608ac48157ecfabb;p=bluesky.git Work on a parallel read benchmark. --- diff --git a/microbench/readbench.c b/microbench/readbench.c new file mode 100644 index 0000000..88eb888 --- /dev/null +++ b/microbench/readbench.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +struct thread_state { + pthread_t thread; + int thread_num; + long long timestamp; +}; + +#define MAX_THREADS 128 +struct thread_state threads[MAX_THREADS]; + +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; +} + +#if 0 +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); + } +} +#endif + +void *benchmark_thread(void *arg) +{ + struct thread_state *ts = (struct thread_state *)arg; + + char namebuf[64]; + sprintf(namebuf, "file-%d", ts->thread_num); + printf("Opening %s\n", namebuf); + + int64_t start, end; + + start = now_hires(); + FILE *f = fopen(namebuf, "rb"); + if (f == NULL) { + perror("fopen"); + return NULL; + } + + char buf[4096]; + fread(buf, 1, sizeof(buf), f); + + end = now_hires(); + printf("Thread %d: Time = %"PRIi64"\n", ts->thread_num, end - start); + + fclose(f); + + return NULL; +} + +void launch_thread(int i) +{ + threads[i].thread_num = i; + printf("Launching thread %d...\n", i); + if (pthread_create(&threads[i].thread, NULL, benchmark_thread, &threads[i]) != 0) { + fprintf(stderr, "Error launching thread!\n"); + exit(1); + } +} + +void wait_thread(int n) +{ + void *result; + pthread_join(threads[n].thread, &result); +} + +int main(int argc, char *argv[]) +{ + for (int i = 0; i < 8; i++) { + launch_thread(i); + } + + for (int i = 0; i < 8; i++) { + wait_thread(i); + } + + return 0; +}