MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HrtStopwatchTest.java
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  * HrtStopwatchTest.java
20  */
21 
22 package utils;
23 
24 import java.io.PrintWriter;
25 import utils.HrtStopwatch;
26 import utils.HrtProfiler;
27 
28 public class HrtStopwatchTest {
29 
30  static protected final PrintWriter out = new PrintWriter(System.out, true);
31 
32  static protected final PrintWriter err = new PrintWriter(System.err, true);
33 
37  static protected void loadSystemLibrary(String name) {
38  out.print("loading libary ...");
39  out.flush();
40  try {
41  System.loadLibrary(name);
42  } catch (UnsatisfiedLinkError e) {
43  String path;
44  try {
45  path = System.getProperty("java.library.path");
46  } catch (Exception ex) {
47  path = "<exception caught: " + ex.getMessage() + ">";
48  }
49  err.println("failed loading library '"
50  + name + "'; java.library.path='" + path + "'");
51  throw e;
52  } catch (SecurityException e) {
53  err.println("failed loading library '"
54  + name + "'; caught exception: " + e);
55  throw e;
56  }
57  out.println(" [" + name + "]");
58  }
59 
60  static public volatile long dummy;
61  static public void do_something()
62  {
63  final long loop = 100000000L;
64  long i;
65  for (i = 0; i < loop; i++)
66  dummy = i;
67  }
68 
69  static public void test0()
70  {
71  out.println("--> HrtStopwatchTest.test0()");
72 
73  out.println("marking global time...");
74  int g0 = HrtStopwatch.pushmark();
75  do_something();
76 
77  out.println("marking global time...");
78  int g1 = HrtStopwatch.pushmark();
79  do_something();
80 
81  out.println("marking global time...");
82  int g2 = HrtStopwatch.pushmark();
83 
84  assert (HrtStopwatch.top() == 2);
85 
86  out.println("amount of times:");
87  double rt0 = HrtStopwatch.rtmicros(g1, g0);
88  double rt1 = HrtStopwatch.rtmicros(g2, g1);
89  double rt2 = HrtStopwatch.rtmicros(g2, g0);
90  out.println("[t0..t1] real = " + rt0 + " us");
91  out.println("[t1..t2] real = " + rt1 + " us");
92  out.println("[t0..t2] real = " + rt2 + " us");
93  double ct0 = HrtStopwatch.ctmicros(g1, g0);
94  double ct1 = HrtStopwatch.ctmicros(g2, g1);
95  double ct2 = HrtStopwatch.ctmicros(g2, g0);
96  out.println("[t0..t1] cpu = " + ct0 + " us");
97  out.println("[t1..t2] cpu = " + ct1 + " us");
98  out.println("[t0..t2] cpu = " + ct2 + " us");
99 
100  out.println("popping timemarks");
101  HrtStopwatch.popmark();
102  assert (HrtStopwatch.top() == 1);
103  HrtStopwatch.popmark();
104  assert (HrtStopwatch.top() == 0);
105  HrtStopwatch.popmark();
106  assert (HrtStopwatch.top() == -1);
107 
108  out.println("<-- HrtStopwatchTest.test0()");
109  }
110 
111  static public void test1()
112  {
113  HrtProfiler.enter("test1");
114  out.println("--> HrtStopwatchTest.test1()");
115  do_something();
116  test11();
117  test11();
118  out.println("<-- HrtStopwatchTest.test1()");
119  HrtProfiler.leave("test1");
120  }
121 
122  static public void test11()
123  {
124  HrtProfiler.enter("test11");
125  out.println("--> HrtStopwatchTest.test11()");
126  do_something();
127  test111();
128  test111();
129  out.println("<-- HrtStopwatchTest.test11()");
130  HrtProfiler.leave("test11");
131  }
132 
133  static public void test111()
134  {
135  HrtProfiler.enter("test111");
136  out.println("--> HrtStopwatchTest.test111()");
137  do_something();
138  out.println("<-- HrtStopwatchTest.test111()");
139  HrtProfiler.leave("test111");
140  }
141 
142  static public void main(String[] args)
143  {
144  out.println("--> HrtStopwatchTest.main()");
145  loadSystemLibrary("utils");
146 
147  out.println("init stopwatch...");
148  HrtStopwatch.init(10);
149  assert (HrtStopwatch.top() == -1);
150 
151  out.println();
152  out.println("testing stopwatch...");
153  test0();
154 
155  out.println();
156  out.println("testing profiler...");
157  test1();
158 
159  HrtProfiler.report();
160 
161  out.println("closing stopwatch...");
162  HrtStopwatch.close();
163 
164  out.println("<-- HrtStopwatchTest.main()");
165  }
166 }