-
Notifications
You must be signed in to change notification settings - Fork 1
/
otest.c
61 lines (54 loc) · 1.22 KB
/
otest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* simple open/read/close test
*
* usage: ./otest [count] [str]
*
* Wait for the file /tmp/o-test to be created, then open, read and close
* /tmp/x/o-file 'count' times. 'str' is just for tagging output
*
* compile with: cc -O2 -Wall -o ./otest otest.c
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#define SYNCFILE "/tmp/o-test"
#define RDFILE "/tmp/x/o-file"
int main(int argc, char *argv[])
{
struct timeval t1, t2;
long i, t, n, fd;
const char *tag;
char buf[64];
t = (argc >= 2)? atoi(argv[1]) : 0;
n = t? t: 1000000;
tag = (argc == 3)? argv[2] : "-";
while (1) {
fd = open(SYNCFILE, O_RDONLY);
if (fd >= 0)
break;
}
close(fd);
gettimeofday(&t1, NULL);
for (i = 0; i < n; i++) {
fd = open(RDFILE, O_RDONLY);
if (fd < 0)
goto fail;
t = read(fd, buf, sizeof(buf));
if (t < 0)
goto fail;
close(fd);
}
gettimeofday(&t2, NULL);
printf("%s %ld open/read/close in %.3fus\n", tag, n,
((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec))/1.0e6);
return 0;
fail:
printf("Failed with %d: %s!\n", errno, strerror(errno));
return 1;
}