1 18 19 package org.apache.jmeter.samplers; 20 21 import java.io.Serializable ; 22 import java.io.StringWriter ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.avalon.framework.configuration.Configuration; 32 import org.apache.jmeter.assertions.AssertionResult; 33 import org.apache.jmeter.util.JMeterUtils; 34 import org.apache.jorphan.logging.LoggingManager; 35 import org.apache.log.LogTarget; 36 import org.apache.log.Logger; 37 import org.apache.log.format.Formatter; 38 import org.apache.log.format.RawFormatter; 39 import org.apache.log.output.io.WriterTarget; 40 41 47 public class SampleResult implements Serializable 48 { 49 private static final String DEFAULT_ENCODING = 53 JMeterUtils.getPropDefault("sampleresult.default.encoding","ISO-8859-1"); 54 55 61 public final static String TEXT = "text"; 62 63 69 public final static String BINARY = "bin"; 70 71 72 private static final byte [] EMPTY_BA = new byte [0]; 73 private static final SampleResult [] EMPTY_SR = new SampleResult[0]; 74 private static final AssertionResult [] EMPTY_AR = new AssertionResult[0]; 75 76 private byte[] responseData = EMPTY_BA; 77 private String responseCode; 78 private String label; 79 private String samplerData; 80 private String threadName; 81 private String responseMessage=""; 82 private String responseHeaders=""; private String contentType=""; private String requestHeaders=""; 85 private long timeStamp = 0; private long startTime = 0; 87 private long endTime = 0; 88 private long idleTime = 0; private long pauseTime = 0; private List assertionResults; 91 private List subResults; 92 private String dataType; 93 private boolean success; 94 private Set files; 95 private String dataEncoding; private long time = 0; 97 private boolean stopThread = false; private boolean stopTest = false; private boolean isMonitor = false; 100 102 private final static String TOTAL_TIME = "totalTime"; 103 104 transient private static Logger log = LoggingManager.getLoggerForClass(); 105 106 private static final boolean startTimeStamp = 107 JMeterUtils.getPropDefault("sampleresult.timestamp.start",false); 108 109 static{ 110 if (startTimeStamp){ 111 log.info("Note: Sample TimeStamps are START times"); 112 } else { 113 log.info("Note: Sample TimeStamps are END times"); 114 } 115 log.info("sampleresult.default.encoding is set to "+DEFAULT_ENCODING); 116 } 117 public SampleResult() 118 { 119 time = 0; 120 } 121 122 128 public SampleResult(SampleResult res) 129 { 130 setStartTime(res.getStartTime()); 131 setTime(0); 132 133 setSampleLabel(res.getSampleLabel()); 134 setRequestHeaders(res.getRequestHeaders()); 135 setResponseData(res.getResponseData()); 136 setResponseCode(res.getResponseCode()); 137 setSuccessful(res.isSuccessful()); 138 setResponseMessage(res.getResponseMessage()); 139 setDataType(res.getDataType()); 140 setResponseHeaders(res.getResponseHeaders()); 141 142 addSubResult(res); } 144 145 154 protected SampleResult(long elapsed, boolean atend) 155 { 156 long now = System.currentTimeMillis(); 157 if (atend){ 158 setTimes(now - elapsed, now); 159 } else { 160 setTimes(now, now + elapsed); 161 } 162 } 163 164 173 public static SampleResult createTestSample(long start, long end) 174 { 175 SampleResult res = new SampleResult(); 176 res.setStartTime(start); 177 res.setEndTime(end); 178 return res; 179 } 180 181 187 public static SampleResult createTestSample(long elapsed) 188 { 189 long now = System.currentTimeMillis(); 190 return createTestSample(now,now+elapsed); 191 } 192 193 202 public SampleResult(long stamp, long elapsed) 203 { 204 if (startTimeStamp) { 206 setTimes(stamp, stamp + elapsed); 207 } else { 208 setTimes(stamp - elapsed, stamp); 209 } 210 } 211 212 222 public void setTime(long elapsed){ 223 long now = System.currentTimeMillis(); 224 setTimes(now-elapsed,now); 225 } 226 227 public void setMarked(String filename) 228 { 229 if (files == null) 230 { 231 files = new HashSet (); 232 } 233 files.add(filename); 234 } 235 236 public boolean isMarked(String filename) 237 { 238 return files != null && files.contains(filename); 239 } 240 241 public String getResponseCode() 242 { 243 return responseCode; 244 } 245 246 public void setResponseCode(String code) 247 { 248 responseCode = code; 249 } 250 251 public String getResponseMessage() 252 { 253 return responseMessage; 254 } 255 256 public void setResponseMessage(String msg) 257 { 258 responseMessage = msg; 259 } 260 261 public String getThreadName() 262 { 263 return threadName; 264 } 265 266 public void setThreadName(String threadName) 267 { 268 this.threadName = threadName; 269 } 270 271 public long getTimeStamp() 272 { 273 return timeStamp; 274 } 275 276 public String getSampleLabel() 277 { 278 return label; 279 } 280 281 public void setSampleLabel(String label) 282 { 283 this.label = label; 284 } 285 286 public void addAssertionResult(AssertionResult assertResult) 287 { 288 if (assertionResults == null) 289 { 290 assertionResults = new ArrayList (); 291 } 292 assertionResults.add(assertResult); 293 } 294 295 301 public AssertionResult[] getAssertionResults() 302 { 303 if (assertionResults == null) 304 { 305 return EMPTY_AR; 306 } 307 return (AssertionResult[]) assertionResults.toArray( 308 new AssertionResult[0]); 309 } 310 311 public void addSubResult(SampleResult subResult) 312 { 313 if (subResults == null) 314 { 315 subResults = new ArrayList (); 316 } 317 subResults.add(subResult); 318 setTime(getTime()+subResult.getTime()); 319 } 320 321 327 public SampleResult[] getSubResults() 328 { 329 if (subResults == null) 330 { 331 return EMPTY_SR; 332 } 333 return (SampleResult[]) subResults.toArray(new SampleResult[0]); 334 } 335 336 public void configure(Configuration info) 337 { 338 time = info.getAttributeAsLong(TOTAL_TIME, 0L); 339 } 340 341 346 public void setResponseData(byte[] response) 347 { 348 responseData = response; 349 } 350 351 356 public byte[] getResponseData() 357 { 358 return responseData; 359 } 360 361 368 public byte [] responseDataAsBA() 369 { 370 return responseData == null ? EMPTY_BA : responseData; 371 } 372 373 public void setSamplerData(String s) 374 { 375 samplerData = s; 376 } 377 378 public String getSamplerData() 379 { 380 return samplerData; 381 } 382 383 388 public long getTime() 389 { 390 return time; 391 } 392 393 public boolean isSuccessful() 394 { 395 return success; 396 } 397 398 public void setDataType(String dataType) 399 { 400 this.dataType = dataType; 401 } 402 403 public String getDataType() 404 { 405 return dataType; 406 } 407 408 413 public void setSuccessful(boolean success) 414 { 415 this.success = success; 416 } 417 418 423 public String toString() 424 { 425 return getSampleLabel(); 426 } 427 428 431 public String getDataEncoding() 432 { 433 if (dataEncoding != null) 434 { 435 return dataEncoding; 436 } 437 else 438 { 439 return DEFAULT_ENCODING; 440 } 441 } 442 443 447 public void setDataEncoding(String dataEncoding) 448 { 449 this.dataEncoding = dataEncoding; 450 } 451 454 public boolean isStopTest() 455 { 456 return stopTest; 457 } 458 459 462 public boolean isStopThread() 463 { 464 return stopThread; 465 } 466 467 470 public void setStopTest(boolean b) 471 { 472 stopTest = b; 473 } 474 475 478 public void setStopThread(boolean b) 479 { 480 stopThread = b; 481 } 482 483 486 public String getRequestHeaders() 487 { 488 return requestHeaders; 489 } 490 491 494 public String getResponseHeaders() 495 { 496 return responseHeaders; 497 } 498 499 502 public void setRequestHeaders(String string) 503 { 504 requestHeaders = string; 505 } 506 507 510 public void setResponseHeaders(String string) 511 { 512 responseHeaders = string; 513 } 514 515 518 public String getContentType() 519 { 520 return contentType; 521 } 522 523 526 public void setContentType(String string) 527 { 528 contentType = string; 529 } 530 531 534 public long getEndTime() 535 { 536 return endTime; 537 } 538 539 542 public long getStartTime() 543 { 544 return startTime; 545 } 546 547 554 protected final void setStartTime(long start) 555 { 556 startTime = start; 557 if (startTimeStamp){ 558 timeStamp = startTime; 559 } 560 } 561 562 private void setEndTime(long end) 563 { 564 endTime = end; 565 if (!startTimeStamp){ 566 timeStamp = endTime; 567 } 568 if (startTime == 0){ 569 log.error("setEndTime must be called after setStartTime" 570 , new Throwable ("Invalid call sequence")); 571 } else { 573 time = endTime - startTime - idleTime; 574 } 575 } 576 577 private void setTimes(long start, long end) 578 { 579 setStartTime(start); 580 setEndTime(end); 581 } 582 583 587 public void sampleStart() 588 { 589 if (startTime == 0){ 590 setStartTime(System.currentTimeMillis()); 591 } else { 592 log.error("sampleStart called twice" 593 , new Throwable ("Invalid call sequence")); 594 } 595 } 596 597 601 public void sampleEnd() 602 { 603 if (endTime == 0){ 604 setEndTime(System.currentTimeMillis()); 605 } else { 606 log.error("sampleEnd called twice" 607 , new Throwable ("Invalid call sequence")); 608 } 609 } 610 611 615 public void samplePause() 616 { 617 if (pauseTime != 0) { 618 log.error("samplePause called twice",new Throwable ("Invalid call sequence")); 619 } 620 pauseTime = System.currentTimeMillis(); 621 } 622 623 627 public void sampleResume() 628 { 629 if (pauseTime == 0) { 630 log.error("sampleResume without samplePause",new Throwable ("Invalid call sequence")); 631 } 632 idleTime += System.currentTimeMillis() - pauseTime; 633 pauseTime=0; 634 } 635 636 640 public void setMonitor(boolean monitor){ 641 isMonitor = monitor; 642 } 643 644 649 public boolean isMonitor(){ 650 return isMonitor; 651 } 652 653 655 656 658 public static class Test extends TestCase 659 { 660 public Test(String name) 661 { 662 super(name); 663 } 664 665 public void testElapsed() throws Exception 666 { 667 SampleResult res = new SampleResult(); 668 669 res.sampleStart(); 671 Thread.sleep(100); 672 res.sampleEnd(); 673 assertTrue(res.getTime() >= 100); 674 } 675 676 public void testPause() throws Exception 677 { 678 SampleResult res = new SampleResult(); 679 res.sampleStart(); 681 Thread.sleep(100); 682 res.samplePause(); 683 684 Thread.sleep(200); 685 686 res.sampleResume(); 688 Thread.sleep(100); 689 res.sampleEnd(); 690 long sampleTime = res.getTime(); 691 if ((sampleTime < 200) || (sampleTime > 290)) { 692 fail("Accumulated time ("+sampleTime+") was not between 200 and 290 ms"); 693 } 694 } 695 696 private static Formatter fmt=new RawFormatter(); 697 private StringWriter wr = null; 698 699 public void divertLog() 700 { 701 wr=new StringWriter (1000); 702 LogTarget [] lt = {new WriterTarget(wr,fmt)}; 703 log.setLogTargets(lt); 704 } 705 706 public void testPause2() throws Exception 707 { 708 divertLog(); 709 SampleResult res = new SampleResult(); 710 res.sampleStart(); 711 res.samplePause(); 712 assertTrue(wr.toString().length()==0); 713 res.samplePause(); 714 assertFalse(wr.toString().length()==0); 715 } 716 } 718 719 private URL location; 720 721 public void setURL(URL location) { 722 this.location= location; 723 } 724 725 public URL getURL() { 726 return location; 727 } 728 } 729 | Popular Tags |