xref: /dragonfly/test/sysperf/read1.c (revision 86d7f5d3)
1 /*
2  * read1.c
3  *
4  * Tests reading 1 byte at a time from a file.
5  *
6  * $DragonFly: src/test/sysperf/read1.c,v 1.1 2004/08/13 02:28:42 dillon Exp $
7  */
8 
9 #include "blib.h"
10 #include <errno.h>
11 #include <sys/resource.h>
12 #include <sys/fcntl.h>
13 
14 char Buf[8192];
15 
16 int
main(int ac,char ** av)17 main(int ac, char **av)
18 {
19     int bytes;
20     int fd;
21     int i;
22     int j;
23     char c;
24     char *ptr;
25     const char *filename;
26 
27     if (ac == 1) {
28 	fprintf(stderr, "%s filesize[k,m]\n", av[0]);
29 	exit(1);
30     }
31     bytes = strtol(av[1], &ptr, 0);
32     if (*ptr == 'k' || *ptr == 'K') {
33 	bytes *= 1024;
34     } else if (*ptr == 'm' || *ptr == 'M') {
35 	bytes *= 1024 * 1024;
36     } else if (*ptr) {
37 	fprintf(stderr, "Illegal numerical suffix: %s\n", ptr);
38 	exit(1);
39     }
40     if (bytes <= 0) {
41 	fprintf(stderr, "I can't handle %d sized buffers\n", bytes);
42 	exit(1);
43     }
44 
45     filename = "read1.dat";
46     fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
47     if (fd < 0) {
48 	if (errno == EROFS) {
49 	    filename = "/tmp/read1.dat";
50 	    fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
51 	}
52 	if (fd < 0) {
53 	    perror("open()");
54 	    exit(1);
55 	}
56     }
57     for (i = 0; i < bytes; i += sizeof(Buf)) {
58 	int n = (bytes - i > sizeof(Buf)) ? sizeof(Buf) : bytes - i;
59 	if (write(fd, Buf, n) != n) {
60 	    close(fd);
61 	    perror("write()");
62 	    remove(filename);
63 	    exit(1);
64 	}
65     }
66     fsync(fd);
67     fsync(fd);
68     sleep(1);
69     fsync(fd);
70     lseek(fd, 0L, 0);
71     sleep(1);
72 
73     start_timing();
74     i = 0;
75     while (stop_timing(0, NULL) == 0) {
76 	for (j = 0; j < 256 * 1024; ++j) {
77 	    if (read(fd, &c, 1) != 1)
78 		lseek(fd, 0L, 0);
79 	}
80 	i += j;
81     }
82     lseek(fd, 0L, 0);
83     start_timing();
84     for (j = 0; j < i; ++j) {
85 	if (read(fd, &c, 1) != 1)
86 	    lseek(fd, 0L, 0);
87     }
88     stop_timing(j, "read 1char from file:");
89     remove(filename);
90     return(0);
91 }
92 
93