]> mj.ucw.cz Git - libucw.git/blob - debug/sorter/file-test.c
c1672d298ea070d0212675b8d4c3d1f95f730db3
[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 #include "lib/lfs.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12
13 int main(int argc, char **argv)
14 {
15   ASSERT(argc == 4);
16   uns files = atol(argv[1]);
17   uns bufsize = atol(argv[2]) * 1024;                           // Kbytes
18   uns rounds = (u64)atol(argv[3]) * 1024*1024 / bufsize;        // Mbytes
19   int fd[files];
20   byte *buf[files];
21
22   log(L_INFO, "Initializing");
23   for (uns i=0; i<files; i++)
24     {
25       byte name[16];
26       sprintf(name, "tmp/ft-%d", i);
27       fd[i] = sh_open(name, O_RDWR | O_CREAT | O_TRUNC, 0666);
28       if (fd[i] < 0)
29         die("Cannot create %s: %m", name);
30       buf[i] = big_alloc(bufsize);
31     }
32   sync();
33
34   log(L_INFO, "Writing %d files in parallel with %d byte buffers", files, bufsize);
35   init_timer();
36   u64 total = 0, total_rep = 0;
37   for (uns r=0; r<rounds; r++)
38     {
39       for (uns i=0; i<files; i++)
40         {
41           for (uns j=0; j<bufsize; j++)
42             buf[i][j] = r+i+j;
43           uns c = write(fd[i], buf[i], bufsize);
44           ASSERT(c == bufsize);
45           total += c;
46           if (total >= total_rep + 1024*1024*1024)
47             {
48               printf("Wrote %d MB (round %d of %d)\r", (int)(total >> 20), r, rounds);
49               fflush(stdout);
50               total_rep = total;
51             }
52         }
53     }
54   log(L_INFO, "Syncing");
55   sync();
56   uns ms = get_timer();
57   log(L_INFO, "Spent %dms (%d MB/sec)", ms, (uns)(total/ms*1000/1048576));
58
59   log(L_INFO, "Reading the files sequentially");
60   total = total_rep = 0;
61   for (uns i=0; i<files; i++)
62     {
63       lseek(fd[i], 0, SEEK_SET);
64       for (uns r=0; r<rounds; r++)
65         {
66           uns c = read(fd[i], buf[i], bufsize);
67           ASSERT(c == bufsize);
68           total += c;
69           if (total >= total_rep + 1024*1024*1024)
70             {
71               printf("Read %d MB (file %d)\r", (int)(total >> 20), i);
72               fflush(stdout);
73               total_rep = total;
74             }
75         }
76       close(fd[i]);
77     }
78   ms = get_timer();
79   log(L_INFO, "Spent %dms (%d MB/sec)", ms, (uns)(total/ms*1000/1048576));
80
81   log(L_INFO, "Done");
82   return 0;
83 }