You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by wo...@apache.org on 2004/03/16 04:28:49 UTC

cvs commit: jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser Constants.java MonitorHandler.java ParserImpl.java

woolfel     2004/03/15 19:28:49

  Modified:    src/monitor/model/org/apache/jmeter/monitor/parser
                        Constants.java MonitorHandler.java ParserImpl.java
  Log:
  the implementation of the custom DocumentHandler for SAX 
  is done and working. I've tested it using a main method and hitting
  tomcat5. I still need to add logging in the case that xml is not
  well-formed. The MonitorHandler does not validate the xml, so it
  eliminates the overhead. Later on, once JaxMe is ready I may
  replace this with jaxme.
  
  peter lin
  
  Revision  Changes    Path
  1.2       +15 -10    jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/Constants.java
  
  Index: Constants.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/Constants.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Constants.java	14 Mar 2004 17:37:35 -0000	1.1
  +++ Constants.java	16 Mar 2004 03:28:49 -0000	1.2
  @@ -1,18 +1,23 @@
  +// $Header:
   /*
  - * Created on Mar 13, 2004
  + * Copyright 2001-2004 The Apache Software Foundation.
    *
  - * To change the template for this generated file go to
  - * Window>Preferences>Java>Code Generation>Code and Comments
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
  + *
  + *   http://www.apache.org/licenses/LICENSE-2.0
  + *
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
    */
   package org.apache.jmeter.monitor.parser;
   
   /**
  - * @author pete<p>
  - * @version 0.1<p>
  - * 
  - * Description:<p>
  - * To change the template for this generated type comment go to
  - * Window>Preferences>Java>Code Generation>Code and Comments
  + * Constants for the custom DocumentHandler.
    */
   public class Constants
   {
  @@ -33,7 +38,7 @@
   	public static final String MINSPARETHREADS = "minSpareThreads";
   	public static final String MAXSPARETHREADS = "maxSpareThreads";
   	public static final String CURRENTTHREADCOUNT = "currentThreadCount";
  -	public static final String CURRENTBUSYTHREADS = "currentThreadsBusy=";
  +	public static final String CURRENTBUSYTHREADS = "currentThreadsBusy";
   	public static final String MAXTIME = "maxTime=";
   	public static final String PROCESSINGTIME = "processingTime=";
   	public static final String REQUESTCOUNT = "requestCount";
  
  
  
  1.4       +267 -8    jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/MonitorHandler.java
  
  Index: MonitorHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/MonitorHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MonitorHandler.java	13 Mar 2004 23:16:07 -0000	1.3
  +++ MonitorHandler.java	16 Mar 2004 03:28:49 -0000	1.4
  @@ -16,13 +16,42 @@
    */
   package org.apache.jmeter.monitor.parser;
   
  +import java.util.List;
  +import java.util.Stack;
  +
   import org.xml.sax.Attributes;
   import org.xml.sax.SAXException;
   import org.xml.sax.helpers.DefaultHandler;
   
  +import org.apache.jmeter.monitor.model.ObjectFactory;
  +import org.apache.jmeter.monitor.model.Connector;
  +import org.apache.jmeter.monitor.model.Jvm;
  +import org.apache.jmeter.monitor.model.Memory;
  +import org.apache.jmeter.monitor.model.RequestInfo;
  +import org.apache.jmeter.monitor.model.Status;
  +import org.apache.jmeter.monitor.model.StatusImpl;
  +import org.apache.jmeter.monitor.model.ThreadInfo;
  +import org.apache.jmeter.monitor.model.Worker;
  +import org.apache.jmeter.monitor.model.Workers;
  +import org.apache.jmeter.monitor.model.WorkersImpl;
  +
   public class MonitorHandler extends DefaultHandler
   {
  +	private boolean startDoc = false;
  +	private boolean endDoc = false;
  +	private Stack stacktree = new Stack();
   
  +	private ObjectFactory factory = null;	
  +	private Status status = null;
  +	private Jvm jvm = null;
  +	private Memory memory = null;
  +	private Connector connector = null;
  +	private ThreadInfo threadinfo = null;
  +	private RequestInfo requestinfo = null;
  +	private Worker worker = null;
  +	private Workers workers = null;
  +	private List workerslist = null;
  +	
       /**
        * 
        */
  @@ -31,14 +60,25 @@
           super();
       }
   
  +	/**
  +	 * Set the ObjectFactory used to create new 
  +	 * @param factory
  +	 */
  +	public void setObjectFactory(ObjectFactory factory){
  +		this.factory = factory;
  +	}
  +	
   	public void startDocument ()
   	throws SAXException
   	{
  +		this.startDoc = true;
   	}
   	
   	public void endDocument ()
   	throws SAXException
   	{
  +		this.startDoc = false;
  +		this.endDoc = true;
   	}
   
   	/**
  @@ -49,9 +89,7 @@
   	 * each element (such as allocating a new tree node or writing
   	 * output to a file).</p>
   	 *
  -	 * @param uri
  -	 * @param localName The element type name.
  -	 * @param qName
  +	 * @param name The element type name.
   	 * @param attributes The specified or defaulted attributes.
   	 * @exception org.xml.sax.SAXException Any SAX exception, possibly
   	 *            wrapping another exception.
  @@ -61,7 +99,152 @@
   				  String qName, Attributes attributes)
   	throws SAXException
   	{
  -	// no op
  +		if (qName.equals(Constants.STATUS)){
  +			status = factory.createStatus();
  +			stacktree.push(status);
  +		} else if (qName.equals(Constants.JVM)){
  +			jvm = factory.createJvm();
  +			if (stacktree.peek() instanceof Status){
  +				status.setJvm(jvm);
  +				stacktree.push(jvm);
  +			}
  +		} else if (qName.equals(Constants.MEMORY)){
  +			memory = factory.createMemory();
  +			if (stacktree.peek() instanceof Jvm){
  +				stacktree.push(memory);
  +				if (attributes != null){
  +					for (int idx=0; idx < attributes.getLength(); idx++){
  +						String attr = attributes.getQName(idx);
  +						if (attr.equals(Constants.MEMORY_FREE)){
  +							memory.
  +								setFree(parseLong(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.MEMORY_TOTAL)){
  +							memory.
  +								setTotal(parseLong(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.MEMORY_MAX)){
  +							memory.
  +								setMax(parseLong(attributes.getValue(idx)));
  +						}
  +					}
  +				}
  +				jvm.setMemory(memory);
  +			}
  +		} else if (qName.equals(Constants.CONNECTOR)){
  +			connector = factory.createConnector();
  +			if (stacktree.peek() instanceof Status ||
  +			stacktree.peek() instanceof Connector){
  +				((StatusImpl)status).addConnector(connector);
  +				stacktree.push(connector);
  +				if (attributes != null){
  +					for (int idx=0; idx < attributes.getLength(); idx++){
  +						String attr = attributes.getQName(idx);
  +						if (attr.equals(Constants.ATTRIBUTE_NAME)){
  +							connector.setName(attributes.getValue(idx));
  +						}
  +					}
  +				}
  +			}
  +		} else if (qName.equals(Constants.THREADINFO)){
  +			threadinfo = factory.createThreadInfo();
  +			if (stacktree.peek() instanceof Connector){
  +				stacktree.push(threadinfo);
  +				connector.setThreadInfo(threadinfo);
  +				if (attributes != null){
  +					for (int idx=0; idx < attributes.getLength(); idx++){
  +						String attr = attributes.getQName(idx);
  +						if (attr.equals(Constants.MAXTHREADS)){
  +							threadinfo.setMaxThreads(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.MINSPARETHREADS)){
  +							threadinfo.setMinSpareThreads(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.MAXSPARETHREADS)){
  +							threadinfo.setMaxSpareThreads(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.CURRENTTHREADCOUNT)){
  +							threadinfo.setCurrentThreadCount(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.CURRENTBUSYTHREADS)){
  +							threadinfo.setCurrentThreadsBusy(
  +								parseInt(attributes.getValue(idx)));
  +						}
  +					}
  +				}
  +			}
  +		} else if (qName.equals(Constants.REQUESTINFO)){
  +			requestinfo = factory.createRequestInfo();
  +			if (stacktree.peek() instanceof Connector){
  +				stacktree.push(requestinfo);
  +				connector.setRequestInfo(requestinfo);
  +				if (attributes != null){
  +					for (int idx=0; idx < attributes.getLength(); idx++){
  +						String attr = attributes.getQName(idx);
  +						if (attr.equals(Constants.MAXTIME)){
  +							requestinfo.setMaxTime(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.PROCESSINGTIME)){
  +							requestinfo.setProcessingTime(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.REQUESTCOUNT)){
  +							requestinfo.setRequestCount(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.ERRORCOUNT)){
  +							requestinfo.setErrorCount(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.BYTESRECEIVED)){
  +							requestinfo.setBytesReceived(
  +								parseLong(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.BYTESSENT)){
  +							requestinfo.setBytesSent(
  +								parseLong(attributes.getValue(idx)));
  +						}
  +					}
  +				}
  +			}
  +		} else if (qName.equals(Constants.WORKERS)){
  +			workers = factory.createWorkers();
  +			if (stacktree.peek() instanceof Connector){
  +				connector.setWorkers(workers);
  +				stacktree.push(workers);
  +			}
  +		} else if (qName.equals(Constants.WORKER)){
  +			worker = factory.createWorker();
  +			if (stacktree.peek() instanceof Workers ||
  +			stacktree.peek() instanceof Worker){
  +				stacktree.push(worker);
  +				((WorkersImpl)workers).addWorker(worker);
  +				if (attributes != null){
  +					for (int idx=0; idx < attributes.getLength(); idx++){
  +						String attr = attributes.getQName(idx);
  +						if (attr.equals(Constants.STAGE)){
  +							worker.setStage(attributes.getValue(idx));
  +						} else if (attr.equals(Constants.REQUESTPROCESSINGTIME)){
  +							worker.setRequestProcessingTime(
  +								parseInt(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.REQUESTBYTESSENT)){
  +							worker.setRequestBytesSent(
  +								parseLong(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.REQUESTBYTESRECEIVED)){
  +							worker.setRequestBytesReceived(
  +								parseLong(attributes.getValue(idx)));
  +						} else if (attr.equals(Constants.REMOTEADDR)){
  +							worker.setRemoteAddr(attributes.getValue(idx));
  +						} else if (attr.equals(Constants.VIRTUALHOST)){
  +							worker.setVirtualHost(attributes.getValue(idx));
  +						} else if (attr.equals(Constants.METHOD)){
  +							worker.setMethod(attributes.getValue(idx));
  +						} else if (attr.equals(Constants.CURRENTURI)){
  +							worker.setCurrentUri(attributes.getValue(idx));
  +						} else if (attr.equals(Constants.CURRENTQUERYSTRING)){
  +							worker.setCurrentQueryString(
  +								attributes.getValue(idx));
  +						} else if (attr.equals(Constants.PROTOCOL)){
  +							worker.setProtocol(attributes.getValue(idx));
  +						}
  +					}
  +				}
  +			}
  +		}
   	}
       
       
  @@ -73,9 +256,8 @@
   	 * each element (such as finalising a tree node or writing
   	 * output to a file).</p>
   	 *
  -	 * @param uri
  -	 * @param localName The element type name.
  -	 * @param qName
  +	 * @param name The element type name.
  +	 * @param attributes The specified or defaulted attributes.
   	 * @exception org.xml.sax.SAXException Any SAX exception, possibly
   	 *            wrapping another exception.
   	 * @see org.xml.sax.ContentHandler#endElement
  @@ -83,6 +265,41 @@
   	public void endElement (String uri, String localName, String qName)
   	throws SAXException
   	{
  +		if (qName.equals(Constants.STATUS)){
  +			if (stacktree.peek() instanceof Status){
  +				stacktree.pop();
  +			}
  +		} else if (qName.equals(Constants.JVM)){
  +			if (stacktree.peek() instanceof Jvm){
  +				stacktree.pop();
  +			}
  +		} else if (qName.equals(Constants.MEMORY)){
  +			if (stacktree.peek() instanceof Memory){
  +				stacktree.pop();
  +			}
  +		} else if (qName.equals(Constants.CONNECTOR)){
  +			if (stacktree.peek() instanceof Connector ||
  +			stacktree.peek() instanceof Connector){
  +				stacktree.pop();
  +			}
  +		} else if (qName.equals(Constants.THREADINFO)){
  +			if (stacktree.peek() instanceof ThreadInfo){
  +				stacktree.pop();
  +			}
  +		} else if (qName.equals(Constants.REQUESTINFO)){
  +			if (stacktree.peek() instanceof RequestInfo){
  +				stacktree.pop();
  +			}
  +		} else if (qName.equals(Constants.WORKERS)){
  +			if (stacktree.peek() instanceof Workers){
  +				stacktree.pop();
  +			}
  +		} else if (qName.equals(Constants.WORKER)){
  +			if (stacktree.peek() instanceof Worker ||
  +			stacktree.peek() instanceof Worker){
  +				stacktree.pop();
  +			}
  +		}
   	}
   
   	/**
  @@ -105,5 +322,47 @@
   	throws SAXException
   	{
   	}
  +
  +	/**
  +	 * Convienance method for parsing long. If the string
  +	 * was not a number, the method returns zero.
  +	 * @param data
  +	 * @return
  +	 */    
  +    public long parseLong(String data){
  +    	long val = 0;
  +    	if (data.length() > 0){
  +			try {
  +				val = Long.parseLong(data);
  +			} catch (NumberFormatException e){
  +				val = 0;
  +			}
  +    	}
  +    	return val;
  +    }
       
  +    /**
  +     * Convienance method for parsing integers. 
  +     * @param data
  +     * @return
  +     */
  +    public int parseInt(String data){
  +    	int val = 0;
  +    	if (data.length() > 0){
  +			try {
  +				val = Integer.parseInt(data);
  +			} catch (NumberFormatException e){
  +				val = 0;
  +			}
  +    	}
  +    	return val;
  +    }
  +    
  +    /**
  +     * method returns the status object.
  +     * @return
  +     */
  +    public Status getContents(){
  +    	return this.status;
  +    }
   }
  
  
  
  1.3       +41 -14    jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/ParserImpl.java
  
  Index: ParserImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/monitor/model/org/apache/jmeter/monitor/parser/ParserImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ParserImpl.java	13 Mar 2004 22:45:16 -0000	1.2
  +++ ParserImpl.java	16 Mar 2004 03:28:49 -0000	1.3
  @@ -16,7 +16,12 @@
    */
   package org.apache.jmeter.monitor.parser;
   
  +import java.io.ByteArrayInputStream;
  +import java.io.IOException;
  +
   import org.xml.sax.SAXException;
  +import org.xml.sax.InputSource;
  +
   import javax.xml.parsers.ParserConfigurationException;
   import javax.xml.parsers.SAXParser;
   import javax.xml.parsers.SAXParserFactory;
  @@ -38,38 +43,60 @@
       public ParserImpl(ObjectFactory factory)
       {
           super();
  -        FACTORY = factory;
  +        this.FACTORY = factory;
           try {
   			PARSERFACTORY = SAXParserFactory.newInstance();
   			PARSER = PARSERFACTORY.newSAXParser();
   			DOCHANDLER = new MonitorHandler();
  +			DOCHANDLER.setObjectFactory(this.FACTORY);
           } catch (SAXException e) {
  +        	// e.printStackTrace();
  +        	// need to add logging later
           } catch (ParserConfigurationException e){
  +			// e.printStackTrace();
  +			// need to add logging later
           }
       }
   
  -    /* (non-Javadoc)
  -     * @see org.apache.jmeter.monitor.parser.Parser#parseBytes(byte[])
  -     */
  +	/**
  +	 * parse byte array and return Status object
  +	 * @param byte[]
  +	 * @return Status
  +	 */
       public Status parseBytes(byte[] bytes)
       {
  -        return null;
  +    	try {
  +			InputSource is = new InputSource();
  +			is.setByteStream(new ByteArrayInputStream(bytes));
  +			PARSER.parse(is,DOCHANDLER);
  +			return DOCHANDLER.getContents();
  +    	} catch (SAXException e){
  +			e.printStackTrace();
  +			// let bad input fail silently
  +			return DOCHANDLER.getContents();
  +    	} catch (IOException e){
  +			e.printStackTrace();
  +			// let bad input fail silently
  +			return DOCHANDLER.getContents();
  +    	}
       }
   
  -    /* (non-Javadoc)
  -     * @see org.apache.jmeter.monitor.parser.Parser#parseString(java.lang.String)
  -     */
  +	/**
  +	 * @param String data
  +	 * @return Status
  +	 */
       public Status parseString(String content)
       {
  -        return null;
  +        return parseBytes(content.getBytes());
       }
   
  -    /* (non-Javadoc)
  -     * @see org.apache.jmeter.monitor.parser.Parser#parseSampleResult(org.apache.jmeter.samplers.SampleResult)
  -     */
  +	/**
  +	 * @param SampleResult result
  +	 * @return Status
  +	 */
       public Status parseSampleResult(SampleResult result)
       {
  -        return null;
  +        return parseBytes(result.getResponseData());
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org