vhardy      2003/07/03 07:51:09

  Modified:    test-resources/org/apache/batik/test unitTesting.xml
  Added:       test-sources/org/apache/batik/test PerformanceTest.java
                        PerformanceTestValidator.java
  Log:
  Initial commit of PerformanceTest with validation test
  
  Revision  Changes    Path
  1.1                  
xml-batik/test-sources/org/apache/batik/test/PerformanceTest.java
  
  Index: PerformanceTest.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.test;
  
  import java.awt.AlphaComposite;
  import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.geom.AffineTransform;
  import java.awt.image.BufferedImage;
  
  /**
   * This abstract <code>Test</code> implementation instruments performance
   * testing.
   *
   * Derived classes need only implement the <code>runOp</code> and, 
   * optionally, the <code>runRef</code> methods.
   *
   * The <code>setReferenceScore</code> method is used to specify 
   * the last recorded score for the performance test and the 
   * <code>setAllowedScoreDeviation</code> method is used to specify
   * the allowed deviation from the reference score.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Hardy</a>
   * @version $Id: PerformanceTest.java,v 1.1 2003/07/03 14:51:08 vhardy Exp $
   */
  public abstract class PerformanceTest extends AbstractTest {
      /**
       * Reference score. -1 means there is no reference score
       */
      protected double referenceScore = -1;
  
      /**
       * Allowed deviation from the reference score. 10% by default
       */
      protected double allowedScoreDeviation = 0.1;
  
      /**
       * Score during last run
       */
      protected double lastScore = -1;
  
      public double getLastScore() {
          return lastScore;
      }
  
      public double getReferenceScore() {
          return referenceScore;
      }
  
      public void setReferenceScore(double referenceScore) {
          this.referenceScore = referenceScore;
      }
  
      public double getAllowedScoreDeviation() {
          return allowedScoreDeviation;
      }
  
      public void setAllowedScoreDeviation(double allowedScoreDeviation) {
          this.allowedScoreDeviation = allowedScoreDeviation;
      }
  
      /**
       * Force implementations to only implement <code>runOp</code>
       * and other performance specific methods.
       */
      public final TestReport run() {
          return super.run();
      }
  
      /**
       * Force implementations to only implement <code>runOp</code>
       * and other performance specific methods.
       */
      public final boolean runImplBasic() throws Exception {
          // Should never be called for a PerformanceTest
          return false;
      }
  
      /**
       * This implementation of runImpl runs the reference 
       * operation (with <code>runRef</code>), then runs
       * the operation (with <code>runOp</code>) and checks whether
       * or not the score is within the allowed deviation of the 
       * reference score.
       *
       * @see #runRef
       * @see #runOp
       */
      public final TestReport runImpl() throws Exception {
          // Run the reference and time it the second time it
          // is run
          runRef();
  
          long refStart = System.currentTimeMillis();
          runRef();
          long refEnd = System.currentTimeMillis();
          
          double refUnit = refEnd - refStart;
  
          System.err.println(">>>>>>>>>>>>>> refUnit : " + refUnit);
  
          // Now, run the test's operation and time it the second
          // time it is run
          runOp();
  
          long opStart = System.currentTimeMillis();
          runOp();
          long opEnd = System.currentTimeMillis();
  
          double opLength = opEnd - opStart;
  
          // Compute the score
          double score = opLength / refUnit;
          this.lastScore = score;
  
          // Compare to the reference score
          if (referenceScore == -1) {
              TestReport report = reportError("no.reference.score.set");
              report.addDescriptionEntry("computed.score", "" + score);
              return report;
          } else {
              double scoreMin = referenceScore*(1-allowedScoreDeviation);
              double scoreMax = referenceScore*(1+allowedScoreDeviation);
              if (score > scoreMax) {
                  TestReport report = reportError("performance.regression");
                  report.addDescriptionEntry("reference.score", "" + referenceScore);
                  report.addDescriptionEntry("computed.score", "" + score);
                  report.addDescriptionEntry("score.deviation", "" + 
100*((score-referenceScore)/referenceScore));
                  return report;
              } else if (score < scoreMin) {
                  TestReport report = 
reportError("unexpected.performance.improvement");
                  report.addDescriptionEntry("reference.score", "" + referenceScore);
                  report.addDescriptionEntry("computed.score", "" + score);
                  report.addDescriptionEntry("score.deviation", "" + 
100*((score-referenceScore)/referenceScore));
                  return report;
              } else {
                  return reportSuccess();
              }
          }
      }
  
      /**
       * Runs the reference operation.
       * By default, this runs the same BufferedImage drawing 
       * operation 10000 times
       */
      protected void runRef() {
          BufferedImage buf = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
          Graphics2D g = buf.createGraphics();
          AffineTransform txf = new AffineTransform();
          g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
          g.setPaint(new Color(30, 100, 200));
  
          for (int i=0; i<50; i++) {
              for (int j=0; j<20; j++) {
                  txf.setToIdentity();
                  txf.translate(-100, -100);
                  txf.rotate(j*Math.PI/100);
                  txf.translate(100, 100);
                  g.setTransform(txf);
                  g.drawRect(30, 30, 140, 140);
              }
          }
      }
  
      /**
       * Runs the tested operation
       */
      protected abstract void runOp() throws Exception;
  }
  
  
  
  1.1                  
