[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>
|
---|
[423f64f] | 43 | #include <mem.h>
|
---|
[15f3c3f] | 44 | #include <loc.h>
|
---|
[423f64f] | 45 | #include <byteorder.h>
|
---|
| 46 | #include <inttypes.h>
|
---|
| 47 | #include <errno.h>
|
---|
| 48 | #include <time.h>
|
---|
| 49 | #include <dirent.h>
|
---|
| 50 |
|
---|
| 51 | #define NAME "bnchmark"
|
---|
| 52 | #define BUFSIZE 8096
|
---|
| 53 | #define MBYTE (1024*1024)
|
---|
| 54 |
|
---|
| 55 | typedef int(*measure_func_t)(void *);
|
---|
| 56 | typedef unsigned long umseconds_t; /* milliseconds */
|
---|
| 57 |
|
---|
| 58 | static void syntax_print(void);
|
---|
| 59 |
|
---|
| 60 | static int measure(measure_func_t fn, void* data, umseconds_t *result)
|
---|
| 61 | {
|
---|
| 62 | struct timeval start_time;
|
---|
[1ab8539] | 63 | gettimeofday(&start_time, NULL);
|
---|
[423f64f] | 64 |
|
---|
[1ab8539] | 65 | int rc = fn(data);
|
---|
[423f64f] | 66 | if (rc != EOK) {
|
---|
| 67 | fprintf(stderr, "measured function failed\n");
|
---|
| 68 | return rc;
|
---|
| 69 | }
|
---|
[1ab8539] | 70 |
|
---|
[423f64f] | 71 | struct timeval final_time;
|
---|
[1ab8539] | 72 | gettimeofday(&final_time, NULL);
|
---|
[423f64f] | 73 |
|
---|
| 74 | /* Calculate time difference in milliseconds */
|
---|
| 75 | *result = ((final_time.tv_usec - start_time.tv_usec) / 1000) +
|
---|
| 76 | ((final_time.tv_sec - start_time.tv_sec) * 1000);
|
---|
| 77 | return EOK;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | static int sequential_read_file(void *data)
|
---|
| 81 | {
|
---|
| 82 | char *path = (char *) data;
|
---|
| 83 | char *buf = malloc(BUFSIZE);
|
---|
[1ab8539] | 84 |
|
---|
| 85 | if (buf == NULL)
|
---|
[423f64f] | 86 | return ENOMEM;
|
---|
| 87 |
|
---|
| 88 | FILE *file = fopen(path, "r");
|
---|
| 89 | if (file == NULL) {
|
---|
| 90 | fprintf(stderr, "Failed opening file: %s\n", path);
|
---|
| 91 | free(buf);
|
---|
| 92 | return EIO;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | while (!feof(file)) {
|
---|
| 96 | fread(buf, 1, BUFSIZE, file);
|
---|
| 97 | if (ferror(file)) {
|
---|
| 98 | fprintf(stderr, "Failed reading file\n");
|
---|
| 99 | fclose(file);
|
---|
| 100 | free(buf);
|
---|
| 101 | return EIO;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | fclose(file);
|
---|
| 106 | free(buf);
|
---|
| 107 | return EOK;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | static int sequential_read_dir(void *data)
|
---|
| 111 | {
|
---|
| 112 | char *path = (char *) data;
|
---|
| 113 |
|
---|
| 114 | DIR *dir = opendir(path);
|
---|
| 115 | if (dir == NULL) {
|
---|
| 116 | fprintf(stderr, "Failed opening directory: %s\n", path);
|
---|
| 117 | return EIO;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | struct dirent *dp;
|
---|
| 121 |
|
---|
| 122 | while ((dp = readdir(dir))) {
|
---|
| 123 | /* Do nothing */
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | closedir(dir);
|
---|
| 127 | return EOK;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | int main(int argc, char **argv)
|
---|
| 131 | {
|
---|
| 132 | int rc;
|
---|
[c4f7bf6] | 133 | umseconds_t milliseconds_taken = 0;
|
---|
[423f64f] | 134 | char *path = NULL;
|
---|
| 135 | measure_func_t fn = NULL;
|
---|
| 136 | int iteration;
|
---|
| 137 | int iterations;
|
---|
| 138 | char *log_str = NULL;
|
---|
| 139 | char *test_type = NULL;
|
---|
| 140 | char *endptr;
|
---|
| 141 |
|
---|
| 142 | if (argc < 5) {
|
---|
| 143 | fprintf(stderr, NAME ": Error, argument missing.\n");
|
---|
| 144 | syntax_print();
|
---|
| 145 | return 1;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | if (argc > 5) {
|
---|
| 149 | fprintf(stderr, NAME ": Error, too many arguments.\n");
|
---|
| 150 | syntax_print();
|
---|
| 151 | return 1;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | // Skip program name
|
---|
| 155 | --argc; ++argv;
|
---|
| 156 |
|
---|
| 157 | iterations = strtol(*argv, &endptr, 10);
|
---|
| 158 | if (*endptr != '\0') {
|
---|
| 159 | printf(NAME ": Error, invalid argument (iterations).\n");
|
---|
| 160 | syntax_print();
|
---|
| 161 | return 1;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | --argc; ++argv;
|
---|
| 165 | test_type = *argv;
|
---|
| 166 |
|
---|
| 167 | --argc; ++argv;
|
---|
| 168 | log_str = *argv;
|
---|
| 169 |
|
---|
| 170 | --argc; ++argv;
|
---|
| 171 | path = *argv;
|
---|
| 172 |
|
---|
| 173 | if (str_cmp(test_type, "sequential-file-read") == 0) {
|
---|
| 174 | fn = sequential_read_file;
|
---|
| 175 | }
|
---|
| 176 | else if (str_cmp(test_type, "sequential-dir-read") == 0) {
|
---|
| 177 | fn = sequential_read_dir;
|
---|
| 178 | }
|
---|
| 179 | else {
|
---|
| 180 | fprintf(stderr, "Error, unknown test type\n");
|
---|
| 181 | syntax_print();
|
---|
| 182 | return 1;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | for (iteration = 0; iteration < iterations; iteration++) {
|
---|
| 186 | rc = measure(fn, path, &milliseconds_taken);
|
---|
| 187 | if (rc != EOK) {
|
---|
| 188 | fprintf(stderr, "Error %d\n", rc);
|
---|
| 189 | return 1;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | printf("%s;%s;%s;%lu;ms\n", test_type, path, log_str, milliseconds_taken);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | return 0;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | static void syntax_print(void)
|
---|
| 200 | {
|
---|
[d030e0c] | 201 | fprintf(stderr, "syntax: " NAME " <iterations> <test type> <log-str> <path>\n");
|
---|
| 202 | fprintf(stderr, " <iterations> number of times to run a given test\n");
|
---|
| 203 | fprintf(stderr, " <test-type> one of:\n");
|
---|
| 204 | fprintf(stderr, " sequential-file-read\n");
|
---|
| 205 | fprintf(stderr, " sequential-dir-read\n");
|
---|
| 206 | fprintf(stderr, " <log-str> a string to attach to results\n");
|
---|
| 207 | fprintf(stderr, " <path> file/directory to use for testing\n");
|
---|
[423f64f] | 208 | }
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * @}
|
---|
| 212 | */
|
---|