Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add fixed length support
  • Loading branch information
jinfahua committed Mar 29, 2018
1 parent a08fb67 commit 124a648
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 18 deletions.
Expand Up @@ -59,6 +59,8 @@ public class TCPConfigGui extends AbstractConfigGui {
private JTextField soLinger;

private JTextField eolByte;

private JTextField responseLenth;

private JSyntaxTextArea requestData;

Expand Down Expand Up @@ -95,6 +97,7 @@ public void configure(TestElement element) {
closeConnection.setTristateFromProperty(element, TCPSampler.CLOSE_CONNECTION);
soLinger.setText(element.getPropertyAsString(TCPSampler.SO_LINGER));
eolByte.setText(element.getPropertyAsString(TCPSampler.EOL_BYTE));
responseLenth.setText(element.getPropertyAsString(TCPSampler.LENGTH, ""));
}

@Override
Expand Down Expand Up @@ -124,6 +127,7 @@ public void modifyTestElement(TestElement element) {
closeConnection.setPropertyFromTristate(element, TCPSampler.CLOSE_CONNECTION); // Don't use default for saving tristates
element.setProperty(TCPSampler.SO_LINGER, soLinger.getText(), "");
element.setProperty(TCPSampler.EOL_BYTE, eolByte.getText(), "");
element.setProperty(TCPSampler.LENGTH, responseLenth.getText(), "");
}

/**
Expand All @@ -141,6 +145,7 @@ public void clearGui() {
closeConnection.setSelected(TCPSampler.CLOSE_CONNECTION_DEFAULT); // TODO should this be indeterminate?
soLinger.setText(""); //$NON-NLS-1$
eolByte.setText(""); //$NON-NLS-1$
responseLenth.setText("");
}


Expand Down Expand Up @@ -212,6 +217,19 @@ private JPanel createEolBytePanel() {
eolBytePanel.add(eolByte);
return eolBytePanel;
}

private JPanel createLengthPanel() {
JLabel label = new JLabel(JMeterUtils.getResString("response_length")); //$NON-NLS-1$

responseLenth = new JTextField(3); // 3 columns size
responseLenth.setMaximumSize(new Dimension(responseLenth.getPreferredSize()));
label.setLabelFor(responseLenth);

JPanel eolBytePanel = new JPanel(new FlowLayout());
eolBytePanel.add(label);
eolBytePanel.add(responseLenth);
return eolBytePanel;
}

private JPanel createRequestPanel() {
JLabel reqLabel = new JLabel(JMeterUtils.getResString("tcp_request_data")); // $NON-NLS-1$
Expand Down Expand Up @@ -249,6 +267,7 @@ private void init() { // WARNING: called from ctor so must not be overridden (i.
optionsPanel.add(createNoDelayPanel());
optionsPanel.add(createSoLingerOption());
optionsPanel.add(createEolBytePanel());
optionsPanel.add(createLengthPanel());
mainPanel.add(optionsPanel);
mainPanel.add(createRequestPanel());

Expand Down
Expand Up @@ -29,7 +29,7 @@ public abstract class AbstractTCPClient implements TCPClient {
private String charset;
protected byte eolByte;
protected boolean useEolByte = false;

protected int length = -1;
/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -88,4 +88,17 @@ public void setCharset(String charset) {
public String read(InputStream is, SampleResult sampleResult) throws ReadException {
return read(is);
}

@Override
public int getLength() {
if(useEolByte) {
return -1;
}
return length;
}

@Override
public void setLength(int length) {
this.length = length;
}
}
Expand Up @@ -131,15 +131,23 @@ public String read(InputStream is, SampleResult sampleResult) throws ReadExcepti
byte[] buffer = new byte[4096];
int x = 0;
boolean first = true;
while ((x = is.read(buffer)) > -1) {
if (first) {
if(getLength() == -1) {
while ((x = is.read(buffer)) > -1) {
if (first) {
sampleResult.latencyEnd();
first = false;
}
w.write(buffer, 0, x);
if (useEolByte && (buffer[x - 1] == eolByte)) {
break;
}
}
} else {
buffer = new byte[length];
if ((x = is.read(buffer, 0, length)) > -1) {
sampleResult.latencyEnd();
first = false;
}
w.write(buffer, 0, x);
if (useEolByte && (buffer[x - 1] == eolByte)) {
break;
}
w.write(buffer, 0, x);
}
}

IOUtils.closeQuietly(w); // For completeness
Expand Down
Expand Up @@ -107,4 +107,19 @@ public interface TCPClient {
* The value to set
*/
void setEolByte(int eolInt);

/**
* Get the response length setting
*
* @return
*/
int getLength();


/**
* Set the length of returned response.
*
* @param length
*/
void setLength(int length);
}
Expand Up @@ -111,15 +111,23 @@ public String read(InputStream is, SampleResult sampleResult) throws ReadExcepti
byte[] buffer = new byte[4096];
int x;
boolean first = true;
while ((x = is.read(buffer)) > -1) {
if (first) {
sampleResult.latencyEnd();
first = false;
}
w.write(buffer, 0, x);
if (useEolByte && (buffer[x - 1] == eolByte)) {
break;
}
if(getLength() == -1) {
while ((x = is.read(buffer)) > -1) {
if (first) {
sampleResult.latencyEnd();
first = false;
}
w.write(buffer, 0, x);
if (useEolByte && (buffer[x - 1] == eolByte)) {
break;
}
}
} else {
buffer = new byte[length];
if ((x = is.read(buffer, 0, length)) > -1) {
sampleResult.latencyEnd();
w.write(buffer, 0, x);
}
}

// do we need to close byte array (or flush it?)
Expand Down
Expand Up @@ -90,6 +90,8 @@ public class TCPSampler extends AbstractSampler implements ThreadListener, Inter
public static final String SO_LINGER = "TCPSampler.soLinger"; //$NON-NLS-1$

public static final String EOL_BYTE = "TCPSampler.EolByte"; //$NON-NLS-1$

public static final String LENGTH = "TCPSampler.length"; //$NON-NLS-1$

private static final String TCPKEY = "TCP"; //$NON-NLS-1$ key for HashMap

Expand Down Expand Up @@ -335,9 +337,13 @@ private TCPClient getProtocol() {
}
try {
tcpClient = (TCPClient) javaClass.newInstance();
/**The EOL byte setting has higher priority, if both EOL byte & length are set, then EOL byte will be used, and length will be ignored.**/
if (getPropertyAsString(EOL_BYTE, "").length()>0){
tcpClient.setEolByte(getEolByte());
log.info("Using eolByte={}", getEolByte());
} else if (getPropertyAsString(LENGTH, "").trim().length() > 0) {
tcpClient.setLength(Integer.parseInt(getPropertyAsString(LENGTH, "").trim()));
log.info("Using length={}", getPropertyAsString(LENGTH, ""));
}

if (log.isDebugEnabled()) {
Expand Down

0 comments on commit 124a648

Please sign in to comment.