MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hrt_stopwatch_test.c
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 /*
19  * hrt_stopwatch_test.c
20  *
21  */
22 
23 #include <assert.h>
24 #include <stdio.h>
25 
26 #include "hrt_stopwatch.h"
27 #include "hrt_gstopwatch.h"
28 
29 /*
30  * High-Resolution Time Stopwatch Utility -- Test
31  */
32 
33 
34 static void
35 do_something(void)
36 {
37  const unsigned long loop = 1000000000L;
38  unsigned long i;
39  static volatile unsigned long dummy;
40  for (i = 0; i < loop; i++)
41  dummy = i;
42 }
43 
44 int
45 main(int argc, const char* argv[])
46 {
47  printf("--> main()\n");
48 
49  printf("init stopwatches...\n");
50  hrt_gsw_init(10);
51  assert(hrt_gsw_top() == -1);
52  hrt_stopwatch sw;
53  hrt_sw_init(&sw, 10);
54  assert(hrt_sw_top(&sw) == -1);
55 
56  printf("marking global time...\n");
57  int g0 = hrt_gsw_pushmark();
58  do_something();
59 
60  printf("marking time...\n");
61  int t0 = hrt_sw_pushmark(&sw);
62  do_something();
63 
64  printf("marking time...\n");
65  int t1 = hrt_sw_pushmark(&sw);
66  do_something();
67 
68  printf("marking time...\n");
69  int t2 = hrt_sw_pushmark(&sw);
70  do_something();
71 
72  printf("marking global time...\n");
73  int g2 = hrt_gsw_pushmark();
74 
75  assert(hrt_gsw_top() == 1);
76  assert(hrt_sw_top(&sw) == 2);
77 
78  printf("\namount of times:\n");
79  double rt0 = hrt_sw_rtmicros(&sw, t1, t0);
80  double rt1 = hrt_sw_rtmicros(&sw, t2, t1);
81  double rt2 = hrt_sw_rtmicros(&sw, t2, t0);
82  double grt2 = hrt_gsw_rtmicros(g2, g0);
83  printf("[t0..t1] real = %.3f us\n", rt0);
84  printf("[t1..t2] real = %.3f us\n", rt1);
85  printf("[t0..t2] real = %.3f us\n", rt2);
86  printf("[g0..g2] real = %.3f us\n", grt2);
87  double ct0 = hrt_sw_ctmicros(&sw, t1, t0);
88  double ct1 = hrt_sw_ctmicros(&sw, t2, t1);
89  double ct2 = hrt_sw_ctmicros(&sw, t2, t0);
90  double gct2 = hrt_gsw_ctmicros(g2, g0);
91  printf("[t0..t1] cpu = %.3f us\n", ct0);
92  printf("[t1..t2] cpu = %.3f us\n", ct1);
93  printf("[t0..t2] cpu = %.3f us\n", ct2);
94  printf("[g0..g2] cpu = %.3f us\n", gct2);
95 
96  printf("\npopping timemarks...\n");
97  hrt_gsw_popmark();
98  assert(hrt_gsw_top() == 0);
99  hrt_gsw_popmark();
100  assert(hrt_gsw_top() == -1);
101  hrt_sw_popmark(&sw);
102  assert(hrt_sw_top(&sw) == 1);
103  hrt_sw_popmark(&sw);
104  assert(hrt_sw_top(&sw) == 0);
105  hrt_sw_popmark(&sw);
106  assert(hrt_sw_top(&sw) == -1);
107 
108  printf("closing stopwatches...\n");
109  hrt_sw_close(&sw);
110  hrt_gsw_close();
111 
112  printf("\n<-- main()\n");
113  return 0;
114 }