added some tools from ronnie sahlberg
[tridge/junkcode.git] / pipespeed.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <errno.h>
5 #include <sys/time.h>
6
7 #define BUFSIZE 0x8000
8
9
10 struct timeval tp1,tp2;
11
12 static void start_timer()
13 {
14         gettimeofday(&tp1,NULL);
15 }
16
17 static double end_timer()
18 {
19         gettimeofday(&tp2,NULL);
20         return((tp2.tv_sec - tp1.tv_sec) + 
21                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
22 }
23
24 int main(int argc, char *argv[])
25 {
26         int fdpair[2];
27         int fd, size, i=0;
28         char buf[BUFSIZE];
29
30         if (argc < 2) {
31                 printf("Usage: pipespeed megabytes\n");
32                 exit(1);
33         }
34
35         memset(buf,'Z',BUFSIZE);
36
37         size = atoi(argv[1]) * 0x100000;
38
39         fd = open("/dev/null", O_WRONLY);
40         if (fd == -1) {
41                 perror("open");
42                 exit(1);
43         }
44
45         if (pipe(fdpair) != 0) {
46                 perror("pipe");
47                 exit(1);
48         }
49
50         if (fork() == 0) {
51                 close(fdpair[1]);
52                 while (i<size) {
53                         int n = read(fdpair[0], buf, BUFSIZE);
54                         if (n <= 0) exit(1);
55                         write(fd, buf, n);
56                         i += n;
57                 }
58                 exit(0);
59         }
60
61         close(fdpair[0]);
62
63         start_timer();
64
65         while (i<size) {
66                 int n = write(fdpair[1], buf, BUFSIZE);
67                 if (n <= 0) {
68                         printf("pipe write error\n");
69                         exit(1);
70                 }
71                 i += n;
72         }
73
74         printf("%g MB/sec\n", (i/(0x100000))/end_timer());
75
76         return 0;
77 }