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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1834a01 was b1834a01, checked in by Jakub Jermar <jakub@…>, 7 years ago

Categorize the remaining orphan doxygroups

  • Property mode set to 100644
File size: 5.0 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 bnchmark
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 *);
58
59static void syntax_print(void);
60
61static errno_t measure(measure_func_t fn, void *data, msec_t *result)
62{
63 struct timespec start_time;
64 getuptime(&start_time);
65
66 errno_t rc = fn(data);
67 if (rc != EOK) {
68 fprintf(stderr, "measured function failed\n");
69 return rc;
70 }
71
72 struct timespec final_time;
73 getuptime(&final_time);
74
75 /* Calculate time difference in milliseconds */
76 *result = NSEC2USEC(ts_sub_diff(&final_time, &start_time));
77 return EOK;
78}
79
80static errno_t sequential_read_file(void *data)
81{
82 char *path = (char *) data;
83 char *buf = malloc(BUFSIZE);
84
85 if (buf == NULL)
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
110static errno_t 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
130int main(int argc, char **argv)
131{
132 errno_t rc;
133 msec_t milliseconds_taken = 0;
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;
156 ++argv;
157
158 iterations = strtol(*argv, &endptr, 10);
159 if (*endptr != '\0') {
160 printf(NAME ": Error, invalid argument (iterations).\n");
161 syntax_print();
162 return 1;
163 }
164
165 --argc;
166 ++argv;
167 test_type = *argv;
168
169 --argc;
170 ++argv;
171 log_str = *argv;
172
173 --argc;
174 ++argv;
175 path = *argv;
176
177 if (str_cmp(test_type, "sequential-file-read") == 0) {
178 fn = sequential_read_file;
179 } else if (str_cmp(test_type, "sequential-dir-read") == 0) {
180 fn = sequential_read_dir;
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;%lld;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.