StressGetObjectSizeApp.java revision 142:9f75a46fad8b
1219019Sgabor/*
2219019Sgabor * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
3219019Sgabor * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4219019Sgabor *
5219019Sgabor * This code is free software; you can redistribute it and/or modify it
6219019Sgabor * under the terms of the GNU General Public License version 2 only, as
7219019Sgabor * published by the Free Software Foundation.
8219019Sgabor *
9219019Sgabor * This code is distributed in the hope that it will be useful, but WITHOUT
10219019Sgabor * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11219019Sgabor * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12219019Sgabor * version 2 for more details (a copy is included in the LICENSE file that
13219019Sgabor * accompanied this code).
14219019Sgabor *
15219019Sgabor * You should have received a copy of the GNU General Public License version
16219019Sgabor * 2 along with this work; if not, write to the Free Software Foundation,
17219019Sgabor * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18219019Sgabor *
19219019Sgabor * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20219019Sgabor * CA 95054 USA or visit www.sun.com if you need additional information or
21219019Sgabor * have any questions.
22219019Sgabor */
23219019Sgabor
24219019Sgaborimport java.lang.instrument.Instrumentation;
25219019Sgabor
26219019Sgaborpublic class StressGetObjectSizeApp
27219019Sgabor    extends ASimpleInstrumentationTestCase
28219019Sgabor{
29219019Sgabor
30219019Sgabor    /**
31219019Sgabor     * Constructor for StressGetObjectSizeApp.
32219019Sgabor     * @param name
33219019Sgabor     */
34219019Sgabor    public StressGetObjectSizeApp(String name)
35219019Sgabor    {
36219019Sgabor        super(name);
37219019Sgabor    }
38219019Sgabor
39219019Sgabor    public static void
40219019Sgabor    main (String[] args)
41219019Sgabor        throws Throwable {
42219019Sgabor        ATestCaseScaffold   test = new StressGetObjectSizeApp(args[0]);
43219019Sgabor        test.runTest();
44219019Sgabor    }
45219019Sgabor
46219019Sgabor    protected final void
47219019Sgabor    doRunTest()
48219019Sgabor        throws Throwable {
49219019Sgabor        stressGetObjectSize();
50219019Sgabor    }
51219019Sgabor
52219019Sgabor    public void stressGetObjectSize() {
53219019Sgabor        System.out.println("main: an object size=" +
54219019Sgabor            fInst.getObjectSize(new Object()));
55219019Sgabor
56219019Sgabor        RoundAndRound[] threads = new RoundAndRound[10];
57219019Sgabor        for (int i = 0; i < threads.length; ++i) {
58219019Sgabor            threads[i] = new RoundAndRound(fInst);
59219019Sgabor            threads[i].start();
60219019Sgabor        }
61219019Sgabor        try {
62219019Sgabor            Thread.sleep(500); // let all threads get going in their loops
63219019Sgabor        } catch (InterruptedException ie) {
64219019Sgabor        }
65219019Sgabor        System.out.println("stressGetObjectSize: returning");
66219019Sgabor        return;
67219019Sgabor    }
68219019Sgabor
69219019Sgabor    private static class RoundAndRound extends Thread {
70219019Sgabor        private final Instrumentation inst;
71219019Sgabor        private final Object anObject;
72219019Sgabor
73219019Sgabor        public RoundAndRound(Instrumentation inst) {
74219019Sgabor            this.inst = inst;
75219019Sgabor            this.anObject = new Object();
76219019Sgabor            setDaemon(true);
77219019Sgabor        }
78219019Sgabor
79219019Sgabor        public void run() {
80219019Sgabor            long sum = 0;
81219019Sgabor            while (true) {
82219019Sgabor              sum += inst.getObjectSize(anObject);
83219019Sgabor            }
84219019Sgabor        }
85219019Sgabor    }
86219019Sgabor}
87219019Sgabor