[423f64f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Sucha
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup test
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file bnchmark.c
|
---|
| 35 | * This program measures time for various actions and writes the results
|
---|
| 36 | * to a file.
|
---|
| 37 | *
|
---|
| 38 | */
|
---|
| 39 |
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 42 | #include <stdint.h>
|
---|
[c1694b6b] | 43 | #include <str_error.h>
|
---|
[423f64f] | 44 | #include <mem.h>
|
---|
[15f3c3f] | 45 | #include <loc.h>
|
---|
[423f64f] | 46 | #include <byteorder.h>
|
---|
| 47 | #include <inttypes.h>
|
---|
| 48 | #include <errno.h>
|
---|
| 49 | #include <time.h>
|
---|
| 50 | #include <dirent.h>
|
---|
[1d6dd2a] | 51 | #include <str.h>
|
---|
[423f64f] | 52 |
|
---|
| 53 | #define NAME "bnchmark"
|
---|
| 54 | #define BUFSIZE 8096
|
---|
| 55 | #define MBYTE (1024*1024)
|
---|
| 56 |
|
---|
[1433ecda] | 57 | typedef errno_t (*measure_func_t)(void *);
|
---|
[423f64f] | 58 | typedef unsigned long umseconds_t; /* milliseconds */
|
---|
| 59 |
|
---|
| 60 | static void syntax_print(void);
|
---|
| 61 |
|
---|
[1433ecda] | 62 | static errno_t measure(measure_func_t fn, void *data, umseconds_t *result)
|
---|
[423f64f] | 63 | {
|
---|
| 64 | struct timeval start_time;
|
---|
[1ab8539] | 65 | gettimeofday(&start_time, NULL);
|
---|
[a35b458] | 66 |
|
---|
[b7fd2a0] | 67 | errno_t rc = fn(data);
|
---|
[423f64f] | 68 | if (rc != EOK) {
|
---|
| 69 | fprintf(stderr, "measured function failed\n");
|
---|
| 70 | return rc;
|
---|
| 71 | }
|
---|
[a35b458] | 72 |
|
---|
[423f64f] | 73 | struct timeval final_time;
|
---|
[1ab8539] | 74 | gettimeofday(&final_time, NULL);
|
---|
[a35b458] | 75 |
|
---|
[423f64f] | 76 | /* Calculate time difference in milliseconds */
|
---|
| 77 | *result = ((final_time.tv_usec - start_time.tv_usec) / 1000) +
|
---|
| 78 | ((final_time.tv_sec - start_time.tv_sec) * 1000);
|
---|
| 79 | return EOK;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[b7fd2a0] | 82 | static errno_t sequential_read_file(void *data)
|
---|
[423f64f] | 83 | {
|
---|
| 84 | char *path = (char *) data;
|
---|
| 85 | char *buf = malloc(BUFSIZE);
|
---|
[a35b458] | 86 |
|
---|
[1ab8539] | 87 | if (buf == NULL)
|
---|
[423f64f] | 88 | return ENOMEM;
|
---|
[a35b458] | 89 |
|
---|
[423f64f] | 90 | FILE *file = fopen(path, "r");
|
---|
| 91 | if (file == NULL) {
|
---|
| 92 | fprintf(stderr, "Failed opening file: %s\n", path);
|
---|
| 93 | free(buf);
|
---|
| 94 | return EIO;
|
---|
| 95 | }
|
---|
[a35b458] | 96 |
|
---|
[423f64f] | 97 | while (!feof(file)) {
|
---|
| 98 | fread(buf, 1, BUFSIZE, file);
|
---|
| 99 | if (ferror(file)) {
|
---|
| 100 | fprintf(stderr, "Failed reading file\n");
|
---|
| 101 | fclose(file);
|
---|
| 102 | free(buf);
|
---|
| 103 | return EIO;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
[a35b458] | 106 |
|
---|
[423f64f] | 107 | fclose(file);
|
---|
| 108 | free(buf);
|
---|
| 109 | return EOK;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[b7fd2a0] | 112 | static errno_t sequential_read_dir(void *data)
|
---|
[423f64f] | 113 | {
|
---|
| 114 | char *path = (char *) data;
|
---|
[a35b458] | 115 |
|
---|
[423f64f] | 116 | DIR *dir = opendir(path);
|
---|
| 117 | if (dir == NULL) {
|
---|
| 118 | fprintf(stderr, "Failed opening directory: %s\n", path);
|
---|
| 119 | return EIO;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | struct dirent *dp;
|
---|
[a35b458] | 123 |
|
---|
[423f64f] | 124 | while ((dp = readdir(dir))) {
|
---|
| 125 | /* Do nothing */
|
---|
| 126 | }
|
---|
[a35b458] | 127 |
|
---|
[423f64f] | 128 | closedir(dir);
|
---|
| 129 | return EOK;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | int main(int argc, char **argv)
|
---|
| 133 | {
|
---|
[b7fd2a0] | 134 | errno_t rc;
|
---|
[c4f7bf6] | 135 | umseconds_t milliseconds_taken = 0;
|
---|
[423f64f] | 136 | char *path = NULL;
|
---|
| 137 | measure_func_t fn = NULL;
|
---|
| 138 | int iteration;
|
---|
| 139 | int iterations;
|
---|
| 140 | char *log_str = NULL;
|
---|
| 141 | char *test_type = NULL;
|
---|
| 142 | char *endptr;
|
---|
[a35b458] | 143 |
|
---|
[423f64f] | 144 | if (argc < 5) {
|
---|
| 145 | fprintf(stderr, NAME ": Error, argument missing.\n");
|
---|
| 146 | syntax_print();
|
---|
| 147 | return 1;
|
---|
| 148 | }
|
---|
[a35b458] | 149 |
|
---|
[423f64f] | 150 | if (argc > 5) {
|
---|
| 151 | fprintf(stderr, NAME ": Error, too many arguments.\n");
|
---|
| 152 | syntax_print();
|
---|
| 153 | return 1;
|
---|
| 154 | }
|
---|
[a35b458] | 155 |
|
---|
[423f64f] | 156 | // Skip program name
|
---|
[1433ecda] | 157 | --argc;
|
---|
| 158 | ++argv;
|
---|
[423f64f] | 159 |
|
---|
| 160 | iterations = strtol(*argv, &endptr, 10);
|
---|
| 161 | if (*endptr != '\0') {
|
---|
| 162 | printf(NAME ": Error, invalid argument (iterations).\n");
|
---|
| 163 | syntax_print();
|
---|
| 164 | return 1;
|
---|
| 165 | }
|
---|
[a35b458] | 166 |
|
---|
[1433ecda] | 167 | --argc;
|
---|
| 168 | ++argv;
|
---|
[423f64f] | 169 | test_type = *argv;
|
---|
| 170 |
|
---|
[1433ecda] | 171 | --argc;
|
---|
| 172 | ++argv;
|
---|
[423f64f] | 173 | log_str = *argv;
|
---|
| 174 |
|
---|
[1433ecda] | 175 | --argc;
|
---|
| 176 | ++argv;
|
---|
[423f64f] | 177 | path = *argv;
|
---|
[a35b458] | 178 |
|
---|
[423f64f] | 179 | if (str_cmp(test_type, "sequential-file-read") == 0) {
|
---|
| 180 | fn = sequential_read_file;
|
---|
[1433ecda] | 181 | } else if (str_cmp(test_type, "sequential-dir-read") == 0) {
|
---|
[423f64f] | 182 | fn = sequential_read_dir;
|
---|
[1433ecda] | 183 | } else {
|
---|
[423f64f] | 184 | fprintf(stderr, "Error, unknown test type\n");
|
---|
| 185 | syntax_print();
|
---|
| 186 | return 1;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | for (iteration = 0; iteration < iterations; iteration++) {
|
---|
| 190 | rc = measure(fn, path, &milliseconds_taken);
|
---|
| 191 | if (rc != EOK) {
|
---|
[c1694b6b] | 192 | fprintf(stderr, "Error: %s\n", str_error(rc));
|
---|
[423f64f] | 193 | return 1;
|
---|
| 194 | }
|
---|
[a35b458] | 195 |
|
---|
[423f64f] | 196 | printf("%s;%s;%s;%lu;ms\n", test_type, path, log_str, milliseconds_taken);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | return 0;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | static void syntax_print(void)
|
---|
| 204 | {
|
---|
[d030e0c] | 205 | fprintf(stderr, "syntax: " NAME " <iterations> <test type> <log-str> <path>\n");
|
---|
| 206 | fprintf(stderr, " <iterations> number of times to run a given test\n");
|
---|
| 207 | fprintf(stderr, " <test-type> one of:\n");
|
---|
| 208 | fprintf(stderr, " sequential-file-read\n");
|
---|
| 209 | fprintf(stderr, " sequential-dir-read\n");
|
---|
| 210 | fprintf(stderr, " <log-str> a string to attach to results\n");
|
---|
| 211 | fprintf(stderr, " <path> file/directory to use for testing\n");
|
---|
[423f64f] | 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * @}
|
---|
| 216 | */
|
---|