]> mj.ucw.cz Git - libucw.git/blob - debug/sorter/file-test.c
Added a simple experiment with parallel reads/writes.
[libucw.git] / debug / sorter / file-test.c
1 /*
2  *  An experiment with parallel reading and writing of files.
3  */
4
5 #include "lib/lib.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11
12 int main(int argc, char **argv)
13 {
14   ASSERT(argc == 4);
15   uns files = atol(argv[1]);
16   uns bufsize = atol(argv[2]);
17   uns rounds = atol(argv[3]);
18   int fd[files];
19   byte *buf[files];
20
21   log(L_INFO, "Initializing");
22   for (uns i=0; i<files; i++)
23     {
24       byte name[16];
25       sprintf(name, "tmp/ft-%d", i);
26       fd[i] = open(name, O_RDWR | O_CREAT | O_TRUNC, 0666);
27       if (fd[i] < 0)
28         die("Cannot create %s: %m", name);
29       buf[i] = big_alloc(bufsize);
30     }
31   sync();
32
33   log(L_INFO, "Writing %d files in parallel with %d byte buffers", files, bufsize);
34   for (uns r=0; r<rounds; r++)
35     {
36       log(L_INFO, "\tRound %d", r);
37       for (uns i=0; i<files; i++)
38         {
39           for (uns j=0; j<bufsize; j++)
40             buf[i][j] = r+i+j;
41           uns c = write(fd[i], buf[i], bufsize);
42           ASSERT(c == bufsize);
43         }
44     }
45   log(L_INFO, "Syncing");
46   sync();
47
48   log(L_INFO, "Reading the files sequentially");
49   for (uns i=0; i<files; i++)
50     {
51       lseek(fd[i], 0, SEEK_SET);
52       for (uns r=0; r<rounds; r++)
53         {
54           uns c = read(fd[i], buf[i], bufsize);
55           ASSERT(c == bufsize);
56         }
57       close(fd[i]);
58     }
59
60   log(L_INFO, "Done");
61   return 0;
62 }