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.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.Collection;
  38 import java.util.List;
  39 
  40 import org.openjdk.jmc.agent.test.util.TestToolkit;
  41 
  42 public class InstrumentMe {
  43         public static void main(String[] args) throws InterruptedException {
  44                 InstrumentMe instance = new InstrumentMe();
  45                 while (true) {
  46                         runStatic();
  47                         runInstance(instance);
  48                 }
  49         }
  50 
  51         private static void runInstance(InstrumentMe instance) throws InterruptedException {
  52                 instance.printInstanceHelloWorld1();
  53                 instance.printInstanceHelloWorld2(TestToolkit.randomString(), TestToolkit.randomLong());
  54                 instance.printInstanceHelloWorld3(Gurka.createGurka());
  55                 instance.printInstanceHelloWorld4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()});
  56                 instance.printInstanceHelloWorld5(createGurkList());
  57                 instance.printInstanceHelloWorldJFR1();
  58                 instance.printInstanceHelloWorldJFR2(TestToolkit.randomString(), TestToolkit.randomLong());
  59                 instance.printInstanceHelloWorldJFR3(Gurka.createGurka());
  60                 instance.printInstanceHelloWorldJFR4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()});
  61                 instance.printInstanceHelloWorldJFR5(createGurkList());
  62                 instance.printInstanceHelloWorldJFR6();
  63         }
  64 
  65         private static void runStatic() throws InterruptedException {
  66                 System.out.println("Running static versions..."); //$NON-NLS-1$
  67                 printHelloWorld1();
  68                 printHelloWorld2(TestToolkit.randomString(), TestToolkit.randomLong());
  69                 printHelloWorld3(Gurka.createGurka());
  70                 printHelloWorld4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()});
  71                 printHelloWorld5(createGurkList());
  72                 printHelloWorldJFR1();
  73                 printHelloWorldJFR2(TestToolkit.randomString(), TestToolkit.randomLong());
  74                 printHelloWorldJFR3(Gurka.createGurka());
  75                 printHelloWorldJFR4(new Gurka[] {Gurka.createGurka(), Gurka.createGurka()});
  76                 printHelloWorldJFR5(createGurkList());
  77                 printHelloWorldJFR6();
  78         }
  79 
  80         private static Collection<Gurka> createGurkList() {
  81                 List<Gurka> gurkList = new ArrayList<>();
  82                 for (int i = 0; i < TestToolkit.RND.nextInt(4) + 1; i++) {
  83                         gurkList.add(Gurka.createGurka());
  84                 }
  85                 return gurkList;
  86         }
  87 
  88         public static void printHelloWorld1() throws InterruptedException {
  89                 System.out.println("#S1. Hello World!"); //$NON-NLS-1$
  90                 Thread.sleep(1000);
  91         }
  92 
  93         public static int printHelloWorld2(String str, long l) throws InterruptedException {
  94                 int returnval = TestToolkit.RND.nextInt(45);
  95                 System.out.println(String.format("#S2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$
  96                 Thread.sleep(1000);
  97                 return returnval;
  98         }
  99 
 100         public static void printHelloWorld3(Gurka gurka) throws InterruptedException {
 101                 System.out.println(String.format("#S3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$
 102                 Thread.sleep(1000);
 103         }
 104 
 105         public static void printHelloWorld4(Gurka[] gurkor) throws InterruptedException {
 106                 System.out.println(String.format("#S4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$
 107                 Thread.sleep(1000);
 108         }
 109 
 110         public static void printHelloWorld5(Collection<Gurka> gurkor) throws InterruptedException {
 111                 System.out.println(String.format("#S5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$
 112                 Thread.sleep(1000);
 113         }
 114 
 115         public static void printHelloWorldJFR1() throws InterruptedException {
 116                 System.out.println("#SJFR1. Hello World!"); //$NON-NLS-1$
 117                 Thread.sleep(1000);
 118         }
 119 
 120         public static int printHelloWorldJFR2(String str, long l) throws InterruptedException {
 121                 int returnval = TestToolkit.RND.nextInt(45);
 122                 System.out.println(String.format("#SJFR2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$
 123                 Thread.sleep(1000);
 124                 return returnval;
 125         }
 126 
 127         public static void printHelloWorldJFR3(Gurka gurka) throws InterruptedException {
 128                 System.out.println(String.format("#SJFR3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$
 129                 Thread.sleep(1000);
 130         }
 131 
 132         public static void printHelloWorldJFR4(Gurka[] gurkor) throws InterruptedException {
 133                 System.out.println(String.format("#SJFR4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$
 134                 Thread.sleep(1000);
 135         }
 136 
 137         public static void printHelloWorldJFR5(Collection<Gurka> gurkor) throws InterruptedException {
 138                 System.out.println(String.format("#SJFR5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$
 139                 Thread.sleep(1000);
 140         }
 141 
 142         public static double printHelloWorldJFR6() throws InterruptedException {
 143                 double returnval = TestToolkit.RND.nextDouble() * 100;
 144                 System.out.println(String.format("#SJFR6. retval:%3.3f", returnval)); //$NON-NLS-1$
 145                 Thread.sleep(1000);
 146                 return returnval;
 147         }
 148 
 149         public void printInstanceHelloWorld1() throws InterruptedException {
 150                 System.out.println("#I1. Hello World!"); //$NON-NLS-1$
 151                 Thread.sleep(1000);
 152         }
 153 
 154         public int printInstanceHelloWorld2(String str, long l) throws InterruptedException {
 155                 int returnval = TestToolkit.RND.nextInt(45);
 156                 System.out.println(String.format("#I2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$
 157                 Thread.sleep(1000);
 158                 return returnval;
 159         }
 160 
 161         public void printInstanceHelloWorld3(Gurka gurka) throws InterruptedException {
 162                 System.out.println(String.format("#I3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$
 163                 Thread.sleep(1000);
 164         }
 165 
 166         public void printInstanceHelloWorld4(Gurka[] gurkor) throws InterruptedException {
 167                 System.out.println(String.format("#I4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$
 168                 Thread.sleep(1000);
 169         }
 170 
 171         public void printInstanceHelloWorld5(Collection<Gurka> gurkor) throws InterruptedException {
 172                 System.out.println(String.format("#I5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$
 173                 Thread.sleep(1000);
 174         }
 175 
 176         public void printInstanceHelloWorldJFR1() throws InterruptedException {
 177                 System.out.println("#IJFR1. Hello World!"); //$NON-NLS-1$
 178                 Thread.sleep(1000);
 179         }
 180 
 181         public int printInstanceHelloWorldJFR2(String str, long l) throws InterruptedException {
 182                 int returnval = TestToolkit.RND.nextInt(45);
 183                 System.out.println(String.format("#IJFR2. Str:%s long:%d retval:%d", str, l, returnval)); //$NON-NLS-1$
 184                 Thread.sleep(1000);
 185                 return returnval;
 186         }
 187 
 188         public void printInstanceHelloWorldJFR3(Gurka gurka) throws InterruptedException {
 189                 System.out.println(String.format("#IJFR3. Got a gurka with id: %d", gurka.getID())); //$NON-NLS-1$
 190                 Thread.sleep(1000);
 191         }
 192 
 193         public void printInstanceHelloWorldJFR4(Gurka[] gurkor) throws InterruptedException {
 194                 System.out.println(String.format("#IJFR4. Got gurkor: %s", Arrays.toString(gurkor))); //$NON-NLS-1$
 195                 Thread.sleep(1000);
 196         }
 197 
 198         public void printInstanceHelloWorldJFR5(Collection<Gurka> gurkor) throws InterruptedException {
 199                 System.out.println(String.format("#IJFR5. Got gurkor: %s", String.valueOf(gurkor))); //$NON-NLS-1$
 200                 Thread.sleep(1000);
 201         }
 202 
 203         public double printInstanceHelloWorldJFR6() throws InterruptedException {
 204                 double returnval = TestToolkit.RND.nextDouble();
 205                 System.out.println(String.format("#IJFR6. retval:%1.3f", returnval)); //$NON-NLS-1$
 206                 Thread.sleep(1000);
 207                 return returnval;
 208         }
 209 }