This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.performance.base-0.0.2
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-performance.git

commit f05b35ed0a1aadd90f972b22b98c01066a28340f
Author: Antonio Sanso <[email protected]>
AuthorDate: Mon Oct 15 07:34:45 2012 +0000

    SLING-2593 - Improvement for the Sling performance tools. Adding Report 
level parameter. Thanks Christian Vazzolla for the patch.
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/performance/base@1398205 
13f79535-47bb-0310-9956-ffa450edef68
---
 .../performance/FrameworkPerformanceMethod.java    |   7 +-
 .../sling/performance/PerformanceRunner.java       |  32 +++++-
 .../org/apache/sling/performance/ReportLogger.java | 115 ++++++++++++++++-----
 3 files changed, 124 insertions(+), 30 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java 
b/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java
index 4f95d3e..8010a3a 100644
--- a/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java
+++ b/src/main/java/org/apache/sling/performance/FrameworkPerformanceMethod.java
@@ -37,13 +37,14 @@ class FrameworkPerformanceMethod extends FrameworkMethod {
 
        private Object target;
        private PerformanceSuiteState performanceSuiteState;
+       private PerformanceRunner.ReportLevel reportLevel = 
PerformanceRunner.ReportLevel.ClassLevel;
 
        public FrameworkPerformanceMethod(Method method, Object target,
-                       PerformanceSuiteState performanceSuiteState) {
+                       PerformanceSuiteState performanceSuiteState, 
PerformanceRunner.ReportLevel reportLevel) {
                super(method);
                this.target = target;
                this.performanceSuiteState = performanceSuiteState;
-
+               this.reportLevel = reportLevel;
        }
 
        @Override
@@ -176,7 +177,7 @@ class FrameworkPerformanceMethod extends FrameworkMethod {
                                        
.writeReport(this.target.getClass().getName(),
                                                        
this.performanceSuiteState.testSuiteName,
                                                        getMethod().getName(), 
statistics,
-                                                       
ReportLogger.ReportType.TXT);
+                                                       
ReportLogger.ReportType.TXT, reportLevel);
                }
 
                // In case of a PerformanceSuite we need to run the methods 
annotated
diff --git a/src/main/java/org/apache/sling/performance/PerformanceRunner.java 
b/src/main/java/org/apache/sling/performance/PerformanceRunner.java
index fef112c..68e2926 100644
--- a/src/main/java/org/apache/sling/performance/PerformanceRunner.java
+++ b/src/main/java/org/apache/sling/performance/PerformanceRunner.java
@@ -17,6 +17,10 @@
 package org.apache.sling.performance;
 
 import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -37,12 +41,34 @@ import org.junit.runners.model.InitializationError;
  * The custom JUnit runner that collects the performance tests
  * 
  */
+
+
+
 public class PerformanceRunner extends BlockJUnit4ClassRunner {
        protected LinkedList<FrameworkMethod> tests = new 
LinkedList<FrameworkMethod>();
        private List<PerformanceSuiteState> suitesState = new 
ArrayList<PerformanceSuiteState>();
-
+       public ReportLevel reportLevel = ReportLevel.ClassLevel;
+       
+       public static enum ReportLevel{
+               ClassLevel,
+               MethodLevel
+       }
+       
+       @Retention(RetentionPolicy.RUNTIME)
+       @Target(ElementType.TYPE)
+       public @interface Parameters {
+               public ReportLevel reportLevel() default ReportLevel.ClassLevel;
+       }
+       
        public PerformanceRunner(Class<?> clazz) throws InitializationError {
                super(clazz);
+               
+               // set the report level for the tests that are run with the 
PerformanceRunner
+               // by default set to class level for legacy tests compatibility
+               if (clazz.getAnnotation(Parameters.class) != null){
+                       reportLevel = 
clazz.getAnnotation(Parameters.class).reportLevel();
+               }
+               
                try {
                        computeTests();
                } catch (Exception e) {
@@ -159,7 +185,7 @@ public class PerformanceRunner extends 
BlockJUnit4ClassRunner {
 
                                for (Method method : testMethods) {
                                        FrameworkPerformanceMethod 
performaceTestMethod = new FrameworkPerformanceMethod(
-                                                       method, testObject, 
current);
+                                                       method, testObject, 
current, reportLevel);
                                        tests.add(performaceTestMethod);
                                }
                        }
@@ -174,7 +200,7 @@ public class PerformanceRunner extends 
BlockJUnit4ClassRunner {
                                PerformanceTest.class)) {
                        Object targetObject = 
getTestClass().getJavaClass().newInstance();
                        FrameworkPerformanceMethod performaceTestMethod = new 
FrameworkPerformanceMethod(
-                                       method.getMethod(), targetObject, 
current);
+                                       method.getMethod(), targetObject, 
current, reportLevel);
                        tests.add(performaceTestMethod);
                }
 
