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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f3d47c97 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • 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; ++argv;
158
159 iterations = strtol(*argv, &endptr, 10);
160 if (*endptr != '\0') {
161 printf(NAME ": Error, invalid argument (iterations).\n");
162 syntax_print();
163 return 1;
164 }
165
166 --argc; ++argv;
167 test_type = *argv;
168
169 --argc; ++argv;
170 log_str = *argv;
171
172 --argc; ++argv;
173 path = *argv;
174
175 if (str_cmp(test_type, "sequential-file-read") == 0) {
176 fn = sequential_read_file;
177 }
178 else if (str_cmp(test_type, "sequential-dir-read") == 0) {
179 fn = sequential_read_dir;
180 }
181 else {
182 fprintf(stderr, "Error, unknown test type\n");
183 syntax_print();
184 return 1;
185 }
186
187 for (iteration = 0; iteration < iterations; iteration++) {
188 rc = measure(fn, path, &milliseconds_taken);
189 if (rc != EOK) {
190 fprintf(stderr, "Error: %s\n", str_error(rc));
191 return 1;
192 }
193
194 printf("%s;%s;%s;%lu;ms\n", test_type, path, log_str, milliseconds_taken);
195 }
196
197 return 0;
198}
199
200
201static void syntax_print(void)
202{
203 fprintf(stderr, "syntax: " NAME " <iterations> <test type> <log-str> <path>\n");
204 fprintf(stderr, " <iterations> number of times to run a given test\n");
205 fprintf(stderr, " <test-type> one of:\n");
206 fprintf(stderr, " sequential-file-read\n");
207 fprintf(stderr, " sequential-dir-read\n");
208 fprintf(stderr, " <log-str> a string to attach to results\n");
209 fprintf(stderr, " <path> file/directory to use for testing\n");
210}
211
212/**
213 * @}
214 */
Note: See TracBrowser for help on using the repository browser.