1 18 19 package org.apache.jmeter.control; 20 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 24 import org.apache.jmeter.engine.event.LoopIterationEvent; 25 import org.apache.jmeter.engine.event.LoopIterationListener; 26 import org.apache.jmeter.junit.JMeterTestCase; 27 import org.apache.jmeter.junit.stubs.TestSampler; 28 import org.apache.jmeter.samplers.Sampler; 29 import org.apache.jmeter.testelement.TestElement; 30 import org.apache.jmeter.testelement.TestListener; 31 import org.apache.jmeter.testelement.property.BooleanProperty; 32 import org.apache.jmeter.testelement.property.FloatProperty; 33 import org.apache.jmeter.testelement.property.IntegerProperty; 34 import org.apache.jmeter.testelement.property.JMeterProperty; 35 import org.apache.jmeter.testelement.property.StringProperty; 36 37 46 public class ThroughputController 47 extends GenericController 48 implements Serializable , LoopIterationListener, TestListener 49 { 50 51 public static final int BYNUMBER = 0; 52 public static final int BYPERCENT = 1; 53 54 private static final String STYLE = "ThroughputController.style"; 55 private static final String PERTHREAD = "ThroughputController.perThread"; 56 private static final String MAXTHROUGHPUT = 57 "ThroughputController.maxThroughput"; 58 private static final String PERCENTTHROUGHPUT = 59 "ThroughputController.percentThroughput"; 60 61 private int globalNumExecutions; 62 private int globalIteration; 63 private transient Object counterLock; 64 65 68 private int numExecutions = 0; 69 70 73 private int iteration = -1; 74 75 78 private boolean runThisTime; 79 80 public ThroughputController() 81 { 82 globalNumExecutions = 0; 83 globalIteration = -1; 84 counterLock = new Object (); 85 setStyle(BYNUMBER); 86 setPerThread(true); 87 setMaxThroughput(1); 88 setPercentThroughput(100); 89 runThisTime = false; 90 } 91 92 public void setStyle(int style) 93 { 94 setProperty(new IntegerProperty(STYLE, style)); 95 } 96 97 public int getStyle() 98 { 99 return getPropertyAsInt(STYLE); 100 } 101 102 public void setPerThread(boolean perThread) 103 { 104 setProperty(new BooleanProperty(PERTHREAD, perThread)); 105 } 106 107 public boolean isPerThread() 108 { 109 return getPropertyAsBoolean(PERTHREAD); 110 } 111 112 public void setMaxThroughput(int maxThroughput) 113 { 114 setProperty(new IntegerProperty(MAXTHROUGHPUT, maxThroughput)); 115 } 116 117 public void setMaxThroughput(String maxThroughput) 118 { 119 setProperty(new StringProperty(MAXTHROUGHPUT, maxThroughput)); 120 } 121 122 public String getMaxThroughput() 123 { 124 return getPropertyAsString(MAXTHROUGHPUT); 125 } 126 127 protected int getMaxThroughputAsInt() 128 { 129 JMeterProperty prop = getProperty(MAXTHROUGHPUT); 130 int retVal = 1; 131 if (prop instanceof IntegerProperty) 132 { 133 retVal = (((IntegerProperty) prop).getIntValue()); 134 } 135 else 136 { 137 try 138 { 139 retVal = Integer.parseInt(prop.getStringValue()); 140 } 141 catch (NumberFormatException e) 142 { 143 } 144 } 145 return retVal; 146 } 147 148 public void setPercentThroughput(float percentThroughput) 149 { 150 setProperty(new FloatProperty(PERCENTTHROUGHPUT, percentThroughput)); 151 } 152 153 public void setPercentThroughput(String percentThroughput) 154 { 155 setProperty(new StringProperty(PERCENTTHROUGHPUT, percentThroughput)); 156 } 157 158 public String getPercentThroughput() 159 { 160 return getPropertyAsString(PERCENTTHROUGHPUT); 161 } 162 163 protected float getPercentThroughputAsFloat() 164 { 165 JMeterProperty prop = getProperty(PERCENTTHROUGHPUT); 166 float retVal = 100; 167 if (prop instanceof FloatProperty) 168 { 169 retVal = (((FloatProperty) prop).getFloatValue()); 170 } 171 else 172 { 173 try 174 { 175 retVal = Float.parseFloat(prop.getStringValue()); 176 } 177 catch (NumberFormatException e) 178 { 179 } 180 } 181 return retVal; 182 } 183 184 protected void setExecutions(int executions) 185 { 186 if (!isPerThread()) 187 { 188 globalNumExecutions=executions; 189 } 190 this.numExecutions = executions; 191 } 192 193 protected int getExecutions() 194 { 195 if (!isPerThread()) 196 { 197 return globalNumExecutions; 198 } 199 else 200 { 201 return numExecutions; 202 } 203 } 204 205 private void increaseExecutions() 206 { 207 setExecutions(getExecutions() + 1); 208 } 209 210 protected void setIteration(int iteration) 211 { 212 if (!isPerThread()) 213 { 214 globalIteration=iteration; 215 } 216 this.iteration = iteration; 217 } 218 219 protected int getIteration() 220 { 221 if (!isPerThread()) 222 { 223 return globalIteration; 224 } 225 else 226 { 227 return iteration; 228 } 229 } 230 231 private void increaseIteration() 232 { 233 setIteration(getIteration() + 1); 234 } 235 236 239 public Sampler next() 240 { 241 if (runThisTime) 242 { 243 return super.next(); 244 } 245 return null; 246 } 247 248 251 private boolean decide() 252 { 253 int executions, iterations; 254 255 executions = getExecutions(); 256 iterations = getIteration(); 257 if (getStyle() == BYNUMBER) 258 { 259 return executions < getMaxThroughputAsInt(); 260 } 261 else 262 { 263 return (100.0*executions+50.0) / (iterations+1) 264 < getPercentThroughputAsFloat(); 265 } 266 } 267 268 271 public boolean isDone() 272 { 273 if (subControllersAndSamplers.size() == 0) 274 { 275 return true; 276 } 277 else if ( 278 getStyle() == BYNUMBER 279 && getExecutions() >= getMaxThroughputAsInt() 280 && current >= getSubControllers().size()) 281 { 282 return true; 283 } 284 else 285 { 286 return false; 287 } 288 } 289 290 public Object clone() 291 { 292 ThroughputController clone = (ThroughputController) super.clone(); 293 clone.numExecutions = numExecutions; 294 clone.iteration = iteration; 295 clone.globalNumExecutions = globalNumExecutions; 296 clone.globalIteration = globalIteration; 297 clone.counterLock = counterLock; 298 clone.runThisTime = false; 299 return clone; 300 } 301 302 private void readObject(java.io.ObjectInputStream in) 303 throws IOException , ClassNotFoundException 304 { 305 in.defaultReadObject(); 306 counterLock = new Object (); 307 } 308 309 312 public void iterationStart(LoopIterationEvent iterEvent) 313 { 314 if (!isPerThread()) 315 { 316 synchronized (counterLock) 317 { 318 increaseIteration(); 319 runThisTime= decide(); 320 if (runThisTime) increaseExecutions(); 321 } 322 } 323 else 324 { 325 increaseIteration(); 326 runThisTime= decide(); 327 if (runThisTime) increaseExecutions(); 328 } 329 } 330 331 335 public void testStarted() 336 { 337 setExecutions(0); 338 setIteration(-1); 339 } 340 341 345 public void testEnded() 346 { 347 } 348 349 353 public void testStarted(String host) 354 { 355 } 356 357 361 public void testEnded(String host) 362 { 363 } 364 365 369 public void testIterationStart(LoopIterationEvent event) 370 { 371 } 372 373 375 public static class Test extends JMeterTestCase 376 { 377 public Test(String name) 378 { 379 super(name); 380 } 381 382 public void testByNumber() throws Exception 383 { 384 ThroughputController sub_1 = new ThroughputController(); 385 sub_1.setStyle(BYNUMBER); 386 sub_1.setMaxThroughput(2); 387 sub_1.addTestElement(new TestSampler("one")); 388 sub_1.addTestElement(new TestSampler("two")); 389 390 LoopController loop = new LoopController(); 391 loop.setLoops(5); 392 loop.addTestElement(new TestSampler("zero")); 393 loop.addTestElement(sub_1); 394 loop.addIterationListener(sub_1); 395 loop.addTestElement(new TestSampler("three")); 396 397 LoopController test = new LoopController(); 398 test.setLoops(2); 399 test.addTestElement(loop); 400 401 String [] order = 402 new String [] { 403 "zero", 404 "one", 405 "two", 406 "three", 407 "zero", 408 "one", 409 "two", 410 "three", 411 "zero", 412 "three", 413 "zero", 414 "three", 415 "zero", 416 "three", 417 "zero", 418 "three", 419 "zero", 420 "three", 421 "zero", 422 "three", 423 "zero", 424 "three", 425 "zero", 426 "three", 427 }; 428 sub_1.testStarted(); 429 test.initialize(); 430 for (int counter= 0; counter < order.length; counter++) 431 { 432 TestElement sampler = test.next(); 433 assertNotNull(sampler); 434 assertEquals("Counter: "+counter 435 +", executions: "+sub_1.getExecutions() 436 +", iteration: "+sub_1.getIteration(), 437 order[counter], 438 sampler.getPropertyAsString(TestElement.NAME) 439 ); 440 } 441 assertNull(test.next()); 442 sub_1.testEnded(); 443 } 444 445 public void testByNumberZero() throws Exception 446 { 447 ThroughputController sub_1 = new ThroughputController(); 448 sub_1.setStyle(BYNUMBER); 449 sub_1.setMaxThroughput(0); 450 sub_1.addTestElement(new TestSampler("one")); 451 sub_1.addTestElement(new TestSampler("two")); 452 453 LoopController controller = new LoopController(); 454 controller.setLoops(5); 455 controller.addTestElement(new TestSampler("zero")); 456 controller.addTestElement(sub_1); 457 controller.addIterationListener(sub_1); 458 controller.addTestElement(new TestSampler("three")); 459 460 String [] order = 461 new String [] { 462 "zero", 463 "three", 464 "zero", 465 "three", 466 "zero", 467 "three", 468 "zero", 469 "three", 470 "zero", 471 "three", 472 }; 473 int counter= 0; 474 sub_1.testStarted(); 475 controller.initialize(); 476 for (int i=0; i<3; i++) 477 { 478 TestElement sampler = null; 479 while ((sampler = controller.next()) != null) 480 { 481 assertEquals("Counter: "+counter+", i: "+i, 482 order[counter], 483 sampler.getPropertyAsString(TestElement.NAME) 484 ); 485 counter++; 486 } 487 assertEquals(counter, order.length); 488 counter= 0; 489 } 490 sub_1.testEnded(); 491 } 492 493 public void testByPercent33() throws Exception 494 { 495 ThroughputController sub_1 = new ThroughputController(); 496 sub_1.setStyle(BYPERCENT); 497 sub_1.setPercentThroughput(33.33f); 498 sub_1.addTestElement(new TestSampler("one")); 499 sub_1.addTestElement(new TestSampler("two")); 500 501 LoopController controller = new LoopController(); 502 controller.setLoops(6); 503 controller.addTestElement(new TestSampler("zero")); 504 controller.addTestElement(sub_1); 505 controller.addIterationListener(sub_1); 506 controller.addTestElement(new TestSampler("three")); 507 String [] order = 510 new String [] { 511 "zero", "three", 513 "zero", "one", 515 "two", 516 "three", 517 "zero", "three", 519 "zero", "three", 521 "zero", "one", 523 "two", 524 "three", 525 "zero", "three", 527 }; 529 int counter= 0; 530 sub_1.testStarted(); 531 controller.initialize(); 532 for (int i=0; i<3; i++) 533 { 534 TestElement sampler = null; 535 while ((sampler = controller.next()) != null) 536 { 537 assertEquals("Counter: "+counter+", i: "+i, 538 order[counter], 539 sampler.getPropertyAsString(TestElement.NAME) 540 ); 541 counter++; 542 } 543 assertEquals(counter, order.length); 544 counter= 0; 545 } 546 sub_1.testEnded(); 547 } 548 549 public void testByPercentZero() throws Exception 550 { 551 ThroughputController sub_1 = new ThroughputController(); 552 sub_1.setStyle(BYPERCENT); 553 sub_1.setPercentThroughput(0.0f); 554 sub_1.addTestElement(new TestSampler("one")); 555 sub_1.addTestElement(new TestSampler("two")); 556 557 LoopController controller = new LoopController(); 558 controller.setLoops(150); 559 controller.addTestElement(new TestSampler("zero")); 560 controller.addTestElement(sub_1); 561 controller.addIterationListener(sub_1); 562 controller.addTestElement(new TestSampler("three")); 563 564 String [] order = 565 new String [] { 566 "zero", 567 "three", 568 }; 569 int counter= 0; 570 sub_1.testStarted(); 571 controller.initialize(); 572 for (int i=0; i<3; i++) 573 { 574 TestElement sampler = null; 575 while ((sampler = controller.next()) != null) 576 { 577 assertEquals("Counter: "+counter+", i: "+i, 578 order[counter%order.length], 579 sampler.getPropertyAsString(TestElement.NAME) 580 ); 581 counter++; 582 } 583 assertEquals(counter, 150*order.length); 584 counter= 0; 585 } 586 sub_1.testEnded(); 587 } 588 589 public void testByPercent100() throws Exception 590 { 591 ThroughputController sub_1 = new ThroughputController(); 592 sub_1.setStyle(BYPERCENT); 593 sub_1.setPercentThroughput(100.0f); 594 sub_1.addTestElement(new TestSampler("one")); 595 sub_1.addTestElement(new TestSampler("two")); 596 597 LoopController controller = new LoopController(); 598 controller.setLoops(150); 599 controller.addTestElement(new TestSampler("zero")); 600 controller.addTestElement(sub_1); 601 controller.addIterationListener(sub_1); 602 controller.addTestElement(new TestSampler("three")); 603 604 String [] order = 605 new String [] { 606 "zero", 607 "one", 608 "two", 609 "three", 610 }; 611 int counter= 0; 612 sub_1.testStarted(); 613 controller.initialize(); 614 for (int i=0; i<3; i++) 615 { 616 TestElement sampler = null; 617 while ((sampler = controller.next()) != null) 618 { 619 assertEquals("Counter: "+counter+", i: "+i, 620 order[counter%order.length], 621 sampler.getPropertyAsString(TestElement.NAME) 622 ); 623 counter++; 624 } 625 assertEquals(counter, 150*order.length); 626 counter= 0; 627 } 628 sub_1.testEnded(); 629 } 630 } 631 } 632 | Popular Tags |