xref: /haiku/src/tests/system/benchmarks/syscallbench.c (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 /*
2  * from ftp://ftp.netbsd.org/pub/NetBSD/misc/gmcgarry/bench/syscallbench.tar.gz
3  *
4  * gcc -Wall -Werror -O3 -static -o ctx ctx.c
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 
12 #define ITERATIONS	1000000
13 
14 static void
15 usage(void)
16 {
17 	printf("syscallbench [-h]\n");
18 	exit(1);
19 }
20 
21 int
22 main(int argc, char *argv[])
23 {
24 	struct timeval before, after;
25 	unsigned long overhead, elapsed;
26 	int i;
27 	pid_t pid;
28 
29 	if (argc > 1)
30 		usage();
31 
32 	gettimeofday(&before, NULL);
33 	for (i=0; i<ITERATIONS; i++) {
34 	}
35 	gettimeofday(&after, NULL);
36 	overhead = 1000000 * (after.tv_sec - before.tv_sec);
37 	overhead += after.tv_usec - before.tv_usec;
38 
39 	gettimeofday(&before, NULL);
40 	for (i=0; i<ITERATIONS; i++) {
41 		pid = getpid();
42 	}
43 	gettimeofday(&after, NULL);
44 	elapsed = 1000000 * (after.tv_sec - before.tv_sec);
45 	elapsed += after.tv_usec - before.tv_usec;
46 
47 	printf("syscall time: %ld microseconds\n",
48 	    (1000*(elapsed-overhead))/ITERATIONS);
49 
50 	return (0);
51 }
52