sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asa...@apache.org
Subject svn commit: r1608717 - in /sling/trunk/performance/base/src/main/java/org/apache/sling/performance: AbstractRepositoryTest.java FrameworkPerformanceMethod.java IdentifiableTestClass.java PerformanceRunner.java ReportLogger.java
Date Tue, 08 Jul 2014 11:07:19 GMT
Author: asanso
Date: Tue Jul  8 11:07:18 2014
New Revision: 1608717

URL: http://svn.apache.org/r1608717
Log:
SLING-3579 - Allow performance tests to report custom class name to the report logger

* applied patch from Andrei Dulvac

Added:
    sling/trunk/performance/base/src/main/java/org/apache/sling/performance/IdentifiableTestClass.java
Modified:
    sling/trunk/performance/base/src/main/java/org/apache/sling/performance/AbstractRepositoryTest.java
    sling/trunk/performance/base/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java
    sling/trunk/performance/base/src/main/java/org/apache/sling/performance/PerformanceRunner.java
    sling/trunk/performance/base/src/main/java/org/apache/sling/performance/ReportLogger.java

Modified: sling/trunk/performance/base/src/main/java/org/apache/sling/performance/AbstractRepositoryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/performance/base/src/main/java/org/apache/sling/performance/AbstractRepositoryTest.java?rev=1608717&r1=1608716&r2=1608717&view=diff
==============================================================================
--- sling/trunk/performance/base/src/main/java/org/apache/sling/performance/AbstractRepositoryTest.java
(original)
+++ sling/trunk/performance/base/src/main/java/org/apache/sling/performance/AbstractRepositoryTest.java
Tue Jul  8 11:07:18 2014
@@ -26,7 +26,7 @@ import org.apache.sling.jcr.api.SlingRep
 
 
   
-public abstract class AbstractRepositoryTest {
+public abstract class AbstractRepositoryTest implements IdentifiableTestClass{
 
     protected String testInstanceName;
 
@@ -87,9 +87,8 @@ public abstract class AbstractRepository
         }
         return testRoot;
     }
-    
-    public String toString(){
-    	return testInstanceName;
-    }
 
+    public String testClassName() {
+        return testInstanceName;
+    }
 }

Modified: sling/trunk/performance/base/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java
URL: http://svn.apache.org/viewvc/sling/trunk/performance/base/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java?rev=1608717&r1=1608716&r2=1608717&view=diff
==============================================================================
--- sling/trunk/performance/base/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java
(original)
+++ sling/trunk/performance/base/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java
Tue Jul  8 11:07:18 2014
@@ -39,6 +39,7 @@ class FrameworkPerformanceMethod extends
 	private PerformanceSuiteState performanceSuiteState;
 	private PerformanceRunner.ReportLevel reportLevel = PerformanceRunner.ReportLevel.ClassLevel;
     private String testCaseName = "";
+    private String className;
 
 	public FrameworkPerformanceMethod(Method method, Object target,
 			PerformanceSuiteState performanceSuiteState, PerformanceRunner.ReportLevel reportLevel)
{
@@ -48,7 +49,16 @@ class FrameworkPerformanceMethod extends
 		this.reportLevel = reportLevel;
         if (target instanceof IdentifiableTestCase) {
             this.testCaseName = ((IdentifiableTestCase) target).testCaseName();
-	}
+	    }
+
+        // Name of the test class, as the report logger needs it
+        // This can be overwritten by tests by implementing IdentifiableTestClass
+        String longClassName = this.target.getClass().getName();
+        className = longClassName.substring(longClassName.lastIndexOf(".") + 1);
+        if (target instanceof IdentifiableTestClass) {
+            this.className = ((IdentifiableTestClass) target).testClassName();
+        }
+
     }
 
 	@Override
@@ -76,11 +86,9 @@ class FrameworkPerformanceMethod extends
 		// in case we don't have to deal with a PerformanceSuite just skip this
 		// as JUnit will run the methods itself
 		if ((performanceSuiteState != null)
-				&& !performanceSuiteState.testSuiteName
-						.equals(ParameterizedTestList.TEST_CASE_ONLY)) {
+				&& !performanceSuiteState.testSuiteName.equals(ParameterizedTestList.TEST_CASE_ONLY))
{
 
-			recursiveCallSpecificMethod(this.target.getClass(), this.target,
-					Before.class);
+			recursiveCallSpecificMethod(this.target.getClass(), this.target, Before.class);
 		}
 
 		// Need to count the number of tests run from the PerformanceSuite
