1 18 19 package org.apache.jmeter.threads; 20 21 import java.io.Serializable ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 27 import org.apache.jmeter.control.Controller; 28 import org.apache.jmeter.control.LoopController; 29 import org.apache.jmeter.engine.event.LoopIterationListener; 30 import org.apache.jmeter.samplers.RemoteSampleListener; 31 import org.apache.jmeter.samplers.SampleEvent; 32 import org.apache.jmeter.samplers.SampleListener; 33 import org.apache.jmeter.samplers.Sampler; 34 import org.apache.jmeter.testelement.AbstractTestElement; 35 import org.apache.jmeter.testelement.TestElement; 36 import org.apache.jmeter.testelement.property.IntegerProperty; 37 import org.apache.jmeter.testelement.property.LongProperty; 38 import org.apache.jmeter.testelement.property.BooleanProperty; 39 import org.apache.jmeter.testelement.property.TestElementProperty; 40 import org.apache.jorphan.logging.LoggingManager; 41 import org.apache.log.Logger; 42 43 49 public class ThreadGroup 50 extends AbstractTestElement 51 implements SampleListener, Serializable , Controller 52 { 53 private static Logger log = LoggingManager.getLoggerForClass(); 54 55 public final static String NUM_THREADS = "ThreadGroup.num_threads"; 56 public final static String RAMP_TIME = "ThreadGroup.ramp_time"; 57 public final static String MAIN_CONTROLLER = "ThreadGroup.main_controller"; 58 59 public final static String SCHEDULER = "ThreadGroup.scheduler"; 60 public final static String START_TIME= "ThreadGroup.start_time"; 61 public final static String END_TIME= "ThreadGroup.end_time"; 62 public final static String DURATION = "ThreadGroup.duration"; 63 public final static String DELAY = "ThreadGroup.delay"; 64 65 66 67 public final static String ON_SAMPLE_ERROR= "ThreadGroup.on_sample_error"; public final static String ON_SAMPLE_ERROR_CONTINUE = "continue"; 69 public final static String ON_SAMPLE_ERROR_STOPTHREAD = "stopthread"; 70 public final static String ON_SAMPLE_ERROR_STOPTEST = "stoptest"; 71 72 private final static int DEFAULT_NUM_THREADS = 1; 73 private final static int DEFAULT_RAMP_UP = 0; 74 private SampleQueue queue = null; 75 private LinkedList listeners = new LinkedList (); 76 private LinkedList remoteListeners = new LinkedList (); 77 78 81 public ThreadGroup() 82 { 83 } 84 85 90 public void setNumThreads(int numThreads) 91 { 92 setProperty(new IntegerProperty(NUM_THREADS, numThreads)); 93 } 94 95 public boolean isDone() 96 { 97 return getSamplerController().isDone(); 98 } 99 100 public Sampler next() 101 { 102 return getSamplerController().next(); 103 } 104 105 106 107 112 public void setScheduler(boolean Scheduler) 113 { 114 setProperty(new BooleanProperty(SCHEDULER,Scheduler)); 115 } 116 117 122 public boolean getScheduler() 123 { 124 return getPropertyAsBoolean(SCHEDULER); 125 } 126 127 132 public void setStartTime(long stime) 133 { 134 setProperty(new LongProperty(START_TIME,stime)); 135 } 136 137 142 public long getStartTime() 143 { 144 return getPropertyAsLong(START_TIME); 145 } 146 147 152 public long getDuration() 153 { 154 return getPropertyAsLong(DURATION); 155 } 156 157 162 public void setDuration(long duration) 163 { 164 setProperty(new LongProperty(DURATION,duration)); 165 } 166 167 172 public long getDelay() 173 { 174 return getPropertyAsLong(DELAY); 175 } 176 177 182 public void setDelay(long delay) 183 { 184 setProperty(new LongProperty(DELAY,delay)); 185 } 186 187 188 189 194 public void setEndTime(long etime) 195 { 196 setProperty(new LongProperty(END_TIME, etime)); 197 } 198 199 204 public long getEndTime() 205 { 206 return getPropertyAsLong(END_TIME); 207 } 208 209 214 public void setRampUp(int rampUp) 215 { 216 setProperty(new IntegerProperty(RAMP_TIME,rampUp)); 217 } 218 219 224 public int getRampUp() 225 { 226 return getPropertyAsInt(ThreadGroup.RAMP_TIME); 227 } 228 229 234 public Controller getSamplerController() 235 { 236 return (Controller) getProperty(MAIN_CONTROLLER).getObjectValue(); 237 } 238 239 244 public void setSamplerController(LoopController c) 245 { 246 c.setContinueForever(false); 247 setProperty(new TestElementProperty(MAIN_CONTROLLER, c)); 248 } 249 250 255 public int getNumThreads() 256 { 257 return this.getPropertyAsInt(ThreadGroup.NUM_THREADS); 258 } 259 260 265 public int getDefaultNumThreads() 266 { 267 return DEFAULT_NUM_THREADS; 268 } 269 270 275 public int getDefaultRampUp() 276 { 277 return DEFAULT_RAMP_UP; 278 } 279 280 285 public void addTestElement(TestElement child) 286 { 287 getSamplerController().addTestElement(child); 288 } 289 290 295 public void sampleOccurred(SampleEvent e) 296 { 297 if (queue == null) 298 { 299 queue = new SampleQueue(); 300 Thread thread = new Thread (queue); 301 thread.start(); 303 } 304 queue.sampleOccurred(e); 305 } 306 307 312 public void sampleStarted(SampleEvent e) 313 { 314 } 315 316 321 public void sampleStopped(SampleEvent e) 322 { 323 } 324 325 333 private class SampleQueue implements Runnable , Serializable 334 { 335 List occurredQ = Collections.synchronizedList(new LinkedList ()); 336 337 340 public SampleQueue() 341 { 342 } 343 344 349 public synchronized void sampleOccurred(SampleEvent e) 350 { 351 occurredQ.add(e); 352 this.notifyAll(); 353 } 354 355 360 public void run() 361 { 362 SampleEvent event = null; 363 while (true) 364 { 365 try 366 { 367 event = (SampleEvent) occurredQ.remove(0); 368 } 369 catch (Exception ex) 370 { 371 waitForSamples(); 372 continue; 373 } 374 try 375 { 376 if (event != null) 377 { 378 Iterator iter = listeners.iterator(); 379 while (iter.hasNext()) 380 { 381 ((SampleListener) iter.next()).sampleOccurred( 382 event); 383 } 384 iter = remoteListeners.iterator(); 385 while (iter.hasNext()) 386 { 387 try 388 { 389 ( 390 (RemoteSampleListener) iter 391 .next()) 392 .sampleOccurred( 393 event); 394 } 395 catch (Exception ex) 396 { 397 log.error("", ex); 398 } 399 } 400 } 401 else 402 { 403 waitForSamples(); 404 } 405 } 406 catch (Throwable ex) 407 { 408 log.error("", ex); 409 } 410 411 } 412 } 413 414 private synchronized void waitForSamples() 415 { 416 try 417 { 418 this.wait(); 419 } 420 catch (Exception ex) 421 { 422 log.error("", ex); 423 } 424 } 425 } 426 427 430 public void addIterationListener(LoopIterationListener lis) 431 { 432 getSamplerController().addIterationListener(lis); 433 } 434 435 438 public void initialize() 439 { 440 getSamplerController().initialize(); 441 } 442 443 448 public boolean getOnErrorStopThread() 449 { 450 return getPropertyAsString(ThreadGroup.ON_SAMPLE_ERROR) 451 .equalsIgnoreCase(ON_SAMPLE_ERROR_STOPTHREAD); 452 } 453 454 459 public boolean getOnErrorStopTest() 460 { 461 return getPropertyAsString(ThreadGroup.ON_SAMPLE_ERROR) 462 .equalsIgnoreCase(ON_SAMPLE_ERROR_STOPTEST); 463 } 464 465 } 466 | Popular Tags |