Author: [email protected]
Date: Mon Apr 16 14:32:23 2012
New Revision: 2224

Log:
[AMDATUCASSANDRA-185] Added support for testing RC releases and write sample 
files to individual files

Modified:
   trunk/test-performance/pom.xml
   trunk/test-performance/src/main/java/org/amdatu/test/performance/Utils.java
   
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ReportSummary.java
   
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/Statistics.java
   
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ZSamples.java
   
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/AmdatuLauncher.java
   
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/JMeterRunner.java
   
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/TestContext.java

Modified: trunk/test-performance/pom.xml
==============================================================================
--- trunk/test-performance/pom.xml      (original)
+++ trunk/test-performance/pom.xml      Mon Apr 16 14:32:23 2012
@@ -9,7 +9,7 @@
   </parent>
   <groupId>org.amdatu.test</groupId>
   <artifactId>org.amdatu.test.performance</artifactId>
-  <version>0.2.0</version>
+  <version>0.2.1</version>
   <packaging>jar</packaging>
   <name>Amdatu - Automated performance test</name>
 
@@ -53,7 +53,7 @@
     <dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
-      <version>2.0.1</version>
+      <version>2.2</version>
       <scope>compile</scope>
     </dependency>
     <dependency>

Modified: 
trunk/test-performance/src/main/java/org/amdatu/test/performance/Utils.java
==============================================================================
--- trunk/test-performance/src/main/java/org/amdatu/test/performance/Utils.java 
(original)
+++ trunk/test-performance/src/main/java/org/amdatu/test/performance/Utils.java 
Mon Apr 16 14:32:23 2012
@@ -15,23 +15,29 @@
  */
 package org.amdatu.test.performance;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-
