Load 57 into trunk.
[nativeclient.git] / tests / null / null.c
blobe568b9bd330d2d099649cfc46d96174f7b874aa3
1 /*
2 * Copyright 2008, Google Inc.
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 are
7 * 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
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <time.h>
37 #include <unistd.h>
40 extern void __nacl_null(void);
42 int main(int ac,
43 char **av) {
44 int opt;
45 int num_rep = 10000000;
46 int i;
47 struct timeval time_start;
48 struct timeval time_end;
49 struct timeval time_elapsed;
50 double time_per_call;
52 while (EOF != (opt = getopt(ac, av, "r:"))) switch (opt) {
53 case 'r':
54 num_rep = strtol(optarg, (char **) NULL, 0);
55 break;
56 default:
57 fprintf(stderr, "Usage: null [-r repetition_count]\n");
58 return 1;
60 gettimeofday(&time_start, (void *) NULL);
61 for (i = num_rep; --i >= 0; ) {
62 __nacl_null();
64 gettimeofday(&time_end, (void *) NULL);
65 time_elapsed.tv_sec = time_end.tv_sec - time_start.tv_sec;
66 time_elapsed.tv_usec = time_end.tv_usec - time_start.tv_usec;
67 if (time_elapsed.tv_usec < 0) {
68 --time_elapsed.tv_sec;
69 time_elapsed.tv_usec += 1000000;
71 printf("Number of null syscalls: %d\n", num_rep);
72 printf("Elapsed time: %d.%06dS\n",
73 (int) time_elapsed.tv_sec,
74 (int) time_elapsed.tv_usec);
75 time_per_call = ((time_elapsed.tv_sec + time_elapsed.tv_usec / 1.0e6)
76 / num_rep);
77 printf("Time per call: %gS or %fnS\n",
78 time_per_call,
79 1.0e9 * time_per_call);
80 printf("Calls per sec: %d\n", (int) (1.0 / time_per_call));
81 return 0;