source: mainline/uspace/app/bnchmark/bnchmark.c@ 45cbf897

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45cbf897 was 1ab8539, checked in by Martin Decky <martin@…>, 11 years ago

remove system.uptime sysinfo entry since it is redundant
cleanup the time handling routines

  • still more cleanup is needed here, the code is a wild combination of quasi-POSIX API (with all the brain-dead types, etc.) and HelenOS API
  • 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 <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 gettimeofday(&start_time, NULL);
65
66 int rc = fn(data);
67 if (rc != EOK) {
68 fprintf(stderr, "measured function failed\n");
69 return rc;
70 }
71
72 struct timeval final_time;
73 gettimeofday(&final_time, NULL);
74
75 /* Calculate time difference in milliseconds */
76 *result = ((final_time.tv_usec - start_time.tv_usec) / 1000) +
77 ((final_time.tv_sec - start_time.tv_sec) * 1000);
78 return EOK;
79}
80
81static int sequential_read_file(void *data)
82{
83 char *path = (char *) data;
84 char *buf = malloc(BUFSIZE);
85
86 if (buf == NULL)
87 return ENOMEM;
88
89 FILE *file = fopen(path, "r");
90 if (file == NULL) {
91 fprintf(stderr, "Failed opening file: %s\n", path);
92 free(buf);
93 return EIO;
94 }
95
96 while (!feof(file)) {
97 fread(buf, 1, BUFSIZE, file);
98 if (ferror(file)) {
99 fprintf(stderr, "Failed reading file\n");
100 fclose(file);
101 free(buf);
102 return EIO;
103 }
104 }
105
106 fclose(file);
107 free(buf);
108 return EOK;
109}
110
111static int sequential_read_dir(void *data)
112{
113 char *path = (char *) data;
114
115 DIR *dir = opendir(path);
116 if (dir == NULL) {
117 fprintf(stderr, "Failed opening directory: %s\n", path);
118 return EIO;
119 }
120
121 struct dirent *dp;
122
123 while ((dp = readdir(dir))) {
124 /* Do nothing */
125 }
126
127 closedir(dir);
128 return EOK;
129}
130
131int main(int argc, char **argv)
132{
133 int rc;
134 umseconds_t milliseconds_taken = 0;
135 char *path = NULL;
136 measure_func_t fn = NULL;
137 int iteration;
138 int iterations;
139 char *log_str = NULL;
140 char *test_type = NULL;
141 char *endptr;
142
143 if (argc < 5) {
144 fprintf(stderr, NAME ": Error, argument missing.\n");
145 syntax_print();
146 return 1;
147 }
148
149 if (argc > 5) {
150 fprintf(stderr, NAME ": Error, too many arguments.\n");
151 syntax_print();
152 return 1;
153 }
154
155 // Skip program name
156 --argc; ++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; ++argv;
166 test_type = *argv;
167
168 --argc; ++argv;
169 log_str = *argv;
170
171 --argc; ++argv;
172 path = *argv;
173
174 if (str_cmp(test_type, "sequential-file-read") == 0) {
175 fn = sequential_read_file;
176 }
177 else if (str_cmp(test_type, "sequential-dir-read") == 0) {
178 fn = sequential_read_dir;
179 }
180 else {
181 fprintf(stderr, "Error, unknown test type\n");
182 syntax_print();
183 return 1;
184 }
185
186 for (iteration = 0; iteration < iterations; iteration++) {
187 rc = measure(fn, path, &milliseconds_taken);
188 if (rc != EOK) {
189 fprintf(stderr, "Error %d\n", rc);
190 return 1;
191 }
192
193 printf("%s;%s;%s;%lu;ms\n", test_type, path, log_str, milliseconds_taken);
194 }
195
196 return 0;
197}
198
199
200static void syntax_print(void)
201{
202 fprintf(stderr, "syntax: " NAME " <iterations> <test type> <log-str> <path>\n");
203 fprintf(stderr, " <iterations> number of times to run a given test\n");
204 fprintf(stderr, " <test-type> one of:\n");
205 fprintf(stderr, " sequential-file-read\n");
206 fprintf(stderr, " sequential-dir-read\n");
207 fprintf(stderr, " <log-str> a string to attach to results\n");
208 fprintf(stderr, " <path> file/directory to use for testing\n");
209}
210
211/**
212 * @}
213 */
Note: See TracBrowser for help on using the repository browser.