KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > stress > ScenarioRunner


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.aop.stress;
23
24 import java.io.FileInputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Random JavaDoc;
32
33 /**
34  *
35  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
36  * @version $Revision: 57349 $
37  */

38 public class ScenarioRunner
39 {
40    private static final String JavaDoc LOOPS = "loops";
41    private static final String JavaDoc THREADS = "threads";
42    private static final String JavaDoc RANDOM_SLEEP_INTERVAL = "random_sleep_interval";
43    private static final String JavaDoc SLEEPTIME_MILLIS = "sleeptime_millis";
44    private static final String JavaDoc LOGGING = "logging";
45    
46    private int loops;
47    private int threads;
48    private boolean randomSleepInterval;
49    private int sleeptimeMillis;
50    Random JavaDoc random = new Random JavaDoc(10);
51    boolean logging;
52
53    
54    public ScenarioRunner()
55    {
56       try
57       {
58          URL JavaDoc url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
59          System.out.println("class url: " + url);
60          String JavaDoc 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 JavaDoc(location);
66          InputStream JavaDoc in = new FileInputStream JavaDoc(url.getFile());
67          Properties JavaDoc properties = new Properties JavaDoc();
68          properties.load(in);
69          
70          loops = Integer.parseInt(properties.getProperty(LOOPS, "10"));
71          threads = Integer.parseInt(properties.getProperty(THREADS, "10"));
72          String JavaDoc 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 JavaDoc e)
88       {
89          throw new RuntimeException JavaDoc(e);
90       }
91    }
92
93    public void executeScenario(Scenario scenario) throws Exception JavaDoc
94    {
95       Scenario[] scenarios = new Scenario[] {scenario};
96       executeScenarios(scenarios);
97    }
98
99    public void executeScenarios(Scenario[] scenarios) throws Exception JavaDoc
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 JavaDoc it = loaders[thread].exceptions.iterator() ; it.hasNext() ; )
129             {
130                ((Exception JavaDoc)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 JavaDoc("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 JavaDoc
169    {
170       int thread;
171       Scenario scenario;
172       int loop;
173       ArrayList JavaDoc exceptions = new ArrayList JavaDoc();
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 JavaDoc e)
192          {
193             e.printStackTrace();
194          }
195          catch(Exception JavaDoc e)
196          {
197             exceptions.add(e);
198             e.printStackTrace();
199          }
200       }
201    }
202 }
203
Popular Tags