-import org.amdatu.test.performance.runtest.Logger;
-import org.apache.commons.io.IOUtils;
+import org.amdatu.test.performance.runtest.JMeterRunner;
+import org.amdatu.test.performance.runtest.Logger;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 
 public class Utils {
     // Private constant for buffer size.
     private static final int BUFFER_SIZE = 40 * 1024; // Blocks of 40 Kb each
-    
+
     /**
      * Unzips the file to the specified target directory.
      * @param zipFile The zipfile to unzip
@@ -101,26 +107,100 @@
                 out.close();
             }
         }
-    }
-    
+    }
+
+    public static void mergeSampleFiles(String resultsDir) throws IOException {
+        File resultDir = new File(resultsDir);
+        for (File file : resultDir.listFiles()) {
+            if (file.getName().contains(JMeterRunner.SAMPLES_X + ".")) {
+                String path = file.getParent();
+                String fileName = file.getName().substring(0, 
file.getName().indexOf(JMeterRunner.SAMPLES_X));
+                String report = path + File.separator + fileName + 
JMeterRunner.SAMPLES;
+                merge(report + "X");
+                merge(report + "Y");
+            }
+        }
+    }
+
+    private static void merge(String bigFileName) throws IOException {
+        File bigFile = new File(bigFileName);
+        List<File> deleteFiles = new ArrayList<File>();
+        if (!bigFile.exists()) {
+            bigFile.createNewFile();
+
+            StringBuffer content = new StringBuffer();
+            boolean notFound = false;
+            int index = 1;
+            while (!notFound) {
+                File sourceFile = new File(bigFileName + "." + index);
+                if (sourceFile.exists()) {
+                    String part = FileUtils.readFileToString(sourceFile);
+                    int from = part.indexOf("<httpSample ");
+                    int to = part.indexOf("</testResults>");
+                    if (content.length() == 0) {
+                        content.append(part.substring(0, to));
+                    }
+                    else {
+                        content.append(part.substring(from, to));
+                    }
+                    deleteFiles.add(sourceFile);
+                }
+                else {
+                    notFound = true;
+                }
+                index++;
+            }
+            content.append("</testResults>");
+            FileUtils.writeStringToFile(bigFile, content.toString(), false);
+        }
+        
+        for (File file : deleteFiles) {
+            file.delete();
+        }
+    }
+
+
+
     public static void dispatchOutput(final Process process) {
         new Thread(new Runnable() {
-            public void run() {
-                try {
-                    IOUtils.copy(process.getErrorStream(), Logger.logFile);    
 
+            public void run() {
+                InputStream is = null;
+                try {
+                    is = process.getErrorStream();
+                    IOUtils.copy(is, Logger.logFile);     
                 } catch (IOException e) {
                     System.err.println(e.toString());
                     e.printStackTrace();
-                } 
+                }  finally {
+                    if (is != null) {
+                        try {
+                            is.close();
+                        }
+                        catch (IOException e) {
+                            System.err.println(e.toString());
+                            e.printStackTrace();
+                        }
+                    }
+                }
             } 
         }).start();
         new Thread(new Runnable() {
-            public void run() {
-                try {
-                    IOUtils.copy(process.getInputStream(), Logger.logFile);
+            public void run() {
+                InputStream is = null;
+                try {
+                    is = process.getInputStream();
+                    IOUtils.copy(is, Logger.logFile);
                 } catch (IOException e) {
                     System.err.println(e.toString());
                     e.printStackTrace();
+                } if (is != null) {
+                    try {
+                        is.close();
+                    }
+                    catch (IOException e) {
+                        System.err.println(e.toString());
+                        e.printStackTrace();
+                    }
                 }
             } 
         }).start();

Modified: 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ReportSummary.java
==============================================================================
--- 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ReportSummary.java
        (original)
+++ 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ReportSummary.java
        Mon Apr 16 14:32:23 2012
@@ -340,7 +340,6 @@
         } finally {
             try {
                 if (pw != null) {
-
                     pw.close();
                 }
             } finally {

Modified: 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/Statistics.java
==============================================================================
--- 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/Statistics.java
   (original)
+++ 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/Statistics.java
   Mon Apr 16 14:32:23 2012
@@ -13,83 +13,93 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.test.performance.analysis;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.amdatu.test.performance.runtest.ApplicationContext;
-import org.amdatu.test.performance.runtest.JMeterRunner;
-import org.amdatu.test.performance.runtest.TestContext;
-import org.apache.commons.math.MathException;
-import org.xml.sax.SAXException;
-
-/**
- * This class analyzes the results of JMeter plans executed against version X 
and Y. It assumes that Z=X-Y is 
- * normally distributed, and under this assumption we test the null hypotheses 
mean(Z) = 0. Under this assumption,
- * T=mean(Z)sqrt(n)/S has a T distribution with (n-1) degrees of freedom.
- * TODO: Note that the assumption that Z is normally distributed is very 
likely, but it remains an assumption. To 
- * verify the distribution, we should perform a goodness of fit check; room 
for improvement.
- * @author ivol
- */
-public class Statistics {
-    private ApplicationContext m_context;
-    
-    private List<ReportSummary> m_reports = new ArrayList<ReportSummary>();
-
-    public Statistics(ApplicationContext context) {
-        m_context = context;
-    }
-
-    public void analyze() throws ParserConfigurationException, SAXException, 
IOException, MathException {
-        // Analyze
-        for (String sample : getJMeterReports()) {
-            File reportX = new File(sample + "X");
-            File reportY = new File(sample + "Y");
-            String reportName = reportX.getName().substring(0, 
reportX.getName().length()-JMeterRunner.SAMPLES_X.length());
-            SAXParser sax = SAXParserFactory.newInstance().newSAXParser();
-            JMeterResultsParser resultsX = new JMeterResultsParser(reportName);
-            JMeterResultsParser resultsY = new JMeterResultsParser(reportName);
-            sax.parse(reportX, resultsX);
-            sax.parse(reportY, resultsY);
-            
-            TestContext x = new TestContext(resultsX.getVersion());
-            TestContext y = new TestContext(resultsY.getVersion());
-            m_context.setContext(x, y);
-            
-            // Merge and add the JMeter results to the overall report
-            ReportSummary report = new ReportSummary(m_context, reportName);
-            report.mergeAndAdd(resultsX, resultsY);
-            m_reports.add(report);
-        }
-    }
-
-    public void print() throws IOException {
-        printHtml();
-    }
-
-    public void printHtml() throws IOException {
-        for (ReportSummary report : m_reports) {
-            report.toHtmlFile();
-        }
-    }
-
-    private List<String> getJMeterReports() {
-        List<String> samples = new ArrayList<String>();
-        File resultDir = new File(m_context.resultsDir);
-        for (File file : resultDir.listFiles()) {
-            if (file.getName().endsWith(JMeterRunner.SAMPLES_X)) {
-                String path = file.getParent();
-                String fileName = file.getName().substring(0, 
file.getName().indexOf(JMeterRunner.SAMPLES_X));
-                samples.add(path + File.separator + fileName + 
JMeterRunner.SAMPLES);
-            }
-        }
-        return samples;
-    }
-}
+package org.amdatu.test.performance.analysis;
+
+import org.amdatu.test.performance.Utils;
+import org.amdatu.test.performance.runtest.ApplicationContext;
+import org.amdatu.test.performance.runtest.JMeterRunner;
+import org.amdatu.test.performance.runtest.TestContext;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.commons.math.MathException;
+import org.xml.sax.SAXException;
+
+/**
+ * This class analyzes the results of JMeter plans executed against version X 
and Y. It assumes that Z=X-Y is
+ * normally distributed, and under this assumption we test the null hypotheses 
mean(Z) = 0. Under this assumption,
+ * T=mean(Z)sqrt(n)/S has a T distribution with (n-1) degrees of freedom.
+ * TODO: Note that the assumption that Z is normally distributed is very 
likely, but it remains an assumption. To
+ * verify the distribution, we should perform a goodness of fit check; room 
for improvement.
+ * 
+ * @author ivol
+ */
+public class Statistics {
+    private ApplicationContext m_context;
+
+    private List<ReportSummary> m_reports = new ArrayList<ReportSummary>();
+
+    public Statistics(ApplicationContext context) {
+        m_context = context;
+    }
+
+    public void analyze() throws ParserConfigurationException, SAXException, 
IOException, MathException {
+        // First merge any sample files that have not yet been merged into one 
big sample file
+        Utils.mergeSampleFiles(m_context.resultsDir);
+        
+        // List the reports
+        List<String> reports = getJMeterReports();
+
+        // Analyze
+        for (String sample : reports) {
+            File reportX = new File(sample + "X");
+            File reportY = new File(sample + "Y");
+            String reportName =
+                reportX.getName().substring(0, reportX.getName().length() - 
JMeterRunner.SAMPLES_X.length());
+            SAXParser sax = SAXParserFactory.newInstance().newSAXParser();
+            JMeterResultsParser resultsX = new JMeterResultsParser(reportName);
+            JMeterResultsParser resultsY = new JMeterResultsParser(reportName);
+            sax.parse(reportX, resultsX);
+            sax.parse(reportY, resultsY);
+
+            TestContext x = new TestContext(resultsX.getVersion());
+            TestContext y = new TestContext(resultsY.getVersion());
+            m_context.setContext(x, y);
+
+            // Merge and add the JMeter results to the overall report
+            ReportSummary report = new ReportSummary(m_context, reportName);
+            report.mergeAndAdd(resultsX, resultsY);
+            m_reports.add(report);
+        }
+    }
+
+    public void print() throws IOException {
+        printHtml();
+    }
+
+    public void printHtml() throws IOException {
+        for (ReportSummary report : m_reports) {
+            report.toHtmlFile();
+        }
+    }
+
+    private List<String> getJMeterReports() {
+        List<String> samples = new ArrayList<String>();
+        File resultDir = new File(m_context.resultsDir);
+        for (File file : resultDir.listFiles()) {
+            if (file.getName().endsWith(JMeterRunner.SAMPLES_X)) {
+                String path = file.getParent();
+                String fileName = file.getName().substring(0, 
file.getName().indexOf(JMeterRunner.SAMPLES_X));
+                samples.add(path + File.separator + fileName + 
JMeterRunner.SAMPLES);
+            }
+        }
+        return samples;
+    }
+}

Modified: 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ZSamples.java
==============================================================================
--- 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ZSamples.java
     (original)
+++ 
trunk/test-performance/src/main/java/org/amdatu/test/performance/analysis/ZSamples.java
     Mon Apr 16 14:32:23 2012
@@ -212,8 +212,8 @@
             yValues[i] = y.get(i).responseTime;
             zValues[i] = xValues[i] - yValues[i];
 
-            if (count > 0 && count == newSampleSize) {
-                m_samplesX.add(meanX / newSampleSize);
+            if (count > 0 && count == newSampleSize) {
+                m_samplesX.add(meanX / newSampleSize);
                 m_samplesY.add(meanY / newSampleSize);
                 count = 0;
                 meanX = 0;

Modified: 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/AmdatuLauncher.java
==============================================================================
--- 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/AmdatuLauncher.java
        (original)
+++ 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/AmdatuLauncher.java
        Mon Apr 16 14:32:23 2012
@@ -120,7 +120,9 @@
             os.close();
 
             m_amdatuProcess.waitFor();
-        }
+        }
+        
+        // FIXME: Stop output dispatcher Utils.dispatchOutput(m_amdatuProcess);
 
         Logger.log("Amdatu (" + version + ") shutdown completed");
     }