diff --git a/src/main/java/org/apache/sling/performance/ReportLogger.java 
b/src/main/java/org/apache/sling/performance/ReportLogger.java
index d43f455..ddbeb62 100644
--- a/src/main/java/org/apache/sling/performance/ReportLogger.java
+++ b/src/main/java/org/apache/sling/performance/ReportLogger.java
@@ -17,11 +17,11 @@ public class ReportLogger {
        }
 
        public static void writeReport(String test, String testSuiteName,
-                       String name, DescriptiveStatistics statistics, 
ReportType reportType)
+                       String name, DescriptiveStatistics statistics, 
ReportType reportType, PerformanceRunner.ReportLevel reportlevel)
                        throws Exception {
                switch (reportType) {
                case TXT:
-                       writeReportTxt(test, testSuiteName, name, statistics);
+                       writeReportTxt(test, testSuiteName, name, statistics, 
reportlevel);
                        break;
                default:
                        throw new Exception(
@@ -40,19 +40,18 @@ public class ReportLogger {
         *            the statistics data to be written
         * @throws IOException
         */
-       public static void writeReportTxt(String test, String testSuiteName,
-                       String name, DescriptiveStatistics statistics) throws 
IOException {
+       public static void writeReportTxt(String test, String testSuiteName, 
String name, DescriptiveStatistics statistics, PerformanceRunner.ReportLevel 
reportlevel)
+                       throws Exception{
 
                String className = test;
                className = className.substring(className.lastIndexOf(".") + 1);
 
                File reportDir = new File("target/performance-reports");
                if (!reportDir.exists()) {
-                       boolean test1 = reportDir.mkdir();
+                       if (!reportDir.mkdir())
+                               throw new IOException("Unable to create 
performance-reports directory");        
                }
 
-               File report = new File("target/performance-reports", className 
+ ".txt");
-
                // need this in the case a user wants to set the suite name 
from the
                // command line
                // useful if we run the test cases from the command line for 
example
@@ -63,25 +62,93 @@ public class ReportLogger {
                        }
                }
 
-               boolean needsPrefix = !report.exists();
-               PrintWriter writer = new PrintWriter(new 
FileWriterWithEncoding(report,
-                               "UTF-8", true));
-               try {
-                       if (needsPrefix) {
-                               writer.format(
-                                               "# %-34.34s     min     10%%    
 50%%     90%%     max%n",
-                                               className);
+               String resultFileName = className;
+               if 
(reportlevel.equals(PerformanceRunner.ReportLevel.ClassLevel)){
+                       writeReportClassLevel(resultFileName, testSuiteName, 
statistics);
+                       }else if 
(reportlevel.equals(PerformanceRunner.ReportLevel.MethodLevel)){
+                               resultFileName = test + "." + name;
+                               writeReportMethodLevel(resultFileName, 
testSuiteName, statistics);
                        }
-
-                       writer.format("%-36.36s  %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();
-               }
        }
+       
+       /**
+     * Write report for class level tests
+     * @param resultFileName the name of the result file (without extension)
+     * @param testSuiteName the name of the test suite name
+     * @param statistics the statistics object used to compute different 
medians
+     * @throws IOException
+     */
+    private static void writeReportClassLevel(String resultFileName, String 
testSuiteName, DescriptiveStatistics statistics)
+               throws IOException{
+       
+       File report = new File("target/performance-reports", resultFileName + 
".txt");
+               boolean needsPrefix = !report.exists();
+           PrintWriter writer = new PrintWriter(
+                       new FileWriterWithEncoding(report, "UTF-8", true));
+           try {
+               if (needsPrefix) {
+                       writer.format(
+                                       "# %-34.34s     min     10%%     50%%   
  90%%     max%n",
+                                       resultFileName);
+               }
+               
+               writer.format(
+                               "%-36.36s  %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();
+                       }
+       }
+    
+    /**
+     * Write report for method level tests
+     * @param resultFileName the name of the result file (without extension)
+     * @param testSuiteName the name of the test suite name
+     * @param statistics the statistics object used to compute different 
medians
+     * @throws IOException
+     */
+    private static void writeReportMethodLevel(String resultFileName, String 
testSuiteName, DescriptiveStatistics statistics)
+           throws IOException{
+       File report = new File("target/performance-reports", resultFileName + 
".txt");
+       
+       String className = resultFileName.substring(0, 
resultFileName.lastIndexOf(".")); 
+       String methodName = 
resultFileName.substring(resultFileName.lastIndexOf(".") + 1); 
+       
+       boolean needsPrefix = !report.exists();
+       PrintWriter writer = new PrintWriter(
+                       new FileWriterWithEncoding(report, "UTF-8", true));
+       try {
+               if (needsPrefix) {
+                       writer.format(
+                                       "%-40.40s|%-80.80s|%-40.40s|      
DateTime      |  min  |   10%%   |   50%%   |   90%%   |   max%n",
+                                       "Test Suite",
+                                       "Test Class",
+                                       "Test Method");
+               }
+               
+               writer.format(
+                               
"%-40.40s|%-80.80s|%-40.40s|%-20.20s|%7.0f|%9.0f|%9.0f|%9.0f|%9.0f%n",
+                               testSuiteName,
+                               className,
+                               methodName,
+                               getDate(),
+                               statistics.getMin(),
+                               statistics.getPercentile(10.0),
+                               statistics.getPercentile(50.0),
+                               statistics.getPercentile(90.0),
+                               statistics.getMax());
+               } finally {
+                       writer.close();
+               }
+    }
+           
+       
+       
 
        /**
         * Get the date that will be written into the result file

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to