1 /* gcc -o readtest readtest.c `pkg-config --cflags --libs glib-2.0` -lm */
18 read_one (int fd, ssize_t sz)
26 fprintf (stderr, "readtest [ -b BLOCKSIZE ] [-l FILELIMIT] [ -D ] filename-template\n");
30 main (int argc, char* argv[])
33 char optstring[] = "b:Dl:q";
34 uint32_t block_size = 64 * 1024 * 4;
39 const struct option longopts[] = {
40 { "blocksize", 1, 0, 'b' },
41 { "direct", 0, 0, 'D' },
42 { "limit", 1, 0, 'l' },
48 char const * name_template = 0;
55 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
61 block_size = atoi (optarg);
64 max_files = atoi (optarg);
81 name_template = argv[optind];
88 char path[PATH_MAX+1];
90 snprintf (path, sizeof (path), name_template, n+1);
92 if (access (path, R_OK) != 0) {
98 if (max_files > 0 && n >= max_files) {
104 fprintf (stderr, "No matching files found for %s\n", name_template);
109 printf ("Discovered %d files using %s\n", n, name_template);
113 files = (int *) malloc (sizeof (int) * nfiles);
115 for (n = 0; n < nfiles; ++n) {
117 char path[PATH_MAX+1];
120 snprintf (path, sizeof (path), name_template, n+1);
122 if ((fd = open (path, flags, 0644)) < 0) {
123 fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
129 /* Apple man pages say only that it returns "a value other than -1 on success",
130 which probably means zero, but you just can't be too careful with
133 if (fcntl (fd, F_NOCACHE, 1) == -1) {
134 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
142 data = (char*) malloc (sizeof (char) * block_size);
144 double max_elapsed = 0;
145 double total_time = 0;
149 before = g_get_monotonic_time();
151 for (n = 0; n < nfiles; ++n) {
153 if (read (files[n], (char*) data, block_size) != block_size) {
154 fprintf (stderr, "read failed on file %d (%s)\n", n, strerror (errno));
160 gint64 elapsed = g_get_monotonic_time() - before;
161 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
164 printf ("BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
167 total_time += elapsed;
169 if (elapsed > max_elapsed) {
170 max_elapsed = elapsed;
176 if (max_elapsed > 0 && total_time > 0) {
177 double bandwidth = ((nfiles * _read)/1048576.0) / (total_time/1000000.0);
178 double min_throughput = ((nfiles * block_size)/1048576.0) / (max_elapsed/1000000.0);
179 printf ("Min: %.4f MB/sec Avg: %.4f MB/sec || Max: %.3f sec \n", min_throughput, bandwidth, max_elapsed/1000000.0);
180 printf ("Max Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * bandwidth / (4 * 48000.)));