Mercurial > hg > truffle
comparison graal/com.oracle.jvmci.debug.test/src/com/oracle/jvmci/debug/test/DebugTimerTest.java @ 21554:b1530a6cce8c
renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Tue, 26 May 2015 23:21:15 +0200 |
parents | graal/com.oracle.graal.debug.test/src/com/oracle/graal/debug/test/DebugTimerTest.java@fa75218e3942 |
children | f5b549811bac |
comparison
equal
deleted
inserted
replaced
21553:0910a9497b02 | 21554:b1530a6cce8c |
---|---|
1 /* | |
2 * Copyright (c) 2013, 2013, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.jvmci.debug.test; | |
24 | |
25 import static org.junit.Assert.*; | |
26 | |
27 import java.lang.management.*; | |
28 | |
29 import org.junit.*; | |
30 | |
31 import com.oracle.jvmci.debug.*; | |
32 | |
33 | |
34 public class DebugTimerTest { | |
35 | |
36 private static final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); | |
37 | |
38 /** | |
39 * Actively spins the current thread for at least a given number of milliseconds in such a way | |
40 * that timers for the current thread keep ticking over. | |
41 * | |
42 * @return the number of milliseconds actually spent spinning which is guaranteed to be >= | |
43 * {@code ms} | |
44 */ | |
45 private static long spin(long ms) { | |
46 long start = threadMXBean.getCurrentThreadCpuTime(); | |
47 do { | |
48 long durationMS = (threadMXBean.getCurrentThreadCpuTime() - start) / 1000; | |
49 if (durationMS >= ms) { | |
50 return durationMS; | |
51 } | |
52 } while (true); | |
53 } | |
54 | |
55 @Test | |
56 public void test1() { | |
57 DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, null, null, System.out); | |
58 try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) { | |
59 | |
60 DebugTimer timerA = Debug.timer("TimerA"); | |
61 DebugTimer timerB = Debug.timer("TimerB"); | |
62 | |
63 long spinA; | |
64 long spinB; | |
65 | |
66 try (DebugCloseable a1 = timerA.start()) { | |
67 spinA = spin(50); | |
68 try (DebugCloseable b1 = timerB.start()) { | |
69 spinB = spin(50); | |
70 } | |
71 } | |
72 | |
73 Assert.assertTrue(timerB.getCurrentValue() < timerA.getCurrentValue()); | |
74 if (timerA.getFlat() != null && timerB.getFlat() != null) { | |
75 assertTrue(spinB >= spinA || timerB.getFlat().getCurrentValue() < timerA.getFlat().getCurrentValue()); | |
76 assertEquals(timerA.getFlat().getCurrentValue(), timerA.getCurrentValue() - timerB.getFlat().getCurrentValue(), 10D); | |
77 } | |
78 } | |
79 } | |
80 | |
81 @Test | |
82 public void test2() { | |
83 DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, null, null, System.out); | |
84 try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) { | |
85 DebugTimer timerC = Debug.timer("TimerC"); | |
86 try (DebugCloseable c1 = timerC.start()) { | |
87 spin(50); | |
88 try (DebugCloseable c2 = timerC.start()) { | |
89 spin(50); | |
90 try (DebugCloseable c3 = timerC.start()) { | |
91 spin(50); | |
92 try (DebugCloseable c4 = timerC.start()) { | |
93 spin(50); | |
94 try (DebugCloseable c5 = timerC.start()) { | |
95 spin(50); | |
96 } | |
97 } | |
98 } | |
99 } | |
100 } | |
101 if (timerC.getFlat() != null) { | |
102 assertEquals(timerC.getFlat().getCurrentValue(), timerC.getCurrentValue()); | |
103 } | |
104 } | |
105 } | |
106 | |
107 @Test | |
108 public void test3() { | |
109 DebugConfig debugConfig = Debug.fixedConfig(0, 0, false, false, true, false, null, null, System.out); | |
110 try (DebugConfigScope dcs = new DebugConfigScope(debugConfig); Debug.Scope s = Debug.scope("DebugTimerTest")) { | |
111 | |
112 DebugTimer timerD = Debug.timer("TimerD"); | |
113 DebugTimer timerE = Debug.timer("TimerE"); | |
114 | |
115 long spinD1; | |
116 long spinE; | |
117 | |
118 try (DebugCloseable d1 = timerD.start()) { | |
119 spinD1 = spin(50); | |
120 try (DebugCloseable e1 = timerE.start()) { | |
121 spinE = spin(50); | |
122 try (DebugCloseable d2 = timerD.start()) { | |
123 spin(50); | |
124 try (DebugCloseable d3 = timerD.start()) { | |
125 spin(50); | |
126 } | |
127 } | |
128 } | |
129 } | |
130 | |
131 Assert.assertTrue(timerE.getCurrentValue() < timerD.getCurrentValue()); | |
132 if (timerD.getFlat() != null && timerE.getFlat() != null) { | |
133 assertTrue(spinE >= spinD1 || timerE.getFlat().getCurrentValue() < timerD.getFlat().getCurrentValue()); | |
134 assertEquals(timerD.getFlat().getCurrentValue(), timerD.getCurrentValue() - timerE.getFlat().getCurrentValue(), 10D); | |
135 } | |
136 } | |
137 } | |
138 } |