xml-batik/test-sources/org/apache/batik/test/PerformanceTestValidator.java
  
  Index: PerformanceTestValidator.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.test;
  
  import java.io.StringWriter;
  import java.io.PrintWriter;
  
  import java.util.Vector;
  
  /**
   * Validates the operation of the <code>PerformanceTest</code> class.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Hardy</a>
   * @version $Id: PerformanceTestValidator.java,v 1.1 2003/07/03 14:51:08 vhardy Exp $
   */
  public class PerformanceTestValidator extends AbstractTest {
      public TestReport runImpl() throws Exception {
          // First, work with SimplePerformanceTest to check the life
          // cycle of using a performance test.
          // ===========================================================
          SimplePerformanceTest p = new SimplePerformanceTest();
          TestReport r = p.run();
          assertTrue(!r.hasPassed());
          assertTrue(r.getErrorCode().equals("no.reference.score.set"));
          double score = p.getLastScore();        
          p.setReferenceScore(score);
          r = p.run();
  
          if (!r.hasPassed()) {
              TestReport result = reportError("unexpected.performance.test.failure");
              result.addDescriptionEntry("expected.score", "" + score);
              result.addDescriptionEntry("actual.score", "" + p.getLastScore());
              return result;
          }
  
          // Now, check that performance changes are detected
          // ===========================================================
          p.setReferenceScore(score*0.5);
          r = p.run();
          assertTrue(!r.hasPassed());
          if (!r.getErrorCode().equals("performance.regression")) {
              TestReport result = 
reportError("unexpected.performance.test.error.code");
              result.addDescriptionEntry("expected.code", "performance.regression");
              result.addDescriptionEntry("actual.code", r.getErrorCode());
              result.addDescriptionEntry("expected.score", "" + score);
              result.addDescriptionEntry("actual.score", "" + p.getLastScore());
              return result;
          }
  
          p.setReferenceScore(score*2);
          r = p.run();
          assertTrue(!r.hasPassed());
          if (!r.getErrorCode().equals("unexpected.performance.improvement")) {
              TestReport result = 
reportError("unexpected.performance.test.error.code");
              result.addDescriptionEntry("expected.code", 
"unexpected.performance.improvement");
              result.addDescriptionEntry("actual.code", r.getErrorCode());
              result.addDescriptionEntry("expected.score", "" + score);
              result.addDescriptionEntry("actual.score", "" + p.getLastScore());
              return result;
          }
  
          return reportSuccess();
      }
  
      static class SimplePerformanceTest extends PerformanceTest {
          public void runOp() {
              Vector v = new Vector();
              for (int i=0; i<10000; i++) {
                  v.addElement("" + i);
              }
  
              for (int i=0; i<10000; i++) {
                  if (v.contains("" + i)) {
                      v.remove("" + i);
                  }
              }
          }
      }
  }
  
  
  
  1.8       +8 -1      xml-batik/test-resources/org/apache/batik/test/unitTesting.xml
  
  Index: unitTesting.xml
  ===================================================================
  RCS file: /home/cvs/xml-batik/test-resources/org/apache/batik/test/unitTesting.xml,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- unitTesting.xml   3 Jul 2003 14:10:04 -0000       1.7
  +++ unitTesting.xml   3 Jul 2003 14:51:08 -0000       1.8
  @@ -45,4 +45,11 @@
       <test id="memoryLeakTestValidator" 
       class="org.apache.batik.test.MemoryLeakTestValidator" />
   
  +    <!-- ========================================================================== 
-->
  +    <!-- Validates the operation of PerformanceTest                                 
-->
  +    <!-- ========================================================================== 
-->
  +    <test id="PerformanceTestValidator" 
class="org.apache.batik.test.PerformanceTestValidator" /> 
  +
  +    
  +
   </testSuite>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to