1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.agent.test; 34 35 import java.io.IOException; 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.Collection; 39 import java.util.List; 40 41 import org.openjdk.jmc.agent.test.util.TestToolkit; 42 43 public class InstrumentMe { 44 public static void main(String[] args) throws InterruptedException, IOException { 45 Thread runner = new Thread(new Runner(), "InstrumentMe Runner"); 46 runner.setDaemon(true); 47 System.out.println("Press <enter> at any time to quit"); 48 System.out.println("Now starting looping through the instrumentation examples"); 49 runner.start(); 50 System.in.read(); 51 } 52 53 private final static class Runner implements Runnable { 54 public void run() { 55 InstrumentMe instance = new InstrumentMe(); 56 while (true) { 57 try { 58 InstrumentMe.runStatic(); 59 InstrumentMe.runInstance(instance); 60 } catch (InterruptedException e) { 61 } 62 } 63 } 64 } 65 66 private static void runInstance(InstrumentMe instance) throws InterruptedException { 67 instance.printInstanceHelloWorld1(); 68 instance.printInstanceHelloWorld2(TestToolkit.randomString(), TestToolkit.randomLong()); 69 instance.printInstanceHelloWorld3(Gurka.createGurka()); 70 instance.printInstanceHelloWorld4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()}); 71 instance.printInstanceHelloWorld5(createGurkList()); 72 instance.printInstanceHelloWorldJFR1(); 73 instance.printInstanceHelloWorldJFR2(TestToolkit.randomString(), TestToolkit.randomLong()); 74 instance.printInstanceHelloWorldJFR3(Gurka.createGurka()); 75 instance.printInstanceHelloWorldJFR4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()}); 76 instance.printInstanceHelloWorldJFR5(createGurkList()); 77 instance.printInstanceHelloWorldJFR6(); 78 } 79 80 private static void runStatic() throws InterruptedException { 81 System.out.println("Running static versions..."); //$NON-NLS-1$ 82 printHelloWorld1(); 83 printHelloWorld2(TestToolkit.randomString(), TestToolkit.randomLong()); 84 printHelloWorld3(Gurka.createGurka()); 85 printHelloWorld4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()}); 86 printHelloWorld5(createGurkList()); 87 printHelloWorldJFR1(); 88 printHelloWorldJFR2(TestToolkit.randomString(), TestToolkit.randomLong()); 89 printHelloWorldJFR3(Gurka.createGurka()); 90 printHelloWorldJFR4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()}); 91 printHelloWorldJFR5(createGurkList()); 92 printHelloWorldJFR6(); 93 } 94 95 private static Collection<Gurka> createGurkList() { 96 List<Gurka> gurkList = new ArrayList<>(); 97 for (int i = 0; i < TestToolkit.RND.nextInt(4) + 1; i++) { 98 gurkList.add(Gurka.createGurka()); 99 } 100 return gurkList; 101 } 102 103 public static void printHelloWorld1() throws InterruptedException { 104 System.out.println("#S1. Hello World!"); //$NON-NLS-1$ 105 Thread.sleep(1000); 106 } 107 108 public static int printHelloWorld2(String str, long l) throws InterruptedException { 109 int returnval = TestToolkit.RND.nextInt(45); 110 System.out.println(String.format("#S2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$ 111 Thread.sleep(1000); 112 return returnval; 113 } 114 115 public static void printHelloWorld3(Gurka gurka) throws InterruptedException { 116 System.out.println(String.format("#S3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$ 117 Thread.sleep(1000); 118 } 119 120 public static void printHelloWorld4(Gurka[] gurkor) throws InterruptedException { 121 System.out.println(String.format("#S4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$ 122 Thread.sleep(1000); 123 } 124 125 public static void printHelloWorld5(Collection<Gurka> gurkor) throws InterruptedException { 126 System.out.println(String.format("#S5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$ 127 Thread.sleep(1000); 128 } 129 130 public static void printHelloWorldJFR1() throws InterruptedException { 131 System.out.println("#SJFR1. Hello World!"); //$NON-NLS-1$ 132 Thread.sleep(1000); 133 } 134 135 public static int printHelloWorldJFR2(String str, long l) throws InterruptedException { 136 int returnval = TestToolkit.RND.nextInt(45); 137 System.out.println(String.format("#SJFR2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$ 138 Thread.sleep(1000); 139 return returnval; 140 } 141 142 public static void printHelloWorldJFR3(Gurka gurka) throws InterruptedException { 143 System.out.println(String.format("#SJFR3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$ 144 Thread.sleep(1000); 145 } 146 147 public static void printHelloWorldJFR4(Gurka[] gurkor) throws InterruptedException { 148 System.out.println(String.format("#SJFR4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$ 149 Thread.sleep(1000); 150 } 151 152 public static void printHelloWorldJFR5(Collection<Gurka> gurkor) throws InterruptedException { 153 System.out.println(String.format("#SJFR5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$ 154 Thread.sleep(1000); 155 } 156 157 public static double printHelloWorldJFR6() throws InterruptedException { 158 double returnval = TestToolkit.RND.nextDouble() * 100; 159 System.out.println(String.format("#SJFR6. retval:%3.3f", returnval)); //$NON-NLS-1$ 160 Thread.sleep(1000); 161 return returnval; 162 } 163 164 public void printInstanceHelloWorld1() throws InterruptedException { 165 System.out.println("#I1. Hello World!"); //$NON-NLS-1$ 166 Thread.sleep(1000); 167 } 168 169 public int printInstanceHelloWorld2(String str, long l) throws InterruptedException { 170 int returnval = TestToolkit.RND.nextInt(45); 171 System.out.println(String.format("#I2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$ 172 Thread.sleep(1000); 173 return returnval; 174 } 175 176 public void printInstanceHelloWorld3(Gurka gurka) throws InterruptedException { 177 System.out.println(String.format("#I3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$ 178 Thread.sleep(1000); 179 } 180 181 public void printInstanceHelloWorld4(Gurka[] gurkor) throws InterruptedException { 182 System.out.println(String.format("#I4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$ 183 Thread.sleep(1000); 184 } 185 186 public void printInstanceHelloWorld5(Collection<Gurka> gurkor) throws InterruptedException { 187 System.out.println(String.format("#I5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$ 188 Thread.sleep(1000); 189 } 190 191 public void printInstanceHelloWorldJFR1() throws InterruptedException { 192 System.out.println("#IJFR1. Hello World!"); //$NON-NLS-1$ 193 Thread.sleep(1000); 194 } 195 196 public int printInstanceHelloWorldJFR2(String str, long l) throws InterruptedException { 197 int returnval = TestToolkit.RND.nextInt(45); 198 System.out.println(String.format("#IJFR2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$ 199 Thread.sleep(1000); 200 return returnval; 201 } 202 203 public void printInstanceHelloWorldJFR3(Gurka gurka) throws InterruptedException { 204 System.out.println(String.format("#IJFR3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$ 205 Thread.sleep(1000); 206 } 207 208 public void printInstanceHelloWorldJFR4(Gurka[] gurkor) throws InterruptedException { 209 System.out.println(String.format("#IJFR4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$ 210 Thread.sleep(1000); 211 } 212 213 public void printInstanceHelloWorldJFR5(Collection<Gurka> gurkor) throws InterruptedException { 214 System.out.println(String.format("#IJFR5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$ 215 Thread.sleep(1000); 216 } 217 218 public double printInstanceHelloWorldJFR6() throws InterruptedException { 219 double returnval = TestToolkit.RND.nextDouble(); 220 System.out.println(String.format("#IJFR6. retval:%1.3f", returnval)); //$NON-NLS-1$ 221 Thread.sleep(1000); 222 return returnval; 223 } 224 }