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 se...@apache.org on 2003/11/16 03:09:07 UTC
cvs commit: jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/control/gui TCPSamplerGui.java
sebb 2003/11/15 18:09:07
Added: src/protocol/tcp/org/apache/jmeter/protocol/tcp/config/gui
TCPConfigGui.java
src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler
TCPClientImpl.java TCPClient.java TCPSampler.java
src/protocol/tcp/org/apache/jmeter/protocol/tcp/config
TCPConfig.java
src/protocol/tcp/org/apache/jmeter/protocol/tcp/control/gui
TCPSamplerGui.java
Log:
Initial release
Revision Changes Path
1.1 jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/config/gui/TCPConfigGui.java
Index: TCPConfigGui.java
===================================================================
package org.apache.jmeter.protocol.tcp.config.gui;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.config.gui.AbstractConfigGui;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/16 02:09:06 $
*/
public class TCPConfigGui extends AbstractConfigGui
{
private final static String SERVER = "server"; //$NON-NLS-1$
private final static String PORT = "port"; //$NON-NLS-1$
private final static String FILENAME = "filename"; //$NON-NLS-1$
private final static String TIMEOUT = "timeout"; //$NON-NLS-1$
private final static String NODELAY = "nodelay"; //$NON-NLS-1$
private JTextField server;
private JTextField port;
private JTextField filename;
private JTextField timeout;
private JTextField nodelay;
private boolean displayName = true;
public TCPConfigGui()
{
this(true);
}
public TCPConfigGui(boolean displayName)
{
this.displayName = displayName;
init();
}
public String getStaticLabel()
{
return JMeterUtils.getResString("tcp_sample_title");
}
public void configure(TestElement element)
{
super.configure(element);
server.setText(element.getPropertyAsString(TCPSampler.SERVER));
port.setText(element.getPropertyAsString(TCPSampler.PORT));
filename.setText(element.getPropertyAsString(TCPSampler.FILENAME));
timeout.setText(element.getPropertyAsString(TCPSampler.TIMEOUT));
nodelay.setText(element.getPropertyAsString(TCPSampler.NODELAY));
}
public TestElement createTestElement()
{
ConfigTestElement element = new ConfigTestElement();
modifyTestElement(element);
return element;
}
/**
* Modifies a given TestElement to mirror the data in the gui components.
* @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
*/
public void modifyTestElement(TestElement element)
{
configureTestElement(element);
element.setProperty(TCPSampler.SERVER, server.getText());
element.setProperty(TCPSampler.PORT, port.getText());
element.setProperty(TCPSampler.FILENAME, filename.getText());
element.setProperty(TCPSampler.NODELAY, nodelay.getText());
element.setProperty(TCPSampler.TIMEOUT, timeout.getText());
}
private JPanel createTimeoutPanel()
{
JLabel label = new JLabel(JMeterUtils.getResString("tcp_timeout"));
timeout = new JTextField(10);
timeout.setName(TIMEOUT);
label.setLabelFor(timeout);
JPanel timeoutPanel = new JPanel(new BorderLayout(5, 0));
timeoutPanel.add(label, BorderLayout.WEST);
timeoutPanel.add(timeout, BorderLayout.CENTER);
return timeoutPanel;
}
private JPanel createNoDelayPanel()
{
JLabel label = new JLabel(JMeterUtils.getResString("tcp_nodelay"));
nodelay = new JTextField(10);
nodelay.setName(NODELAY);
label.setLabelFor(nodelay);
JPanel nodelayPanel = new JPanel(new BorderLayout(5, 0));
nodelayPanel.add(label, BorderLayout.WEST);
nodelayPanel.add(nodelay, BorderLayout.CENTER);
return nodelayPanel;
}
private JPanel createServerPanel()
{
JLabel label = new JLabel(JMeterUtils.getResString("server"));
server = new JTextField(10);
server.setName(SERVER);
label.setLabelFor(server);
JPanel serverPanel = new JPanel(new BorderLayout(5, 0));
serverPanel.add(label, BorderLayout.WEST);
serverPanel.add(server, BorderLayout.CENTER);
return serverPanel;
}
private JPanel createPortPanel()
{
JLabel label = new JLabel(JMeterUtils.getResString("tcp_port"));
port = new JTextField(10);
port.setName(PORT);
label.setLabelFor(port);
JPanel PortPanel = new JPanel(new BorderLayout(5, 0));
PortPanel.add(label, BorderLayout.WEST);
PortPanel.add(port, BorderLayout.CENTER);
return PortPanel;
}
private JPanel createFilenamePanel()
{
JLabel label = new JLabel(JMeterUtils.getResString("file_to_retrieve"));
filename = new JTextField(10);
filename.setName(FILENAME);
label.setLabelFor(filename);
JPanel filenamePanel = new JPanel(new BorderLayout(5, 0));
filenamePanel.add(label, BorderLayout.WEST);
filenamePanel.add(filename, BorderLayout.CENTER);
return filenamePanel;
}
private void init()
{
setLayout(new BorderLayout(0, 5));
if (displayName)
{
setBorder(makeBorder());
add(makeTitlePanel(), BorderLayout.NORTH);
}
VerticalPanel mainPanel = new VerticalPanel();
mainPanel.add(createServerPanel());
mainPanel.add(createPortPanel());
mainPanel.add(createTimeoutPanel());
mainPanel.add(createNoDelayPanel());
mainPanel.add(createFilenamePanel());
add(mainPanel, BorderLayout.CENTER);
}
}
1.1 jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java
Index: TCPClientImpl.java
===================================================================
/*
* Basic TCP Sampler Client class
*
* Can be used to test the TCP Sampler against an HTTP server
*
* The protocol handler class name is defined by the property tcp.handler
*
*/
package org.apache.jmeter.protocol.tcp.sampler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketTimeoutException;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
/**
*
* @author sebb AT apache DOT org
* @version $Revision: 1.1 $ $Date: 2003/11/16 02:09:06 $
*
*/
public class TCPClientImpl implements TCPClient
{
private static Logger log = LoggingManager.getLoggerForClass();
public TCPClientImpl()
{
super();
log.info("Created "+this);
}
/* (non-Javadoc)
* @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#setupTest()
*/
public void setupTest()
{
log.info("setuptest");
}
/* (non-Javadoc)
* @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#teardownTest()
*/
public void teardownTest()
{
log.info("teardowntest");
}
/* (non-Javadoc)
* @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#write(java.io.OutputStream)
*/
public String write(OutputStream os)
{
String s = "GET / HTTP/1.1\nHost: www.dummy.invalid\n\n"; //or get from file etc
try
{
os.write(s.getBytes());
os.flush();
}
catch (IOException e)
{
log.debug("Write error",e);
}
log.debug("Wrote: "+s);
return s;
}
/* (non-Javadoc)
* @see org.apache.jmeter.protocol.tcp.sampler.TCPClient#read(java.io.InputStream)
*/
public String read(InputStream is)
{
byte [] buffer = new byte[4096];
ByteArrayOutputStream w = new ByteArrayOutputStream();
int x = 0;
try {
while ((x = is.read(buffer)) > -1)
{
w.write(buffer, 0, x);
}
} catch (SocketTimeoutException e) {
// drop out to handle buffer
} catch (IOException e) {
log.debug("Read error",e);
return "";
}
// do we need to close byte array (or flush it?)
log.debug("Read:\n"+w.toString());
return w.toString();
}
}
1.1 jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java
Index: TCPClient.java
===================================================================
/*
* Created on 24-Sep-2003
*
* Interface for generic TCP protocol handler
*
*/
package org.apache.jmeter.protocol.tcp.sampler;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author sebb AT apache DOT org
* @version $revision$ $date$
*/
public interface TCPClient
{
void setupTest();
void teardownTest();
/**
*
* @param os - OutputStream for socket
* @return String written to socket
*/
String write(OutputStream os);
/**
*
* @param is - InputStream for socket
* @return String read from socket
*/
String read(InputStream is);
}
1.1 jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java
Index: TCPSampler.java
===================================================================
package org.apache.jmeter.protocol.tcp.sampler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
/**
* A sampler which understands Tcp requests.
*
* @version $Revision: 1.1 $ $Date: 2003/11/16 02:09:06 $
*/
public class TCPSampler extends AbstractSampler implements TestListener
{
protected static Logger log = LoggingManager.getLoggerForClass();
public final static String SERVER = "TCPSampler.server"; //$NON-NLS-1$
public final static String PORT = "TCPSampler.port"; //$NON-NLS-1$
public final static String FILENAME = "TCPSampler.filename"; //$NON-NLS-1$
public final static String CLASSNAME = "TCPSampler.classname";//$NON-NLS-1$
public final static String NODELAY = "TCPSampler.nodelay"; //$NON-NLS-1$
public final static String TIMEOUT = "TCPSampler.timeout"; //$NON-NLS-1$
private final static String TCPKEY = "TCP"; //$NON-NLS-1$ key for HashMap
private final static String ERRKEY = "ERR"; //$NON-NLS-1$ key for HashMap
private static Set allSockets = new HashSet();// Keep track of connections to allow close
/** the cache of TCP Connections */
private static ThreadLocal tp = new ThreadLocal(){
protected Object initialValue(){
return new HashMap();
}
};
public TCPSampler()
{
log.debug("Created "+this);
}
private String getError(){
Map cp = (Map) tp.get();
return (String) cp.get(ERRKEY);
}
private Socket getSocket() {
Map cp = (Map) tp.get();
Socket con = (Socket) cp.get(TCPKEY);
if (con != null) {
log.debug(this+" Reusing connection "+con); //$NON-NLS-1$
return (Socket) con;
}
// Not in cache, so create new one and cache it
try
{
con = new Socket(getServer(),getPort());
con.setSoTimeout(getTimeout());
con.setTcpNoDelay(getNoDelay());
log.debug(this+" Timeout "+getTimeout()+" NoDelay "+getNoDelay()); //$NON-NLS-1$
log.debug("Created new connection "+con); //$NON-NLS-1$
cp.put(TCPKEY,con);
allSockets.add(con);// Save so can be closed
}
catch (UnknownHostException e)
{
log.warn("Unknown host for "+getLabel(),e);//$NON-NLS-1$
cp.put(ERRKEY,e.toString());
}
catch (IOException e)
{
log.warn("Could not create socket for "+getLabel(),e); //$NON-NLS-1$
cp.put(ERRKEY,e.toString());
}
return con;
}
public String getUsername()
{
return getPropertyAsString(ConfigTestElement.USERNAME);
}
public String getPassword()
{
return getPropertyAsString(ConfigTestElement.PASSWORD);
}
public void setServer(String newServer)
{
this.setProperty(SERVER, newServer);
}
public String getServer()
{
return getPropertyAsString(SERVER);
}
public void setPort(String newFilename)
{
this.setProperty(PORT, newFilename);
}
public int getPort()
{
return getPropertyAsInt(PORT);
}
public void setFilename(String newFilename)
{
this.setProperty(FILENAME, newFilename);
}
public String getFilename()
{
return getPropertyAsString(FILENAME);
}
public void setTimeout(String newTimeout)
{
this.setProperty(FILENAME, newTimeout);
}
public int getTimeout()
{
return getPropertyAsInt(TIMEOUT);
}
public void setNoDelay(String newNoDelay)
{
this.setProperty(NODELAY, newNoDelay);
}
public boolean getNoDelay()
{
return getPropertyAsBoolean(NODELAY);
}
/**
* Returns a formatted string label describing this sampler
* Example output:
* Tcp://Tcp.nowhere.com/pub/README.txt
*
* @return a formatted string label describing this sampler
*/
public String getLabel()
{
return ("tcp://" + this.getServer() + ":" + this.getPort());//$NON-NLS-1$
}
private String getClassname()
{
String className = JMeterUtils.getPropDefault("tcp.handler","TCPClientImpl");
return className;
}
private static final String protoPrefix = "org.apache.jmeter.protocol.tcp.sampler.";
private Class getClass(String className)
{
Class c = null;
try
{
c = Class.forName(className
,false,Thread.currentThread().getContextClassLoader());
}
catch (ClassNotFoundException e)
{
try
{
c = Class.forName(protoPrefix+className
,false,Thread.currentThread().getContextClassLoader());
}
catch (ClassNotFoundException e1)
{
log.error("Could not find protocol class "+ className);
}
}
return c;
}
private Object getProtocol(){
Object TCPClient = null;
Class javaClass = getClass(getClassname());
try
{
TCPClient = javaClass.newInstance();
if (log.isDebugEnabled())
{
log.debug(this
+ "Created: "
+ getClassname()
+ "@"
+ Integer.toHexString(TCPClient.hashCode()));
}
}
catch (Exception e)
{
log.error(
this + " Exception creating: " + getClassname(),e);
}
return TCPClient;
}
public SampleResult sample(Entry e)// Entry tends to be ignored ...
{
log.info(getLabel()+" "+getFilename()+" "+getUsername()+" "+getPassword());
SampleResult res = new SampleResult();
boolean isSuccessful = false;
res.setSampleLabel(getLabel());
long start = System.currentTimeMillis();
try
{
Socket sock = getSocket();
if (sock == null){
res.setResponseCode("500");
res.setResponseMessage(getError());
} else {
InputStream is = sock.getInputStream();
OutputStream os = sock.getOutputStream();
TCPClient proto = (TCPClient) getProtocol();
log.debug("Found class "+ proto.toString());
String req=proto.write(os);
res.setSamplerData(req);
String in = proto.read(is);
res.setResponseData(in.getBytes());
res.setDataType(SampleResult.TEXT);
res.setResponseCode("200");
res.setResponseMessage("OK");
isSuccessful = true;
}
}
catch (Exception ex)
{
log.debug("",ex);
res.setResponseCode("500");
res.setResponseMessage(ex.toString());
}
// Calculate response time
long end = System.currentTimeMillis();
res.setTime(end - start);
// Set if we were successful or not
res.setSuccessful(isSuccessful);
return res;
}
private void disconnectAll(){
synchronized (allSockets)
{
Iterator i = allSockets.iterator();
while (i.hasNext())
{
Socket socket = (Socket) i.next();
try
{
socket.close();
}
catch (IOException e)
{
log.warn("Error closing socket ",e);
} finally {
i.remove();
}
}
}
}
/* (non-Javadoc)
* @see org.apache.jmeter.testelement.TestListener#testStarted()
*/
public void testStarted() // Only called once per class?
{
log.debug(this+" test started");
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.apache.jmeter.testelement.TestListener#testEnded()
*/
public void testEnded() // Only called once per class?
{
log.debug(this+" test ended");
disconnectAll();
}
/* (non-Javadoc)
* @see org.apache.jmeter.testelement.TestListener#testStarted(java.lang.String)
*/
public void testStarted(String host)
{
log.debug(this+" test started on "+host);
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.apache.jmeter.testelement.TestListener#testEnded(java.lang.String)
*/
public void testEnded(String host)
{
log.debug(this+" test ended on "+host);
disconnectAll();
}
/* (non-Javadoc)
* @see org.apache.jmeter.testelement.TestListener#testIterationStart(org.apache.jmeter.engine.event.LoopIterationEvent)
*/
public void testIterationStart(LoopIterationEvent event)
{
log.debug(this+" test iteration start on "+event.getIteration());
// TODO Auto-generated method stub
}
}
1.1 jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/config/TCPConfig.java
Index: TCPConfig.java
===================================================================
package org.apache.jmeter.protocol.tcp.config;
import java.io.Serializable;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/16 02:09:06 $
*/
public class TCPConfig extends ConfigTestElement implements Serializable
{
public TCPConfig()
{
}
public void setServer(String newServer)
{
this.setProperty(TCPSampler.SERVER, newServer);
}
public String getServer()
{
return getPropertyAsString(TCPSampler.SERVER);
}
public void setPort(String newPort)
{
this.setProperty(TCPSampler.PORT, newPort);
}
public int getPort()
{
return getPropertyAsInt(TCPSampler.PORT);
}
public void setFilename(String newFilename)
{
this.setProperty(TCPSampler.FILENAME, newFilename);
}
public String getFilename()
{
return getPropertyAsString(TCPSampler.FILENAME);
}
/**
* Returns a formatted string label describing this sampler
* Example output:
* Tcp://Tcp.nowhere.com:port
*
* @return a formatted string label describing this sampler
*/
public String getLabel()
{
return ("tcp://" + this.getServer() + ":" + this.getPort());
}
}
1.1 jakarta-jmeter/src/protocol/tcp/org/apache/jmeter/protocol/tcp/control/gui/TCPSamplerGui.java
Index: TCPSamplerGui.java
===================================================================
package org.apache.jmeter.protocol.tcp.control.gui;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import org.apache.jmeter.config.gui.LoginConfigGui;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.protocol.tcp.config.gui.TCPConfigGui;
import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/16 02:09:06 $
*/
public class TCPSamplerGui extends AbstractSamplerGui implements ActionListener
{
private static Logger log = LoggingManager.getLoggerForClass();
private LoginConfigGui loginPanel;
private TCPConfigGui TcpDefaultPanel;
public TCPSamplerGui()
{
init();
}
public void configure(TestElement element)
{
super.configure(element);
loginPanel.configure(element);
TcpDefaultPanel.configure(element);
}
public TestElement createTestElement()
{
TCPSampler sampler = new TCPSampler();
modifyTestElement(sampler);
return sampler;
}
/**
* Modifies a given TestElement to mirror the data in the gui components.
* @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
*/
public void modifyTestElement(TestElement sampler)
{
sampler.clear();
((TCPSampler) sampler).addTestElement(
TcpDefaultPanel.createTestElement());
((TCPSampler) sampler).addTestElement(loginPanel.createTestElement());
this.configureTestElement(sampler);
}
public String getStaticLabel()
{
return JMeterUtils.getResString("tcp_testing_title");
}
private void init()
{
setLayout(new BorderLayout(0, 5));
setBorder(makeBorder());
add(makeTitlePanel(), BorderLayout.NORTH);
VerticalPanel mainPanel = new VerticalPanel();
TcpDefaultPanel = new TCPConfigGui(false);
mainPanel.add(TcpDefaultPanel);
loginPanel = new LoginConfigGui(false);
loginPanel.setBorder(
BorderFactory.createTitledBorder(
JMeterUtils.getResString("login_config")));
mainPanel.add(loginPanel);
add(mainPanel, BorderLayout.CENTER);
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org