|
1 /* |
|
2 * Copyright (c) 2011, 2018, 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 |
|
24 package nsk.monitoring.ThreadMXBean.GetThreadAllocatedBytes; |
|
25 |
|
26 import nsk.share.*; |
|
27 import nsk.monitoring.share.*; |
|
28 import nsk.monitoring.ThreadMXBean.ThreadMXBeanTestBase; |
|
29 import nsk.monitoring.ThreadMXBean.MXBeanTestThread; |
|
30 import nsk.monitoring.ThreadMXBean.BarrierHandler; |
|
31 |
|
32 /** |
|
33 * Tests getThreadAllocatedBytes(long id) function of com.sun.management.ThreadMXBean |
|
34 * <p> |
|
35 * - Check that result of getThreadAllocatedBytes(long id) call for similar |
|
36 * threads are approximately equals. |
|
37 */ |
|
38 public class EqualThreadsTest extends ThreadMXBeanTestBase { |
|
39 |
|
40 /** |
|
41 * Actually runs the test |
|
42 */ |
|
43 public void run() { |
|
44 if (threadMXBean == null) |
|
45 return; |
|
46 MXBeanTestThread.warmUp(garbageProducerId); |
|
47 MXBeanTestThread tr1 = new MXBeanTestThread(garbageProducerId); |
|
48 MXBeanTestThread tr2 = new MXBeanTestThread(garbageProducerId); |
|
49 BarrierHandler handler = startThreads(tr1, tr2); |
|
50 try { |
|
51 long startBytesTr1 = threadMXBean.getThreadAllocatedBytes(tr1.getId()); |
|
52 long startBytesTr2 = threadMXBean.getThreadAllocatedBytes(tr2.getId()); |
|
53 handler.proceed(); |
|
54 long value1 = threadMXBean.getThreadAllocatedBytes(tr1.getId()) |
|
55 - startBytesTr1; |
|
56 long value2 = threadMXBean.getThreadAllocatedBytes(tr2.getId()) |
|
57 - startBytesTr2; |
|
58 // Expect value1 and value2 differs not more then for 10% |
|
59 if ( Math.abs(value1 - value2) > value1*DELTA_PERCENT/100) |
|
60 throw new TestFailure("Failure! Let f(thread) = getThreadAllocatedBytes()." |
|
61 + " Expected if thread tr1 is similar to thread tr2" |
|
62 + " then f(tr1) and f(tr2) differs not more then for " |
|
63 + DELTA_PERCENT + " %. Recieved: f(tr1)=" + value1 |
|
64 + " f(tr2)=" + value2); |
|
65 log.info("EqualThreadsTest passed."); |
|
66 } finally { |
|
67 handler.finish(); |
|
68 } |
|
69 } |
|
70 |
|
71 /** |
|
72 * Entry point for java program |
|
73 * @param args sets the test configuration |
|
74 */ |
|
75 public static void main(String[] args) { |
|
76 ThreadMXBeanTestBase test = new EqualThreadsTest(); |
|
77 Monitoring.runTest(test, test.setGarbageProducer(args)); |
|
78 } |
|
79 } |