1/*
2 * Copyright (c) 2008, 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
24import java.lang.instrument.Instrumentation;
25
26public class StressGetObjectSizeApp
27    extends ASimpleInstrumentationTestCase
28{
29
30    /**
31     * Constructor for StressGetObjectSizeApp.
32     * @param name
33     */
34    public StressGetObjectSizeApp(String name)
35    {
36        super(name);
37    }
38
39    public static void
40    main (String[] args)
41        throws Throwable {
42        ATestCaseScaffold   test = new StressGetObjectSizeApp(args[0]);
43        test.runTest();
44    }
45
46    protected final void
47    doRunTest()
48        throws Throwable {
49        stressGetObjectSize();
50    }
51
52    public void stressGetObjectSize() {
53        System.out.println("main: an object size=" +
54            fInst.getObjectSize(new Object()));
55
56        RoundAndRound[] threads = new RoundAndRound[10];
57        for (int i = 0; i < threads.length; ++i) {
58            threads[i] = new RoundAndRound(fInst);
59            threads[i].start();
60        }
61        try {
62            Thread.sleep(500); // let all threads get going in their loops
63        } catch (InterruptedException ie) {
64        }
65        System.out.println("stressGetObjectSize: returning");
66        return;
67    }
68
69    private static class RoundAndRound extends Thread {
70        private final Instrumentation inst;
71        private final Object anObject;
72
73        public RoundAndRound(Instrumentation inst) {
74            this.inst = inst;
75            this.anObject = new Object();
76            setDaemon(true);
77        }
78
79        public void run() {
80            long sum = 0;
81            while (true) {
82              sum += inst.getObjectSize(anObject);
83            }
84        }
85    }
86}
87