@@ -104,16 +112,12 @@ class FrameworkPerformanceMethod extends
 
 		DescriptiveStatistics statistics = new DescriptiveStatistics();
 
-		// System.out.println("Warmup started - test :" +
-		// testMethodToInvoke.getName());
-
 		if (warmupinvocations != 0) {
 			// Run the number of invocation specified in the annotation
 			// for warming up the system
 			for (int invocationIndex = 0; invocationIndex < warmupinvocations; invocationIndex++)
{
 
-				recursiveCallSpecificMethod(this.target.getClass(),
-						this.target, BeforeMethodInvocation.class);
+				recursiveCallSpecificMethod(this.target.getClass(), this.target, BeforeMethodInvocation.class);
 
 				// TODO: implement the method to run a before a specific test
 				// method
@@ -177,8 +181,8 @@ class FrameworkPerformanceMethod extends
 		}
 
 		if (statistics.getN() > 0) {
-		    ReportLogger.writeReport(this.performanceSuiteState.testSuiteName, testCaseName, ((String
)this.target.getClass().getMethod("toString", null).invoke(this.target, null)),
-                    getMethod().getName(), statistics, ReportLogger.ReportType.TXT, reportLevel);
+		    ReportLogger.writeReport(this.performanceSuiteState.testSuiteName, testCaseName, className,
getMethod().getName(),
+                    statistics, ReportLogger.ReportType.TXT, reportLevel);
 		}
 
 		// In case of a PerformanceSuite we need to run the methods annotated
@@ -187,11 +191,9 @@ class FrameworkPerformanceMethod extends
 		// with a PerformanceSuite
 		// just skip this as JUnit will run the methods itself
 		if ((performanceSuiteState != null)
-				&& !performanceSuiteState.testSuiteName
-						.equals(ParameterizedTestList.TEST_CASE_ONLY)) {
+				&& !performanceSuiteState.testSuiteName.equals(ParameterizedTestList.TEST_CASE_ONLY))
{
 
-			recursiveCallSpecificMethod(this.target.getClass(), this.target,
-					After.class);
+			recursiveCallSpecificMethod(this.target.getClass(), this.target, After.class);
 		}
 
 		// Check if this is the last test running from a PerformanceSuite
@@ -199,13 +201,9 @@ class FrameworkPerformanceMethod extends
 		if ((performanceSuiteState != null)
 				&& (performanceSuiteState.getAfterSuiteMethod() != null)
 				&& (performanceSuiteState.getTargetObjectSuite() != null)
-				&& (performanceSuiteState.getNumberOfExecutedMethods() == performanceSuiteState
-						.getNumberOfMethodsInSuite())
-				&& !performanceSuiteState.testSuiteName
-						.equals(ParameterizedTestList.TEST_CASE_ONLY)) {
-			performanceSuiteState.getAfterSuiteMethod().invoke(
-					performanceSuiteState.getTargetObjectSuite());
-
+				&& (performanceSuiteState.getNumberOfExecutedMethods() == performanceSuiteState.getNumberOfMethodsInSuite())
+				&& !performanceSuiteState.testSuiteName.equals(ParameterizedTestList.TEST_CASE_ONLY))
{
+			performanceSuiteState.getAfterSuiteMethod().invoke(performanceSuiteState.getTargetObjectSuite());
 		}
 
 		return response;
@@ -240,8 +238,7 @@ class FrameworkPerformanceMethod extends
 		// System.out.println("Start test: " + testMethodToInvoke.getName());
 		long start = System.nanoTime();
 		response = super.invokeExplosively(this.target, params);
-		long timeMilliseconds = TimeUnit.MILLISECONDS.convert(System.nanoTime()
-				- start, TimeUnit.NANOSECONDS);
+		long timeMilliseconds = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
 		statistics.addValue(timeMilliseconds);
 
 		// System.out.println("End test: " + testMethodToInvoke.getName());
@@ -252,8 +249,7 @@ class FrameworkPerformanceMethod extends
 		// recursiveCallSpecificMethod(this.target.getClass(), this.target,
 		// AfterSpecificTest.class);
 
-		recursiveCallSpecificMethod(this.target.getClass(), this.target,
-				AfterMethodInvocation.class);
+		recursiveCallSpecificMethod(this.target.getClass(), this.target, AfterMethodInvocation.class);
 
 		return response;
 	}
