xref: /dragonfly/test/sysperf/blib.c (revision a4da4a90)
1 /*
2  * BLIB.C
3  *
4  * Simple benchmarking library
5  *
6  * $DragonFly: src/test/sysperf/blib.c,v 1.6 2007/08/21 19:23:46 corecode Exp $
7  */
8 
9 #include <sys/types.h>
10 #include <sys/time.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 
15 static __thread struct timeval tv1;
16 static __thread struct timeval tv2;
17 static __thread long long last_us;
18 
19 void
20 start_timing(void)
21 {
22     gettimeofday(&tv1, NULL);
23 }
24 
25 int
26 stop_timing(long long count, const char *ctl, ...)
27 {
28     long long us;
29     va_list va;
30 
31     gettimeofday(&tv2, NULL);
32     us = (tv2.tv_usec - tv1.tv_usec) + (tv2.tv_sec - tv1.tv_sec) * 1000000LL;
33     last_us = us;
34     if (ctl == NULL) 	/* dummy call to pre-cache */
35 	return(us > 1000000);
36 
37     va_start(va, ctl);
38     vfprintf(stderr, ctl, va);
39     va_end(va);
40 
41     fprintf(stderr, " %6.3fs %lld loops, %8.0f loops/sec, %6.3fuS/loop\n",
42 	(double)us / 1000000.0,
43 	count,
44 	(double)count * 1e6 / (double)us,
45 	(double)us / (double)count
46     );
47 
48     tv1 = tv2;
49 
50     return(0);
51 }
52 
53 int
54 stop_timing2(long long count, long long us, const char *ctl, ...)
55 {
56     va_list va;
57 
58     va_start(va, ctl);
59     vfprintf(stderr, ctl, va);
60     va_end(va);
61 
62     fprintf(stderr, " %6.3fs %lld loops = %6.3fnS/loop\n",
63 	(double)us / 1000000.0,
64 	count,
65 	(double)us * 1000.0 / (double)count
66     );
67     return(0);
68 }
69 
70 long long
71 get_timing(void)
72 {
73     return (last_us);
74 }
75 
76 void
77 nop(void)
78 {
79 }
80