Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / microbench / bench.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <inttypes.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <time.h>
8
9 int64_t now_hires()
10 {
11     struct timespec time;
12
13     if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
14         perror("clock_gettime");
15         return 0;
16     }
17
18     return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
19 }
20
21 void bench_write()
22 {
23     FILE *f = fopen("writetest", "wb");
24     if (f == NULL) {
25         perror("fopen");
26         return;
27     }
28
29     char buf[4096];
30     for (int i = 0; i < 16; i++) {
31         int64_t start, end;
32         start = now_hires();
33         rewind(f);
34         fwrite(buf, 1, sizeof(buf), f);
35         fflush(f);
36         end = now_hires();
37         printf("Pass %d: Time = %"PRIi64"\n", i, end - start);
38     }
39 }
40
41 int main(int argc, char *argv[])
42 {
43     bench_write();
44 }