Modified: 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/JMeterRunner.java
==============================================================================
--- 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/JMeterRunner.java
  (original)
+++ 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/JMeterRunner.java
  Mon Apr 16 14:32:23 2012
@@ -13,240 +13,289 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.test.performance.runtest;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.RandomAccessFile;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.amdatu.test.performance.Utils;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.io.LineIterator;
-
-/**
- * This class is responsible to run all JMeter plans present in the configured 
directory.
- * 
- * @author ivol
- */
-public class JMeterRunner {
-    public final static String SAMPLES = "_samples.";
-    public final static String SAMPLES_X = SAMPLES + "X";
-    public final static String SAMPLES_Y = SAMPLES + "Y";
-
-    private Process m_jmeterProcess;
-    private File m_sampleFile;
-    private ApplicationContext m_context;
-
-    public JMeterRunner(ApplicationContext context) {
-        m_context = context;
-    }
-
-    public void run(boolean rampUp) throws FileNotFoundException, IOException, 
InterruptedException {
-        File resultDir = new File(m_context.resultsDir);
-        File jmeterLogFile = new File(resultDir, "jmeter.log");
-
-        File jmeterDir = installJMeter();
-        jmeterDir = new File(jmeterDir, "bin");
-        File plans = new File(m_context.jmeterPlanDir);
-
-        for (File jmeterPlan : plans.listFiles()) {
-            if (jmeterPlan.isFile() && jmeterPlan.getName().endsWith(".jmx")) {
-                // Update the port number for the http samples in the JMeter 
plans
-                preparePlan(jmeterPlan);
-
-                // Run the JMeter plan!
-                if (rampUp) {
-                    m_sampleFile = new File(m_context.rootTmpDir, 
"rampupsamples");
-                } else {
-                    String sampleName = jmeterPlan.getName().replace(".jmx", 
"") + m_context.getSamplesPostFix()+ "." + m_context.currentContextName;
-                    m_sampleFile = new File(resultDir, sampleName);
-                }
-                List<String> command = new ArrayList<String>();
-                String os = System.getProperty("os.name");
-                if (os.indexOf("Windows") != -1) {
-                    command.add("cmd.exe");
-                    command.add("/C");
-                    command.add("jmeter");
-                } else {
-                    command.add("jmeter.sh");
-                }
-
-                command.add("-n"); // Non-GUI mode
-                command.add("-t");
-                command.add(jmeterPlan.getAbsolutePath());
-                command.add("-l");
-                if (m_context.omit <= 0) {
-                    command.add(m_sampleFile.getAbsolutePath());
-                } else {
-                    command.add(m_sampleFile.getAbsolutePath()+ ".tmp");
-                }
-                command.add("-j");
-                command.add(jmeterLogFile.getAbsolutePath());
-
-                String cmd = "";
-                for (String c : command) {
-                    cmd += c + " ";
-                }
-                cmd = cmd.substring(0, cmd.length()-1);
+package org.amdatu.test.performance.runtest;
+
+import org.amdatu.test.performance.Utils;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.LineIterator;
+
+/**
+ * This class is responsible to run all JMeter plans present in the configured 
directory.
+ * A JMeter runner runs the JMeter plans for 1 test version X vs Y
+ * 
+ * @author ivol
+ */
+public class JMeterRunner {
+    public final static String SAMPLES = "_samples.";
+    public final static String SAMPLES_X = SAMPLES + "X";
+    public final static String SAMPLES_Y = SAMPLES + "Y";
+
+    private Process m_jmeterProcess;
+    private File m_sampleFile;
+    private ApplicationContext m_context;
+    private Map<String, Integer> m_fileIndex = new HashMap<String, Integer>();
+
+    public JMeterRunner(ApplicationContext context) {
+        m_context = context;
+    }
+
+    public void run(boolean rampUp) throws FileNotFoundException, IOException, 
InterruptedException {
+        File resultDir = new File(m_context.resultsDir);
+
+        File jmeterLogFile = new File(resultDir, "jmeter.log");
+
+        File jmeterDir = installJMeter();
+        jmeterDir = new File(jmeterDir, "bin");
+        File plans = new File(m_context.jmeterPlanDir);
+
+        for (File jmeterPlan : plans.listFiles()) {
+            if (jmeterPlan.isFile() && jmeterPlan.getName().endsWith(".jmx")) {
+                // Update the port number for the http samples in the JMeter 
plans
+                preparePlan(jmeterPlan);
+                
+                // Run the JMeter plan!
+                if (rampUp) {
+                    m_sampleFile = new File(m_context.rootTmpDir, 
"rampupsamples");
+                }
+                else {
+                    // Write each JMeter run result to its own file, otherwise 
we get 1 very big sample file
+                    // slowing down JMeter
+                    String sampleName =
+                        jmeterPlan.getName().replace(".jmx", "") + 
m_context.getSamplesPostFix() + "."
+                            + m_context.currentContextName + "." + 
incFileIndex(jmeterPlan);
+                    m_sampleFile = new File(resultDir, sampleName);
+                }
+                List<String> command = new ArrayList<String>();
+                String os = System.getProperty("os.name");
+                if (os.indexOf("Windows") != -1) {
+                    command.add("cmd.exe");
+                    command.add("/C");
+                    command.add("jmeter");
+                }
+                else {
+                    command.add("jmeter.sh");
+                }
+
+                command.add("-n"); // Non-GUI mode
+                command.add("-t");
+                command.add(jmeterPlan.getAbsolutePath());
+                command.add("-l");
+                if (m_context.omit <= 0) {
+                    command.add(m_sampleFile.getAbsolutePath());
+                }
+                else {
+                    command.add(m_sampleFile.getAbsolutePath() + ".tmp");
+                }
+                command.add("-j");
+                command.add(jmeterLogFile.getAbsolutePath());
+
+                String cmd = "";
+                for (String c : command) {
+                    cmd += c + " ";
+                }
+                cmd = cmd.substring(0, cmd.length() - 1);
                 Logger.log("------------------------------------------------");
-                String version = m_context.currentContext.version + " " + 
m_context.currentContextName;
-                Logger.log("Executing JMeter plan '" + jmeterPlan.getName() + 
"' on " + version + (rampUp ? " (ramp-up mode)" : ""));
-                Logger.log("");
-                Logger.log("Executing '" + cmd + "'");
-                ProcessBuilder processBuilder = new ProcessBuilder(command);
-                processBuilder.directory(jmeterDir);
-                Logger.log("Running JMeter plan '" + jmeterPlan.getName() + "' 
from directory '" + jmeterDir.getAbsolutePath() + "'");
-                long before = System.currentTimeMillis();
-                m_jmeterProcess = processBuilder.start();
-                Utils.dispatchOutput(m_jmeterProcess);
-
-                m_jmeterProcess.waitFor();
-                long diff = System.currentTimeMillis() - before;
-                Logger.log("JMeter plan execution finished in " + diff + " 
milliseconds");
-                if (m_context.omit > 0) {
-                    if (!rampUp) {
-                        merge(m_sampleFile.getAbsolutePath()+ ".tmp", 
m_context.omit);
-                    }
-                    new File(m_sampleFile.getAbsolutePath()+ ".tmp").delete();
-                }
-            }
-        }
-    }
-
-    private File installJMeter() throws FileNotFoundException, IOException {
-        // Install only once
-        File jmeter = new File(new File(m_context.rootTmpDir, "jmeter-2.4"), 
"jakarta-jmeter-2.4");
-        if (!jmeter.exists()) {
-            // Copy the jmeter.zip from this jar to the temp dir
-            URL url = 
getClass().getClassLoader().getResources("jakarta-jmeter-2.4.zip").nextElement();
-            InputStream is = null;
-            try {
-                is = url.openStream();
-                File jmeterFile = new File(m_context.rootTmpDir, 
"jmeter-2.4.zip");
-                IOUtils.copy(is, new FileOutputStream(jmeterFile));
-
-                // And unzip
-                File target = new File(m_context.rootTmpDir, "jmeter-2.4");
-                Utils.unzip(jmeterFile, target);
-                File jmeterDir = new File(target, "jakarta-jmeter-2.4");
-                File binDir = new File(jmeterDir, "bin");
-                for (File bin : binDir.listFiles()) {
-                    // Make bin files executable
-                    bin.setExecutable(true);
-                }
-                return jmeterDir;
-            } finally {
-                if (is != null) {
-                    is.close();
-                }
-            }
-        } else {
-            return jmeter;
-        }
-    }
-
-    private void merge(String file, int omit) throws IOException {
-        // Merge the results of this temporary file in the large results file, 
but omitting omit results
-        File targetFile = new File(file.replace(".tmp", ""));
-        boolean isNewFile = !targetFile.exists();
-        LineIterator iter = FileUtils.lineIterator(new File(file));
-        int count = 0;
-        BufferedWriter bw = new BufferedWriter(new FileWriter(targetFile, 
true));
-        String eol = System.getProperty("line.separator");
-        if (!isNewFile) {
-            removeEndTestResults();
-        }
-        try {
-            while (iter.hasNext()) {
-                String line = iter.next();
-                boolean write  = false;
-                if (line.startsWith("<httpSample") || 
line.startsWith("</testResults>")) {
-                    count++;
-                    if (count >= omit) {
-                        write = true;
-                    }
-                } else if (isNewFile) {
-                    if (line.indexOf("<testResults ") != -1) {
-                        line = line.replace(">", " amdatuversion=\"" + 
m_context.currentContext.version + "\">");
-                    }
-                    write = true; 
-                }
-                if (write) {
-                    bw.append(line + eol);
-                }
-            }
-        } finally {
-            iter.close();
-            bw.flush();
-            bw.close();
-        }
-    }
-
-    public void removeEndTestResults() throws IOException {
-        long length = m_sampleFile.length();
-        long offset = m_sampleFile.length()-1;
-        boolean endFound = false;
-        RandomAccessFile fis = new RandomAccessFile(m_sampleFile, "rw");
-        try {
-            while(!endFound && offset >= 0) {
-                offset--;
-                byte[] bytes = new byte[(int) (length-offset)];
-                fis.seek(offset);
-                fis.read(bytes, 0, (int) (length-offset));
-                endFound = new String(bytes, 
"UTF-8").indexOf("</testResults>") != -1;
-                if (endFound) {
-                    fis.setLength(offset);           
-                }
-            }
-        } finally {
-            fis.close();
-        }
-    }
-
-    private final static String SAMPLER_PORT_PROP = 
"name=\"HTTPSampler.port\"";
-    private final static String SAMPLER_PATH_PROP = 
"name=\"HTTPSampler.path\"";
-
-    private void preparePlan(File plan) throws IOException {
-        boolean after010 = m_context.currentContext.isAfterVersion("0.1.0") >= 
0;
-
-        List<String> lines = FileUtils.readLines(plan);
-        List<String> newLines = new ArrayList<String>();
-        for (String line : lines) {
-            if (line.indexOf(SAMPLER_PORT_PROP) != -1) {
-                newLines.add("<stringProp " + SAMPLER_PORT_PROP + ">" + 
m_context.port + "</stringProp>");
-            } else if (line.indexOf(SAMPLER_PATH_PROP) != -1) {
-                int start = line.indexOf(SAMPLER_PATH_PROP) + 
SAMPLER_PATH_PROP.length() + 1;
-                int end = line.indexOf("</stringProp>");
-                String path = line.substring(start, end);
-                if (!path.isEmpty()) {
-                    if (!after010) {
-                        // Before 0.1.0, rest urls were 
/rest/services/[alias]/[alias]
-                        if (path.startsWith("/rest/") && 
!path.startsWith("/rest/services")) {
-                            String alias = 
path.substring(path.indexOf("/rest/") + "/rest/".length());
-                            path = "/rest/services/" + alias + "/" + alias;
-                        }
-                    } else {
-                        // In 0.1.0 and later, rest urls are /rest/[alias]
-                        if (path.startsWith("/rest/services")) {
-                            String alias = 
path.substring(path.indexOf("/rest/services/") + "/rest/services/".length());
-                            path = "/rest/" + alias;
-                        }
-                    }
-                }
-                newLines.add("<stringProp " + SAMPLER_PATH_PROP + ">" + path + 
"</stringProp>");
-            } else {
-                newLines.add(line);
-            }
-        }
-        FileUtils.writeLines(plan, newLines);
-    }
-}
+                String version = m_context.currentContext.version + " " + 
m_context.currentContextName;
+                Logger.log("Executing JMeter plan '" + jmeterPlan.getName() + 
"' on " + version
+                    + (rampUp ? " (ramp-up mode)" : ""));
+                Logger.log("");
+                Logger.log("Executing '" + cmd + "'");
+                ProcessBuilder processBuilder = new ProcessBuilder(command);
+                processBuilder.directory(jmeterDir);
+                Logger.log("Running JMeter plan '" + jmeterPlan.getName() + "' 
from directory '"
+                    + jmeterDir.getAbsolutePath() + "'");
+                long before = System.currentTimeMillis();
+                m_jmeterProcess = processBuilder.start();
+                Utils.dispatchOutput(m_jmeterProcess);
+
+                m_jmeterProcess.waitFor();
+                long diff = System.currentTimeMillis() - before;
+                Logger.log("JMeter plan execution finished in " + diff + " 
milliseconds");
+                if (m_context.omit > 0) {
+                    if (!rampUp) {
+                        merge(m_sampleFile.getAbsolutePath() + ".tmp", 
m_context.omit);
+                    }
+                    new File(m_sampleFile.getAbsolutePath() + ".tmp").delete();
+                }
+            }
+        }
+    }
+    
+    private int incFileIndex(File jmeterPlan) {
+        int index = 1;
+        String key = jmeterPlan.getName() +  "." + 
m_context.currentContextName;
+        if (!m_fileIndex.containsKey(key)) {
+            m_fileIndex.put(key, 1);
+        } else {
+            index = m_fileIndex.get(key) + 1;
+            m_fileIndex.put(key, index);
+        }
+        return index;
+    }
+
+    private File installJMeter() throws FileNotFoundException, IOException {
+        // Install only once
+        File jmeter = new File(new File(m_context.rootTmpDir, "jmeter-2.4"), 
"jakarta-jmeter-2.4");
+        if (!jmeter.exists()) {
+            // Copy the jmeter.zip from this jar to the temp dir
+            URL url = 
getClass().getClassLoader().getResources("jakarta-jmeter-2.4.zip").nextElement();
+            InputStream is = null;
+            try {
+                is = url.openStream();
+                File jmeterFile = new File(m_context.rootTmpDir, 
"jmeter-2.4.zip");
+                IOUtils.copy(is, new FileOutputStream(jmeterFile));
+
+                // And unzip
+                File target = new File(m_context.rootTmpDir, "jmeter-2.4");
+                Utils.unzip(jmeterFile, target);
+                File jmeterDir = new File(target, "jakarta-jmeter-2.4");
+                File binDir = new File(jmeterDir, "bin");
+                for (File bin : binDir.listFiles()) {
+                    // Make bin files executable
+                    bin.setExecutable(true);
+                }
+                return jmeterDir;
+            }
+            finally {
+                if (is != null) {
+                    is.close();
+                }
+            }
+        }
+        else {
+            return jmeter;
+        }
+    }
+
+    private void merge(String file, int omit) throws IOException {
+        // Merge the results of this temporary file in the large results file, 
but omitting omit results
+        File targetFile = new File(file.replace(".tmp", ""));
+        boolean isNewFile = !targetFile.exists();
+        LineIterator iter = FileUtils.lineIterator(new File(file));
+        int count = 0;
+        BufferedWriter bw = null;
+        try {
+            bw = new BufferedWriter(new FileWriter(targetFile, true));
+
+            String eol = System.getProperty("line.separator");
+            if (!isNewFile) {
+                removeEndTestResults();
+            }
+
+            while (iter.hasNext()) {
+                String line = iter.next();
+                boolean write = false;
+                if (line.startsWith("<httpSample") || 
line.startsWith("</testResults>")) {
+                    count++;
+                    if (count >= omit) {
+                        write = true;
+                    }
+                }
+                else if (isNewFile) {
+                    if (line.indexOf("<testResults ") != -1) {
+                        line = line.replace(">", " amdatuversion=\"" + 
m_context.currentContext.version + "\">");
+                    }
+                    write = true;
+                }
+                if (write) {
+                    bw.append(line + eol);
+                }
+            }
+        }
+        finally {
+            try {
+                iter.close();
+            }
+            finally {
+                if (bw != null) {
+                    try {
+                        bw.flush();
+                    }
+                    finally {
+                        bw.close();
+                    }
+                }
+            }
+
+        }
+    }
+
+    public void removeEndTestResults() throws IOException {
+        long length = m_sampleFile.length();
+        long offset = m_sampleFile.length() - 1;
+        boolean endFound = false;
+        RandomAccessFile fis = new RandomAccessFile(m_sampleFile, "rw");
+        try {
+            while (!endFound && offset >= 0) {
+                offset--;
+                byte[] bytes = new byte[(int) (length - offset)];
+                fis.seek(offset);
+                fis.read(bytes, 0, (int) (length - offset));
+                endFound = new String(bytes, 
"UTF-8").indexOf("</testResults>") != -1;
+                if (endFound) {
+                    fis.setLength(offset);
+                }
+            }
+        }
+        finally {
+            fis.close();
+        }
+    }
+
+    private final static String SAMPLER_PORT_PROP = 
"name=\"HTTPSampler.port\"";
+    private final static String SAMPLER_PATH_PROP = 
"name=\"HTTPSampler.path\"";
+
+    private void preparePlan(File plan) throws IOException {
+        boolean after010 = m_context.currentContext.isAfterVersion("0.1.0") >= 
0;
+
+        List<String> lines = FileUtils.readLines(plan);
+        List<String> newLines = new ArrayList<String>();
+        for (String line : lines) {
+            if (line.indexOf(SAMPLER_PORT_PROP) != -1) {
+                newLines.add("<stringProp " + SAMPLER_PORT_PROP + ">" + 
m_context.port + "</stringProp>");
+            }
+            else if (line.indexOf(SAMPLER_PATH_PROP) != -1) {
+                int start = line.indexOf(SAMPLER_PATH_PROP) + 
SAMPLER_PATH_PROP.length() + 1;
+                int end = line.indexOf("</stringProp>");
+                String path = line.substring(start, end);
+                if (!path.isEmpty()) {
+                    if (!after010) {
+                        // Before 0.1.0, rest urls were 
/rest/services/[alias]/[alias]
+                        if (path.startsWith("/rest/") && 
!path.startsWith("/rest/services")) {
+                            String alias = 
path.substring(path.indexOf("/rest/") + "/rest/".length());
+                            path = "/rest/services/" + alias + "/" + alias;
+                        }
+                    }
+                    else {
+                        // In 0.1.0 and later, rest urls are /rest/[alias]
+                        if (path.startsWith("/rest/services")) {
+                            String alias = 
path.substring(path.indexOf("/rest/services/") + "/rest/services/".length());
+                            path = "/rest/" + alias;
+                        }
+                    }
+                }
+                newLines.add("<stringProp " + SAMPLER_PATH_PROP + ">" + path + 
"</stringProp>");
+            }
+            else {
+                newLines.add(line);
+            }
+        }
+        FileUtils.writeLines(plan, newLines);
+    }
+}

Modified: 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/TestContext.java
==============================================================================
--- 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/TestContext.java
   (original)
+++ 
trunk/test-performance/src/main/java/org/amdatu/test/performance/runtest/TestContext.java
   Mon Apr 16 14:32:23 2012
@@ -13,82 +13,110 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.test.performance.runtest;
-
-import java.io.File;
-
-public class TestContext {
-    public String version;
-    public String binFile;
-    
-    public TestContext(File zipFile) {
-        binFile = zipFile.getAbsolutePath();
-        version = getVersionFromZipName(zipFile.getName());
-    }
-    
-    public TestContext(String name) {
-        version = name;
-    }
-    
-    private static String getVersionFromZipName(String zipName) {
+package org.amdatu.test.performance.runtest;
+
+import java.io.File;
+
+public class TestContext {
+    public String version;
+    public String binFile;
+
+    public TestContext(File zipFile) {
+        binFile = zipFile.getAbsolutePath();
+        version = getVersionFromZipName(zipFile.getName());
+    }
+
+    public TestContext(String name) {
+        version = name;
+    }
+
+    private static String getVersionFromZipName(String zipName) {
         // Syntax ...amdatu-release-[version](-bin).zip ('-bin' is optional)
         String version = "";
         if (zipName.endsWith(".zip")) {
-           version =  zipName.substring(0, zipName.indexOf(".zip"));
+            version = zipName.substring(0, zipName.indexOf(".zip"));
         }
-        if (version.endsWith("-bin")) {
-             version = zipName.substring(0, zipName.indexOf("-bin"));
-            
+        if (version.endsWith("-bin")) {
+            version = version.substring(0, version.indexOf("-bin"));
         }
         if (version.endsWith("-SNAPSHOT")) {
-             version = zipName.substring(0, zipName.indexOf("-SNAPSHOT"));
-        } 
-        version = version.substring(version.lastIndexOf("-") + 1);
-        return version;
-    }
-    
-    // Compares given version to this version. 
-    // Returns:
-    //   0 if they are equal
-    //  -1 if the specified version is before the version of this context
-    //  +1 if the specified version is after the version of this context
-    public int isAfterVersion(String version2) {
-        if (version.equals(version2)) {
-            // Same versions
-            return 0;
-        }
-        String[] v1 = version.split("\\.");
-        String[] v2 = version2.split("\\.");
-        if (v1[2].endsWith("-SNAPSHOT")) {
-            v1[2] = v1[2].substring(0, v1[2].indexOf("-SNAPSHOT"));
-        }
-        if (v2[2].endsWith("-SNAPSHOT")) {
-            v2[2] = v2[2].substring(0, v2[2].indexOf("-SNAPSHOT"));
-        }
-        int major1 = Integer.parseInt(v1[0]);
-        int major2 = Integer.parseInt(v2[0]);
-        if (major1 < major2) {
-            return -1;
-        } else if (major1 > major2) {
-            return +1;
-        } else {
-            int minor1 = Integer.parseInt(v1[1]);
-            int minor2 = Integer.parseInt(v2[1]);
-            if (minor1 < minor2) {
-                return -1;
-            } else if (minor1 > minor2) {
-                return +1;
-            } else {
-                int micro1 = Integer.parseInt(v1[2]);
-                int micro2 = Integer.parseInt(v2[2]);
-                if (micro1 < micro2) {
-                    return -1;
-                } else if (micro1 > micro2) {
-                    return +1;
-                } else {
-                    return 0;
-                }
-            }
-        }
-    }
-}
+            version = version.substring(0, version.indexOf("-SNAPSHOT"));
+        }
+        if (version.indexOf("-RC") != -1) {
+            String norc = version.substring(0, version.indexOf("-RC"));
+            String main = norc.substring(norc.lastIndexOf("-") + 1);
+            String rc = version.substring(version.lastIndexOf("-") + 1);
+            version = main + "-" + rc;
+        }
+        else {
+            version = version.substring(version.lastIndexOf("-") + 1);
+        }
+        return version;
+    }
+
+    // Compares given version to this version.
+    // Returns:
+    // 0 if they are equal
+    // -1 if the specified version is before the version of this context
+    // +1 if the specified version is after the version of this context
+    public int isAfterVersion(String version2) {
+        if (version.equals(version2)) {
+            // Same versions
+            return 0;
+        }
+        String[] v1 = version.split("\\.");
+        String[] v2 = version2.split("\\.");
+        int rc1 = 0;
+        int rc2 = 0;
+        if (v1[2].endsWith("-SNAPSHOT")) {
+            v1[2] = v1[2].substring(0, v1[2].indexOf("-SNAPSHOT"));
+        }
+        else if (v1[2].indexOf("-RC") != -1) {
+            rc1 = Integer.parseInt(v1[2].substring((v1[2].indexOf("-RC") + 
3)));
+            v1[2] = v1[2].substring(0, v1[2].indexOf("-RC"));
+        }
+        if (v2[2].endsWith("-SNAPSHOT")) {
+            v2[2] = v2[2].substring(0, v2[2].indexOf("-SNAPSHOT"));
+        }
+        else if (v2[2].indexOf("-RC") != -1) {
+            rc2 = Integer.parseInt(v2[2].substring((v2[2].indexOf("-RC") + 
3)));
+            v2[2] = v2[2].substring(0, v2[2].indexOf("-RC"));
+        }
+        int major1 = Integer.parseInt(v1[0]);
+        int major2 = Integer.parseInt(v2[0]);
+        if (major1 < major2) {
+            return -1;
+        }
+        else if (major1 > major2) {
+            return +1;
+        }
+        else {
+            int minor1 = Integer.parseInt(v1[1]);
+            int minor2 = Integer.parseInt(v2[1]);
+            if (minor1 < minor2) {
+                return -1;
+            }
+            else if (minor1 > minor2) {
+                return +1;
+            }
+            else {
+                int micro1 = Integer.parseInt(v1[2]);
+                int micro2 = Integer.parseInt(v2[2]);
+                if (micro1 < micro2) {
+                    return -1;
+                }
+                else if (micro1 > micro2) {
+                    return +1;
+                }
+                else {
+                    if (rc1 < rc2) {
+                        return -1;
+                    } else if (rc1 > rc2) {
+                        return +1;
+                    }
+                    return 0;
+                }
+            }
+        }
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to