source: mainline/uspace/app/bnchmark/bnchmark.c@ e498a45

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e498a45 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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>
42#include <stdint.h>
43#include <str_error.h>
44#include <mem.h>
45#include <loc.h>
46#include <byteorder.h>
47#include <inttypes.h>
48#include <errno.h>
49#include <time.h>
50#include <dirent.h>
51#include <str.h>
52
53#define NAME "bnchmark"
54#define BUFSIZE 8096
55#define MBYTE (1024*1024)
56
57typedef errno_t (*measure_func_t)(void *);
58typedef unsigned long umseconds_t; /* milliseconds */
59
60static void syntax_print(void);
61
62static errno_t measure(measure_func_t fn, void *data, umseconds_t *result)
63{
64 struct timeval start_time;
65 gettimeofday(&start_time, NULL);
66
67 errno_t rc = fn(data);
68 if (rc != EOK) {
69 fprintf(stderr, "measured function failed\n");
70 return rc;
71 }
72
73 struct timeval final_time;
74 gettimeofday(&final_time, NULL);
75
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
82static errno_t sequential_read_file(void *data)
83{
84 char *path = (char *) data;
85 char *buf = malloc(BUFSIZE);
86
87 if (buf == NULL)
88 return ENOMEM;
89
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 }
96
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 }
106
107 fclose(file);
108 free(buf);
109 return EOK;
110}
111
112static errno_t sequential_read_dir(void *data)
113{
114 char *path = (char *) data;
115
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;
123
124 while ((dp = readdir(dir))) {
125 /* Do nothing */
126 }
127
128 closedir(dir);
129 return EOK;
130}
131
132int main(int argc, char **argv)
133{
134 errno_t rc;
135 umseconds_t milliseconds_taken = 0;
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;
143
144 if (argc < 5) {
145 fprintf(stderr, NAME ": Error, argument missing.\n");
146 syntax_print();
147 return 1;
148 }
149
150 if (argc > 5) {
151 fprintf(stderr, NAME ": Error, too many arguments.\n");
152 syntax_print();
153 return 1;
154 }
155
156 // Skip program name
157 --argc;
158 ++argv;
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 }
166
167 --argc;
168 ++argv;
169 test_type = *argv;
170
171 --argc;
172 ++argv;
173 log_str = *argv;
174
175 --argc;
176 ++argv;
177 path = *argv;
178
179 if (str_cmp(test_type, "sequential-file-read") == 0) {
180 fn = sequential_read_file;
181 } else if (str_cmp(test_type, "sequential-dir-read") == 0) {
182 fn = sequential_read_dir;
183 } else {
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) {
192 fprintf(stderr, "Error: %s\n", str_error(rc));
193 return 1;
194 }
195
196 printf("%s;%s;%s;%lu;ms\n", test_type, path, log_str, milliseconds_taken);
197 }
198
199 return 0;
200}
201
202
203static void syntax_print(void)
204{
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");
212}
213
214/**
215 * @}
216 */
Note: See TracBrowser for help on using the repository browser.