MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JniInstrumentationTest.java
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 /*
20  * JniInstrumentationTest.java
21  */
22 
23 package utils;
24 
25 import java.io.PrintWriter;
26 import utils.HrtStopwatch;
27 
28 import java.lang.management.ThreadMXBean;
29 import java.lang.management.ManagementFactory;
30 
31 public class JniInstrumentationTest {
32 
33  static protected final PrintWriter out = new PrintWriter(System.out, true);
34 
35  static protected final PrintWriter err = new PrintWriter(System.err, true);
36 
40  static protected void loadSystemLibrary(String name) {
41  out.print("loading libary ...");
42  out.flush();
43  try {
44  System.loadLibrary(name);
45  } catch (UnsatisfiedLinkError e) {
46  String path;
47  try {
48  path = System.getProperty("java.library.path");
49  } catch (Exception ex) {
50  path = "<exception caught: " + ex.getMessage() + ">";
51  }
52  err.println("failed loading library '"
53  + name + "'; java.library.path='" + path + "'");
54  throw e;
55  } catch (SecurityException e) {
56  err.println("failed loading library '"
57  + name + "'; caught exception: " + e);
58  throw e;
59  }
60  out.println(" [" + name + "]");
61  }
62 
63  static public native void aNativeMethod();
64 
65  static public volatile long dummy;
66  static public void do_something(long count)
67  {
68  long i;
69  for (i = 0; i < count; i++)
70  dummy = i;
71  }
72 
73 
74  static public void main(String[] args)
75  {
76  out.println("--> JniInstrumentationTest.main()");
77 
78  ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();
79  assert (tmxb.isCurrentThreadCpuTimeSupported());
80  long t0 = tmxb.getCurrentThreadCpuTime();
81 
82  out.println();
83  loadSystemLibrary("utils");
84  loadSystemLibrary("jnitest");
85 
86  out.println();
87  out.println("init libutils stopwatch...");
88  HrtStopwatch.init(10);
89 
90  out.println();
91  out.println("marking time Java ...");
92  int g0 = HrtStopwatch.pushmark();
93 
94  aNativeMethod();
95 
96  int g3 = HrtStopwatch.pushmark();
97  out.println("... marked time Java");
98 
99  out.println();
100  out.println("amount of times Java:");
101  double rt0 = HrtStopwatch.rtmicros(g3, g0);
102  out.println("[g" + g0 + "..g" + g3 + "] real = " + rt0 + " us");
103  double rt1 = HrtStopwatch.rtmicros(2, 1);
104  out.println("[g2..g1] real = " + rt1 + " us");
105  double ct0 = HrtStopwatch.ctmicros(g3, g0);
106  out.println("[g" + g0 + "..g" + g3 + "] cpu = " + ct0 + " us");
107  double ct1 = HrtStopwatch.ctmicros(2, 1);
108  out.println("[g2..g1] cpu = " + ct1 + " us");
109 
110  do_something(10000000);
111 
112  long t1 = tmxb.getCurrentThreadCpuTime();
113  out.println();
114  out.println("amount of times TMB:");
115  out.println("[t0..t1] cpu = " + ((t1 - t0)/1000.0) + " us");
116 
117  out.println();
118  out.println("closing libutils stopwatch...");
119  HrtStopwatch.close();
120 
121  out.println("<-- JniInstrumentationTest.main()");
122  }
123 }