Package org.apache.jmeter

Source Code of org.apache.jmeter.TextDriver$OutputListener

/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation.  All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache JMeter" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache JMeter", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation.  For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jmeter;

import java.io.IOException;
import java.net.MalformedURLException;

import java.net.URL;
import org.apache.jmeter.control.SamplerController;
import org.apache.jmeter.engine.JMeterEngine;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.config.UrlConfig;
import org.apache.jmeter.protocol.http.control.HttpTestSample;
import org.apache.jmeter.reporters.*;
import org.apache.jmeter.samplers.SampleEvent;

import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.timers.*;
import org.apache.jmeter.util.*;

/************************************************************
*  !ToDo (Class description)
*
*@author     $Author: mstover1 $
*@created    $Date: 2001/07/26 00:34:43 $
*@version    $Revision: 1.11 $
***********************************************************/
public class TextDriver
{
  JMeterEngine engine;
  OutputListener ol;
  private final static String USAGE =
      "java org.apache.jmeter.TextDriver [-o output-file] url";

  /************************************************************
   *  !ToDo (Constructor description)
   *
   *@param  outfile                    !ToDo (Parameter description)
   *@param  url                        !ToDo (Parameter description)
   *@param  propsFile                  !ToDo (Parameter description)
   *@exception  MalformedURLException  !ToDo (Exception description)
   ***********************************************************/
  public TextDriver(String outfile, String url, String propsFile) throws MalformedURLException
  {
    JMeterUtils.getProperties(propsFile);
    SamplerController sc = createTestSample(url);
    ol = new OutputListener(outfile);
    engine = new StandardJMeterEngine();
    ThreadGroup tGroup = new ThreadGroup();
    tGroup.addListener(ol);
    tGroup.addSamplerController(sc);
    tGroup.setName("Group 1");
    tGroup.setNumThreads(1);
    tGroup.addTimer(new GaussianRandomTimer());
    engine.addThreadGroup(tGroup);

  }

  /************************************************************
   *  !ToDo (Method description)
   ***********************************************************/
  public void run()
  {
    engine.runTest();
  }

  private SamplerController createTestSample(String url) throws MalformedURLException
  {
    UrlConfig config = new UrlConfig();
    URL u = new URL(url);
    config.setName(u.toString());
    config.putProperty(UrlConfig.METHOD, UrlConfig.GET);
    config.putProperty(UrlConfig.DOMAIN, u.getHost());
    config.putProperty(UrlConfig.PATH, u.getFile());
    SamplerController test = new HttpTestSample();
    test.addConfigElement(config);
    return test;
  }

  /************************************************************
   *  !ToDo (Method description)
   *
   *@param  args                                !ToDo (Parameter description)
   *@exception  java.net.MalformedURLException  !ToDo (Exception description)
   ***********************************************************/
  public static void main(String[] args)
       throws java.net.MalformedURLException
  {
    if(args.length < 1)
    {
      System.err.println(USAGE);
      System.exit(-1);
    }

    String url = args[args.length - 1];
    String outfile = "jmeter.out";
    String propsFile = null;

    char opt = (char)-1;
    for(int i = 0; i < (args.length - 1); i++)
    {
      if(opt == (char)-1)
      {
        if(args[i].charAt(0) == '-')
        {
          opt = args[i].charAt(1);
        }
        else
        {
          System.err.println("Illegal option " + args[i]);
          System.exit(-1);
        }
      }
      else
      {
        switch (opt)
        {
          case 'o':
            outfile = args[i];
            break;
          case 'p':
            propsFile = args[i];
            break;
          default:
            System.err.println("Unknown option: " + opt);
            System.exit(-1);
        }
        opt = (char)-1;
      }
    }

    try
    {
      TextDriver td = new TextDriver(outfile, url, propsFile);
      td.run();
    }
    catch(MalformedURLException e)
    {
      System.out.println("The URL you gave was not syntactically valid");
    }
  }

  /************************************************************
   *  !ToDo (Class description)
   *
   *@author     $Author: mstover1 $
   *@created    $Date: 2001/07/26 00:34:43 $
   *@version    $Revision: 1.11 $
   ***********************************************************/
  private class OutputListener implements SampleListener
  {
    Filer viz;

    private OutputListener(String file)
    {
      viz = new Filer();
      viz.setFile(file);
      viz.open();
    }

    /************************************************************
     *  !ToDo (Method description)
     ***********************************************************/
    public void flush()
    {
      try
      {
        viz.flush();
      }
      catch(Exception e)
      {
      }
    }

    /************************************************************
     *  !ToDo (Method description)
     *
     *@exception  IOException  !ToDo (Exception description)
     ***********************************************************/
    public void close() throws IOException
    {
      viz.close();
    }

    /************************************************************
     *  !ToDo (Method description)
     *
     *@param  e  !ToDo (Parameter description)
     ***********************************************************/
    public void sampleOccurred(SampleEvent e)
    {
      viz.sampleOccurred(e);
      flush();
    }

    /************************************************************
     *  !ToDo (Method description)
     *
     *@param  e  !ToDo (Parameter description)
     ***********************************************************/
    public void sampleStarted(SampleEvent e)
    {
    }

    /************************************************************
     *  !ToDo (Method description)
     *
     *@param  e  !ToDo (Parameter description)
     ***********************************************************/
    public void sampleStopped(SampleEvent e)
    {
    }
  }
}
TOP

Related Classes of org.apache.jmeter.TextDriver$OutputListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.