source: mainline/uspace/app/bnchmark/bnchmark.c@ 03b2b2c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 03b2b2c was 15f3c3f, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Rename devmap to loc, devfs to locfs.

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