Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.reporters » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.jmeter.reporters;
   20   
   21   import java.io.File;
   22   import java.io.FileNotFoundException;
   23   import java.io.FileOutputStream;
   24   import java.io.IOException;
   25   import java.io.Serializable;
   26   
   27   import org.apache.commons.lang.text.StrBuilder;
   28   import org.apache.jmeter.samplers.SampleEvent;
   29   import org.apache.jmeter.samplers.SampleListener;
   30   import org.apache.jmeter.samplers.SampleResult;
   31   import org.apache.jmeter.testelement.AbstractTestElement;
   32   import org.apache.jmeter.threads.JMeterContextService;
   33   import org.apache.jorphan.logging.LoggingManager;
   34   import org.apache.jorphan.util.JOrphanUtils;
   35   import org.apache.log.Logger;
   36   
   37   /**
   38    * Save Result responseData to a set of files
   39    *
   40    *
   41    * This is mainly intended for validation tests
   42    *
   43    */
   44   // TODO - perhaps save other items such as headers?
   45   public class ResultSaver extends AbstractTestElement implements Serializable, SampleListener {
   46       private static final Logger log = LoggingManager.getLoggerForClass();
   47   
   48       // File name sequence number
   49       //@GuardedBy("this")
   50       private static long sequenceNumber = 0;
   51   
   52       public static final String FILENAME = "FileSaver.filename"; // $NON-NLS-1$
   53   
   54       public static final String VARIABLE_NAME = "FileSaver.variablename"; // $NON-NLS-1$
   55   
   56       public static final String ERRORS_ONLY = "FileSaver.errorsonly"; // $NON-NLS-1$
   57   
   58       public static final String SUCCESS_ONLY = "FileSaver.successonly"; // $NON-NLS-1$
   59   
   60       public static final String SKIP_AUTO_NUMBER = "FileSaver.skipautonumber"; // $NON-NLS-1$
   61   
   62       public static final String SKIP_SUFFIX = "FileSaver.skipsuffix"; // $NON-NLS-1$
   63   
   64       private synchronized long nextNumber() {
   65           return ++sequenceNumber;
   66       }
   67   
   68       /*
   69        * Constructor is initially called once for each occurrence in the test plan
   70        * For GUI, several more instances are created Then clear is called at start
   71        * of test Called several times during test startup The name will not
   72        * necessarily have been set at this point.
   73        */
   74       public ResultSaver() {
   75           super();
   76           // log.debug(Thread.currentThread().getName());
   77           // System.out.println(">> "+me+" "+this.getName()+"
   78           // "+Thread.currentThread().getName());
   79       }
   80   
   81       /*
   82        * Constructor for use during startup (intended for non-GUI use) @param name
   83        * of summariser
   84        */
   85       public ResultSaver(String name) {
   86           this();
   87           setName(name);
   88       }
   89   
   90       /*
   91        * This is called once for each occurrence in the test plan, before the
   92        * start of the test. The super.clear() method clears the name (and all
   93        * other properties), so it is called last.
   94        */
   95       public void clear() {
   96           super.clear();
   97           synchronized(this){
   98               sequenceNumber = 0; // TODO is this the right thing to do?
   99           }
  100       }
  101   
  102       /**
  103        * Saves the sample result (and any sub results) in files
  104        *
  105        * @see org.apache.jmeter.samplers.SampleListener#sampleOccurred(org.apache.jmeter.samplers.SampleEvent)
  106        */
  107       public void sampleOccurred(SampleEvent e) {
  108         processSample(e.getResult(), new Counter());
  109      }
  110   
  111      /**
  112       * Recurse the whole (sub)result hierarchy.
  113       *
  114       * @param s Sample result
  115       * @param c sample counter
  116       */
  117      private void processSample(SampleResult s, Counter c) {
  118           saveSample(s, c.num++);
  119           SampleResult[] sr = s.getSubResults();
  120           for (int i = 0; i < sr.length; i++) {
  121               processSample(sr[i], c);
  122           }
  123       }
  124   
  125       /**
  126        * @param s SampleResult to save
  127        * @param num number to append to variable (if >0)
  128        */
  129       private void saveSample(SampleResult s, int num) {
  130           // Should we save the sample?
  131           if (s.isSuccessful()){
  132               if (getErrorsOnly()){
  133                   return;
  134               }
  135           } else {
  136               if (getSuccessOnly()){
  137                   return;
  138               }
  139           }
  140   
  141           String fileName = makeFileName(s.getContentType(), getSkipAutoNumber(), getSkipSuffix());
  142           log.debug("Saving " + s.getSampleLabel() + " in " + fileName);
  143           s.setResultFileName(fileName);// Associate sample with file name
  144           String variable = getVariableName();
  145           if (variable.length()>0){
  146               if (num > 0) {
  147                   StrBuilder sb = new StrBuilder(variable);
  148                   sb.append(num);
  149                   variable=sb.toString();
  150               }
  151               JMeterContextService.getContext().getVariables().put(variable, fileName);
  152           }
  153           File out = new File(fileName);
  154           FileOutputStream pw = null;
  155           try {
  156               pw = new FileOutputStream(out);
  157               pw.write(s.getResponseData());
  158           } catch (FileNotFoundException e1) {
  159               log.error("Error creating sample file for " + s.getSampleLabel(), e1);
  160           } catch (IOException e1) {
  161               log.error("Error saving sample " + s.getSampleLabel(), e1);
  162           } finally {
  163               JOrphanUtils.closeQuietly(pw);
  164           }
  165       }
  166   
  167       /**
  168        * @return fileName composed of fixed prefix, a number, and a suffix derived
  169        *         from the contentType e.g. Content-Type:
  170        *         text/html;charset=ISO-8859-1
  171        */
  172       private String makeFileName(String contentType, boolean skipAutoNumber, boolean skipSuffix) {
  173           StrBuilder sb = new StrBuilder(getFilename());
  174           if (!skipAutoNumber){
  175               sb.append(nextNumber());
  176           }
  177           if (!skipSuffix){
  178               sb.append('.');
  179               if (contentType != null) {
  180                   int i = contentType.indexOf("/"); // $NON-NLS-1$
  181                   if (i != -1) {
  182                       int j = contentType.indexOf(";"); // $NON-NLS-1$
  183                       if (j != -1) {
  184                           sb.append(contentType.substring(i + 1, j));
  185                       } else {
  186                           sb.append(contentType.substring(i + 1));
  187                       }
  188                   } else {
  189                       sb.append("unknown");                    
  190                   }
  191               } else {
  192                   sb.append("unknown");
  193               }
  194           }
  195           return sb.toString();
  196       }
  197   
  198       /*
  199        * (non-Javadoc)
  200        *
  201        * @see org.apache.jmeter.samplers.SampleListener#sampleStarted(org.apache.jmeter.samplers.SampleEvent)
  202        */
  203       public void sampleStarted(SampleEvent e) {
  204           // not used
  205       }
  206   
  207       /*
  208        * (non-Javadoc)
  209        *
  210        * @see org.apache.jmeter.samplers.SampleListener#sampleStopped(org.apache.jmeter.samplers.SampleEvent)
  211        */
  212       public void sampleStopped(SampleEvent e) {
  213           // not used
  214       }
  215   
  216       private String getFilename() {
  217           return getPropertyAsString(FILENAME);
  218       }
  219   
  220       private String getVariableName() {
  221           return getPropertyAsString(VARIABLE_NAME,""); // $NON-NLS-1$
  222       }
  223   
  224       private boolean getErrorsOnly() {
  225           return getPropertyAsBoolean(ERRORS_ONLY);
  226       }
  227   
  228       private boolean getSkipAutoNumber() {
  229           return getPropertyAsBoolean(SKIP_AUTO_NUMBER);
  230       }
  231   
  232       private boolean getSkipSuffix() {
  233           return getPropertyAsBoolean(SKIP_SUFFIX);
  234       }
  235   
  236       private boolean getSuccessOnly() {
  237           return getPropertyAsBoolean(SUCCESS_ONLY);
  238       }
  239   
  240       // Mutable int to keep track of sample count
  241       private static class Counter{
  242           int num;
  243       }
  244   }

Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.reporters » [javadoc | source]