1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.timer;
22
23 import java.text.DecimalFormat;
24 import java.lang.reflect.Method;
25
26 import org.apache.log4j.Logger;
27
28
29
30
31
32
33
34
35
36 import junit.framework.TestCase;
37 /***
38 * TODO Add docs
39 * @author vlads
40 * @version $Revision: 1.4 $ ($Author: vlads $)
41 */
42 public class NativeTimerTest extends TestCase {
43
44 /***
45 * logger.
46 */
47 protected final Logger log = Logger.getLogger(this.getClass());
48
49 final DecimalFormat format = new DecimalFormat();
50 final DecimalFormat formatLong = new DecimalFormat("00000000000");
51
52 protected void setUp() throws Exception {
53 super.setUp();
54 format.setMinimumFractionDigits(3);
55 format.setMaximumFractionDigits(4);
56 formatLong.setMaximumFractionDigits(0);
57 }
58
59 public void testAACompareTimers() {
60 compareTimers();
61 }
62
63 private void compareTimers() {
64 double t1 = NativeTimer.getTime();
65 double s1 = SystemTimer.getTime();
66 double diff = t1 - s1;
67 if (Math.abs(diff) > 10) {
68 log.debug("SystemTimer = " + formatLong.format(t1));
69 log.debug("NativeTimer = " + formatLong.format(s1));
70 log.debug("diff = " + diff);
71 }
72 assertTrue("System to Native diff", (Math.abs(diff) <= 32));
73 }
74
75 public void testTimer() {
76 double t1, t2;
77 log.debug("Start");
78
79 t1 = NativeTimer.getTime();
80 t2 = NativeTimer.getTime();
81
82 log.debug("t1 = " + t1);
83 log.debug("t2 = " + t2);
84
85 assertTrue(t1 <= t2);
86 }
87
88 public void xx_testSystemTimer() throws Exception {
89
90 for (int i = 0; i < 5; ++ i) {
91 double t1 = NativeTimer.getTime();
92 long st1 = System.currentTimeMillis();
93 Thread.sleep(3);
94 double duration_s = System.currentTimeMillis() - st1;
95 double duration_n = NativeTimer.getTime() - t1;
96 log.debug("duration_n = " + format.format (duration_n) + " ms");
97 log.debug("duration_s = " + format.format (duration_s) + " ms");
98 assertTrue((duration_n >= 3) && (duration_n < 15));
99 }
100 }
101
102 public double mesuremensFunction(String methodName) throws Exception {
103 Method method = this.getClass().getMethod(methodName, null);
104 double start = NativeTimer.getTime();
105 long cnt = 10000;
106 for (int i = 0; i < cnt; ++ i) {
107 method.invoke(this, null);
108 }
109 double duration = NativeTimer.getTime() - start;
110 double callDuration = duration/cnt;
111 log.debug(methodName + " call duration = " + format.format(callDuration) + " ms");
112 return callDuration;
113 }
114
115 public void mesuremensCallTimer() throws Exception {
116 double start = NativeTimer.getTime();
117 long cnt = 10000;
118 for (int i = 0; i < cnt; ++ i) {
119 callTimer();
120 }
121 double duration = NativeTimer.getTime() - start;
122 log.debug("no reflection call duration = " + format.format (duration/cnt) + " ms");
123 }
124 public void mesuremensNanoTime() throws Exception {
125 double start = NativeTimer.getTime();
126 long cnt = 10000;
127 for (int i = 0; i < cnt; ++ i) {
128 callNanoTime();
129 }
130 double duration = NativeTimer.getTime() - start;
131 log.debug("no reflection callNanoTime duration = " + format.format (duration/cnt) + " ms");
132 }
133
134 public void callTimer() {
135 double t1 = NativeTimer.getTime();
136 }
137
138 public void callNanoTime() {
139
140 long t = System.currentTimeMillis();
141 }
142
143
144 public void testTimerPerformance() throws Exception {
145 mesuremensFunction("callTimer");
146 mesuremensCallTimer();
147 mesuremensFunction("callNanoTime");
148 mesuremensNanoTime();
149 }
150
151 public void testBBCompareTimers() {
152 compareTimers();
153 }
154
155 public class TimerInThread extends Thread {
156
157 public boolean callTimer;
158 int cnt_max = 5000;
159 double value = 0;
160
161 public void run() {
162 for (int i = 0; i < cnt_max; i ++) {
163 if (callTimer) {
164 value += NativeTimer.getTime();
165 }
166 }
167 }
168 }
169
170 public void testInThread() throws Exception {
171 int cnt_max = 10000;
172 runInThread(1, cnt_max);
173 runInThread(2, cnt_max);
174 runInThread(3, cnt_max);
175 }
176
177 public double runInThread(int totalThread, int cnt_max) throws Exception {
178
179 TimerInThread[] threads = new TimerInThread[totalThread];
180
181 SimpleTimer tr = new SimpleTimer();
182
183 for (int i = 0 ; i < totalThread; i++ ) {
184 TimerInThread t = new TimerInThread();
185 t.callTimer = true;
186 t.cnt_max = cnt_max;
187 threads[i] = t;
188 t.start();
189 }
190
191 for (int i = 0; i < threads.length; i++) {
192 threads[i].join();
193 }
194 tr.end();
195 log.debug(totalThread + " threads duration = " + tr.text() + " ms");
196 log.debug("~call duration = " + format.format(tr.duration()/(cnt_max * totalThread)) + " ms");
197
198 return tr.duration();
199 }
200
201
202
203
204 public void testZZCompareTimers() {
205 compareTimers();
206 }
207 }