From c886d0360ed0eba6c468219a2b6fbcba30fbcaeb Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 14 Jun 2017 19:40:00 +0200 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- measure.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 measure.c (limited to 'measure.c') diff --git a/measure.c b/measure.c new file mode 100644 index 0000000..ef06c5b --- /dev/null +++ b/measure.c @@ -0,0 +1,74 @@ +/* See LICENSE file for copyright and license details. */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + + +#ifndef CAPACITY +# define CAPACITY (1 << 20) +#endif + + +int +main(int argc, char *argv[]) +{ + int fd, n = 0, m = 150000; + ssize_t r; + size_t sum = 0, totsum = 0; + struct timespec start, end; + double rate; + + if (!argc) + *argv = "measure"; + + if (argc > 1) + m = atoi(argv[1]); + + fd = open("/dev/null", O_WRONLY); + if (fd < 0) + goto fail; + + usleep(10000L); + if (argc < 2 && fcntl(STDIN_FILENO, F_GETPIPE_SZ) == CAPACITY) + m = 10; + + if (clock_gettime(CLOCK_MONOTONIC, &start)) + goto fail; + for (;;) { + r = splice(STDIN_FILENO, NULL, fd, NULL, SSIZE_MAX, 0); + if (r <= 0) { + if (r < 0) + goto fail; + goto done; + } + sum += (size_t)r; + if (++n == m) { + if (clock_gettime(CLOCK_MONOTONIC, &end)) + goto fail; + n = 0; + start.tv_sec = end.tv_sec - start.tv_sec; + start.tv_nsec = end.tv_nsec - start.tv_nsec; + rate = (double)start.tv_nsec; + rate /= 1000000000.; + rate += start.tv_sec; + rate = (double)sum / rate; + rate /= 1000000000.; + totsum += sum; + sum = 0; + dprintf(STDERR_FILENO, " %lf GB/s\033[K\n\033[A", rate); + start = end; + } + } + +done: + return 0; + +fail: + perror(*argv); + return 1; +} -- cgit v1.2.3-70-g09d2