/alps/fwbench

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/fwbench
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
1
#define _GNU_SOURCE
2
7 by Suren A. Chilingaryan
seqreader
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <sys/types.h>
6
#include <sys/stat.h>
7
#include <sys/time.h>
8
#include <unistd.h>
9
#include <dirent.h>
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
10
#include <fcntl.h>
11
#include <string.h>
12
#include <errno.h>
13
14
#define BUFSIZE 1048576//65536
15
#define BLOCK_SIZE 16384//16384
16
#define WRITE_INTERVAL 1
17
7 by Suren A. Chilingaryan
seqreader
18
19
int main(int argc, char *argv[]) {
20
    int err;
21
    size_t SKIP = 1;
22
    DIR *dir;
23
    struct dirent *ent;
24
    struct timeval start, tv;
25
    size_t us;
26
    size_t files = 0;
27
    size_t total_size = 0;
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
28
    size_t last_write = 0;
7 by Suren A. Chilingaryan
seqreader
29
    size_t skip;
30
    size_t run;
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
31
    char buffer[BUFSIZE];
7 by Suren A. Chilingaryan
seqreader
32
    
33
    if (argc < 2) {
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
34
	printf("Usage: %s <directory|device> [skip]\n", argv[0]);
7 by Suren A. Chilingaryan
seqreader
35
	exit(0);
36
    }
37
    
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
38
    if (!strstr(argv[0], "/dev/")) {
39
	int fd = open(argv[1], O_RDONLY|O_NOATIME|O_LARGEFILE/*|O_DIRECT*/, 0);
40
	if (fd < 0) {
41
	    printf("Unable to open device %s\n", argv[1]);
42
	    exit(1);
43
	}
44
	
45
	int size = BLOCK_SIZE;
46
	
47
	gettimeofday(&start, NULL);
48
        
49
        err = read(fd, buffer, size);
50
	while (err > 0) {
51
	    total_size += err;
52
53
	    gettimeofday(&tv, NULL);
54
	    us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
55
	    if ((us - last_write) > WRITE_INTERVAL * 1000000) {
56
		last_write = us;
57
		printf("Reading: %s (%lu GB),  Measured speed: %lu mB/s\n", argv[0], total_size / 1024 / 1024 / 1024, total_size / us);
58
	    }
59
	    err = read(fd, buffer, size);
60
	}
61
	
62
	close(fd);
63
64
65
	return 0;
66
    }
67
7 by Suren A. Chilingaryan
seqreader
68
    chdir(argv[1]);
69
70
    if (argc > 2) {
71
	SKIP = atoi(argv[2]);
72
	
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
73
	printf("Skip %zu\n", SKIP);
7 by Suren A. Chilingaryan
seqreader
74
    }
75
76
    gettimeofday(&start, NULL);
77
    for (run = 0; run < SKIP; run++) {
78
      skip = 0;
79
      dir = opendir(".");
8 by Suren A. Chilingaryan
Support RAW devices by seqreader
80
      while ((ent = readdir(dir))) {
7 by Suren A. Chilingaryan
seqreader
81
	struct stat st;
82
	
83
	if (((skip++)%SKIP) != run) continue;
84
85
	if (stat(ent->d_name, &st)) continue;
86
	if (!S_ISREG(st.st_mode)) continue;
87
	
88
	FILE *f = fopen(ent->d_name, "r");
89
	if (!f) continue;
90
	
91
	int size = st.st_blksize;
92
	
93
	if (size > BUFSIZE) {
94
	    printf("Buffer too small\n");
95
	    exit(1);
96
	}
97
	
98
	while (!feof(f)) {
99
	    err = fread(buffer, 1, size, f);
100
	    if (err < 0) {
101
		printf("Read failed\n");
102
		exit(1);
103
	    }
104
	}
105
	
106
	fclose(f);
107
108
	total_size += st.st_size;
109
	files++;
110
	
111
	gettimeofday(&tv, NULL);
112
	us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
113
	printf("Reading: %s (%lu MB), Read: %lu files (%lu GB),  Measured speed: %lu mB/s\n", ent->d_name, st.st_size/1024/1024, files, total_size / 1024 / 1024 / 1024, total_size / us);
114
      }
115
      closedir(dir);
116
    }
117
118
}