@@ -309,14 +305,12 @@ class FrameworkPerformanceMethod extends
 			throws InvalidAttributesException, IllegalAccessException,
 			InstantiationException {
 
-		Method[] methodsToReturn = getSpecificMethods(testClass,
-				methodAnnotation);
+		Method[] methodsToReturn = getSpecificMethods(testClass, methodAnnotation);
 		Method methodToReturn = null;
 		if (methodsToReturn.length == 1) {
 			methodToReturn = methodsToReturn[0];
 		} else if (methodsToReturn.length > 1) {
-			throw new InvalidAttributesException(
-					"Only 1 non parameterized before method accepted");
+			throw new InvalidAttributesException("Only 1 non parameterized before method accepted");
 		}
 
 		return methodToReturn;

Added: sling/trunk/performance/base/src/main/java/org/apache/sling/performance/IdentifiableTestClass.java
URL: http://svn.apache.org/viewvc/sling/trunk/performance/base/src/main/java/org/apache/sling/performance/IdentifiableTestClass.java?rev=1608717&view=auto
==============================================================================
--- sling/trunk/performance/base/src/main/java/org/apache/sling/performance/IdentifiableTestClass.java
(added)
+++ sling/trunk/performance/base/src/main/java/org/apache/sling/performance/IdentifiableTestClass.java
Tue Jul  8 11:07:18 2014
@@ -0,0 +1,10 @@
+package org.apache.sling.performance;
+
+/**
+ * Interface to be implemented by a class with PerformanceTests.
+ * <p></p>The provided method @{link #testCaseName()} exposes the possibility
to give a name to each instance of the
+ * implementing class</p>
+ */
+public interface IdentifiableTestClass {
+    public String testClassName();
+}

Modified: sling/trunk/performance/base/src/main/java/org/apache/sling/performance/PerformanceRunner.java
URL: http://svn.apache.org/viewvc/sling/trunk/performance/base/src/main/java/org/apache/sling/performance/PerformanceRunner.java?rev=1608717&r1=1608716&r2=1608717&view=diff
==============================================================================
--- sling/trunk/performance/base/src/main/java/org/apache/sling/performance/PerformanceRunner.java
(original)
+++ sling/trunk/performance/base/src/main/java/org/apache/sling/performance/PerformanceRunner.java
Tue Jul  8 11:07:18 2014
@@ -196,17 +196,14 @@ public class PerformanceRunner extends B
 
 		if (!suiteAlreadyRegistered) {
 			if (beforeSuiteMethods.size() == 1) {
-				newSuite.setBeforeSuiteMethod(beforeSuiteMethods.get(0)
-						.getMethod());
+				newSuite.setBeforeSuiteMethod(beforeSuiteMethods.get(0).getMethod());
 			}
 			if (afterSuiteMethods.size() == 1) {
-				newSuite.setAfterSuiteMethod(afterSuiteMethods.get(0)
-						.getMethod());
+				newSuite.setAfterSuiteMethod(afterSuiteMethods.get(0).getMethod());
 			}
 
 			current = newSuite;
-			newSuite.setTargetObjectSuite(getTestClass().getJavaClass()
-					.newInstance());
+			newSuite.setTargetObjectSuite(getTestClass().getJavaClass().newInstance());
 
 		}
 
@@ -218,14 +215,11 @@ public class PerformanceRunner extends B
 			for (Object testObject : testObjects) {
 
 				// retrieve the test methods from the test classes
-				Method[] testMethods = getSpecificMethods(
-						testObject.getClass(), PerformanceTest.class);
-
+				Method[] testMethods = getSpecificMethods(testObject.getClass(), PerformanceTest.class);
 
 				for (Method method : testMethods) {
-					FrameworkPerformanceMethod performaceTestMethod = new
-							FrameworkPerformanceMethod(
-							method, testObject, current, reportLevel);
+					FrameworkPerformanceMethod performaceTestMethod =
+                            new FrameworkPerformanceMethod(method, testObject, current, reportLevel);
 					tests.add(performaceTestMethod);
 				}
 
@@ -240,8 +234,7 @@ public class PerformanceRunner extends B
 
 		// Retrieve the performance tests in the case we don't have a
 		// performance test suite
-		for (FrameworkMethod method : getTestClass().getAnnotatedMethods(
-				PerformanceTest.class)) {
+		for (FrameworkMethod method : getTestClass().getAnnotatedMethods(PerformanceTest.class))
{
 			Object targetObject = getTestClass().getJavaClass().newInstance();
 			FrameworkPerformanceMethod performanceTestMethod = new FrameworkPerformanceMethod(
 					method.getMethod(), targetObject, current, reportLevel);

Modified: sling/trunk/performance/base/src/main/java/org/apache/sling/performance/ReportLogger.java
URL: http://svn.apache.org/viewvc/sling/trunk/performance/base/src/main/java/org/apache/sling/performance/ReportLogger.java?rev=1608717&r1=1608716&r2=1608717&view=diff
==============================================================================
--- sling/trunk/performance/base/src/main/java/org/apache/sling/performance/ReportLogger.java
(original)
+++ sling/trunk/performance/base/src/main/java/org/apache/sling/performance/ReportLogger.java
Tue Jul  8 11:07:18 2014
@@ -37,10 +37,10 @@ public class ReportLogger {
     public static void writeReport(String testSuiteName, String testCaseName, String className,
String methodName,
             DescriptiveStatistics statistics, ReportType reportType, PerformanceRunner.ReportLevel
reportLevel) throws Exception {
 		switch (reportType) {
-		case TXT:
+            case TXT:
                 writeReportTxt(testSuiteName, testCaseName, className, methodName, statistics,
reportLevel);
-			break;
-		default:
+                break;
+            default:
                 throw new Exception("The specified reporting format is not yet supported");
 		}
 	}
@@ -53,21 +53,16 @@ public class ReportLogger {
      * @param className
      * @param methodName
 	 * @param statistics
-     * @param reportlevel
+     * @param reportLevel
      * @throws Exception
 	 */
     public static void writeReportTxt(String testSuiteName, String testCaseName, String className,
String methodName,
-            DescriptiveStatistics statistics, PerformanceRunner.ReportLevel reportlevel)
throws Exception {
-
-        // Short class form
-        String shortClassName = className.substring(className.lastIndexOf(".") + 1);
+            DescriptiveStatistics statistics, PerformanceRunner.ReportLevel reportLevel)
throws Exception {
 
         File reportDir = new File("target/" + REPORTS_DIR);
-		if (!reportDir.exists()) {
-            if (!reportDir.mkdir()) {
+		if (!reportDir.exists() && !reportDir.mkdir()) {
                 throw new IOException("Unable to create " + REPORTS_DIR + " directory");
 		}
-        }
 
 		// need this in the case a user wants to set the suite name from the
 		// command line
@@ -79,13 +74,13 @@ public class ReportLogger {
 			}
 		}
 
-        String resultFileName = shortClassName;
-		if (reportlevel.equals(PerformanceRunner.ReportLevel.ClassLevel)){
-			writeReportClassLevel(resultFileName, testSuiteName, statistics);
-			}else if (reportlevel.equals(PerformanceRunner.ReportLevel.MethodLevel)){
-            resultFileName = shortClassName + "." + methodName;
+		if (reportLevel.equals(PerformanceRunner.ReportLevel.ClassLevel)) {
+            String resultFileName = className;
+            writeReportClassLevel(resultFileName, testSuiteName, statistics);
+        } else if (reportLevel.equals(PerformanceRunner.ReportLevel.MethodLevel)) {
+            String resultFileName = className + "." + methodName;
             writeReportMethodLevel(resultFileName, testSuiteName, testCaseName, className,
methodName, statistics);
-			}
+        }
 	}
 	
 	/**
@@ -104,22 +99,20 @@ public class ReportLogger {
 	    		new FileWriterWithEncoding(report, "UTF-8", true));
 	    try {
 	    	if (needsPrefix) {
-	    		writer.format(
-	    				"# %-34.34s     min     10%%     50%%     90%%     max%n",
-	    				resultFileName);
+	    		writer.format("# %-50.50s     min     10%%     50%%     90%%     max%n", resultFileName);
 	    	}
 	    	
 	    	writer.format(
-	    			"%-36.36s  %6.0f  %6.0f  %6.0f  %6.0f  %6.0f%n",
+	    			"%-52.52s  %6.0f  %6.0f  %6.0f  %6.0f  %6.0f%n",
 	    			testSuiteName,
 	    			statistics.getMin(),
 	    			statistics.getPercentile(10.0),
 	    			statistics.getPercentile(50.0),
 	    			statistics.getPercentile(90.0),
 	    			statistics.getMax());
-	    	} finally {
-	    		writer.close();
-	   		}
+        } finally {
+            writer.close();
+        }
 	}
     
     /**



Mime
View raw message