1 22 package org.jboss.test.aop.stress; 23 24 import java.io.FileInputStream ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Iterator ; 30 import java.util.Properties ; 31 import java.util.Random ; 32 33 38 public class ScenarioRunner 39 { 40 private static final String LOOPS = "loops"; 41 private static final String THREADS = "threads"; 42 private static final String RANDOM_SLEEP_INTERVAL = "random_sleep_interval"; 43 private static final String SLEEPTIME_MILLIS = "sleeptime_millis"; 44 private static final String LOGGING = "logging"; 45 46 private int loops; 47 private int threads; 48 private boolean randomSleepInterval; 49 private int sleeptimeMillis; 50 Random random = new Random (10); 51 boolean logging; 52 53 54 public ScenarioRunner() 55 { 56 try 57 { 58 URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation(); 59 System.out.println("class url: " + url); 60 String location = url.toString(); 61 int index = location.indexOf("/output/"); 62 location = location.substring(0, index); 63 64 location = location + "/src/resources/test/stress/config.properties"; 65 url = new URL (location); 66 InputStream in = new FileInputStream (url.getFile()); 67 Properties properties = new Properties (); 68 properties.load(in); 69 70 loops = Integer.parseInt(properties.getProperty(LOOPS, "10")); 71 threads = Integer.parseInt(properties.getProperty(THREADS, "10")); 72 String bool = properties.getProperty(RANDOM_SLEEP_INTERVAL, "false"); 73 randomSleepInterval = bool.equals("true"); 74 sleeptimeMillis = Integer.parseInt(properties.getProperty(SLEEPTIME_MILLIS, "100")); 75 bool = properties.getProperty(LOGGING, "false"); 76 logging = bool.equals("true"); 77 78 System.out.println("============================================"); 79 System.out.println("Configured ScenarioRunner"); 80 System.out.println(" loops: " + loops); 81 System.out.println(" threads: " + threads); 82 System.out.println(" Random sleep Interval: " + randomSleepInterval); 83 System.out.println(" Sleep time millis: " + randomSleepInterval); 84 System.out.println(" Logging: " + logging); 85 System.out.println("============================================"); 86 } 87 catch (Exception e) 88 { 89 throw new RuntimeException (e); 90 } 91 } 92 93 public void executeScenario(Scenario scenario) throws Exception 94 { 95 Scenario[] scenarios = new Scenario[] {scenario}; 96 executeScenarios(scenarios); 97 } 98 99 public void executeScenarios(Scenario[] scenarios) throws Exception 100 { 101 System.out.println("Starting run with Scenarios " + Arrays.asList(scenarios)); 102 long start = System.currentTimeMillis(); 103 104 ScenarioLoader[] loaders = new ScenarioLoader[threads]; 105 for (int thread = 0 ; thread < threads ; thread++) 106 { 107 loaders[thread] = getLoader(thread, scenarios); 108 } 109 110 System.out.println("Starting threads..."); 111 for (int thread = 0 ; thread < loaders.length ; thread++) 112 { 113 loaders[thread].start(); 114 } 115 116 for (int thread = 0 ; thread < loaders.length ; thread++) 117 { 118 loaders[thread].join(); 119 } 120 121 long end = System.currentTimeMillis(); 122 boolean hadExceptions = false; 123 for (int thread = 0 ; thread < loaders.length ; thread++) 124 { 125 if (loaders[thread].exceptions.size() > 0) 126 { 127 hadExceptions = true; 128 for (Iterator it = loaders[thread].exceptions.iterator() ; it.hasNext() ; ) 129 { 130 ((Exception )it.next()).printStackTrace(System.err); 131 } 132 } 133 } 134 135 System.out.println("--- DONE --- test took " + (end - start) + " ms"); 136 137 if (hadExceptions) 138 { 139 throw new Exception ("Exceptions occurred, see System.err"); 140 } 141 } 142 143 144 private ScenarioLoader getLoader(int thread, Scenario[] scenarios) 145 { 146 int num = thread % scenarios.length; 147 Scenario scenario = scenarios[num]; 148 if (logging) 149 { 150 scenario = new ScenarioLoggingDecorator(scenario); 151 } 152 return new ScenarioLoader(scenario, thread); 153 } 154 155 private int getSleepInterval() 156 { 157 if(sleeptimeMillis ==0) return sleeptimeMillis; 158 159 if(randomSleepInterval) 160 { 161 return random.nextInt(sleeptimeMillis); 162 } else 163 { 164 return sleeptimeMillis; 165 } 166 } 167 168 private class ScenarioLoader extends Thread 169 { 170 int thread; 171 Scenario scenario; 172 int loop; 173 ArrayList exceptions = new ArrayList (); 174 175 ScenarioLoader(Scenario scenario, int thread) 176 { 177 this.scenario = scenario; 178 this.thread = thread; 179 } 180 181 public void run() 182 { 183 try 184 { 185 while (loop++ < loops) 186 { 187 scenario.execute(thread, loop); 188 Thread.sleep(getSleepInterval()); 189 } 190 } 191 catch (InterruptedException e) 192 { 193 e.printStackTrace(); 194 } 195 catch(Exception e) 196 { 197 exceptions.add(e); 198 e.printStackTrace(); 199 } 200 } 201 } 202 } 203 | Popular Tags |