Skip to content

Commit 124a648

Browse files
committedMar 29, 2018
add fixed length support
1 parent a08fb67 commit 124a648

File tree

6 files changed

+87
-18
lines changed

6 files changed

+87
-18
lines changed
 

‎tcp/src/protocol/tcp/org/apache/jmeter/protocol/tcp/config/gui/TCPConfigGui.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class TCPConfigGui extends AbstractConfigGui {
5959
private JTextField soLinger;
6060

6161
private JTextField eolByte;
62+
63+
private JTextField responseLenth;
6264

6365
private JSyntaxTextArea requestData;
6466

@@ -95,6 +97,7 @@ public void configure(TestElement element) {
9597
closeConnection.setTristateFromProperty(element, TCPSampler.CLOSE_CONNECTION);
9698
soLinger.setText(element.getPropertyAsString(TCPSampler.SO_LINGER));
9799
eolByte.setText(element.getPropertyAsString(TCPSampler.EOL_BYTE));
100+
responseLenth.setText(element.getPropertyAsString(TCPSampler.LENGTH, ""));
98101
}
99102

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

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

146151

@@ -212,6 +217,19 @@ private JPanel createEolBytePanel() {
212217
eolBytePanel.add(eolByte);
213218
return eolBytePanel;
214219
}
220+
221+
private JPanel createLengthPanel() {
222+
JLabel label = new JLabel(JMeterUtils.getResString("response_length")); //$NON-NLS-1$
223+
224+
responseLenth = new JTextField(3); // 3 columns size
225+
responseLenth.setMaximumSize(new Dimension(responseLenth.getPreferredSize()));
226+
label.setLabelFor(responseLenth);
227+
228+
JPanel eolBytePanel = new JPanel(new FlowLayout());
229+
eolBytePanel.add(label);
230+
eolBytePanel.add(responseLenth);
231+
return eolBytePanel;
232+
}
215233

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

‎tcp/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/AbstractTCPClient.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract class AbstractTCPClient implements TCPClient {
2929
private String charset;
3030
protected byte eolByte;
3131
protected boolean useEolByte = false;
32-
32+
protected int length = -1;
3333
/**
3434
* {@inheritDoc}
3535
*/
@@ -88,4 +88,17 @@ public void setCharset(String charset) {
8888
public String read(InputStream is, SampleResult sampleResult) throws ReadException {
8989
return read(is);
9090
}
91+
92+
@Override
93+
public int getLength() {
94+
if(useEolByte) {
95+
return -1;
96+
}
97+
return length;
98+
}
99+
100+
@Override
101+
public void setLength(int length) {
102+
this.length = length;
103+
}
91104
}

‎tcp/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/BinaryTCPClientImpl.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,23 @@ public String read(InputStream is, SampleResult sampleResult) throws ReadExcepti
131131
byte[] buffer = new byte[4096];
132132
int x = 0;
133133
boolean first = true;
134-
while ((x = is.read(buffer)) > -1) {
135-
if (first) {
134+
if(getLength() == -1) {
135+
while ((x = is.read(buffer)) > -1) {
136+
if (first) {
137+
sampleResult.latencyEnd();
138+
first = false;
139+
}
140+
w.write(buffer, 0, x);
141+
if (useEolByte && (buffer[x - 1] == eolByte)) {
142+
break;
143+
}
144+
}
145+
} else {
146+
buffer = new byte[length];
147+
if ((x = is.read(buffer, 0, length)) > -1) {
136148
sampleResult.latencyEnd();
137-
first = false;
138-
}
139-
w.write(buffer, 0, x);
140-
if (useEolByte && (buffer[x - 1] == eolByte)) {
141-
break;
142-
}
149+
w.write(buffer, 0, x);
150+
}
143151
}
144152

145153
IOUtils.closeQuietly(w); // For completeness

‎tcp/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,19 @@ public interface TCPClient {
107107
* The value to set
108108
*/
109109
void setEolByte(int eolInt);
110+
111+
/**
112+
* Get the response length setting
113+
*
114+
* @return
115+
*/
116+
int getLength();
117+
118+
119+
/**
120+
* Set the length of returned response.
121+
*
122+
* @param length
123+
*/
124+
void setLength(int length);
110125
}

‎tcp/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPClientImpl.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,23 @@ public String read(InputStream is, SampleResult sampleResult) throws ReadExcepti
111111
byte[] buffer = new byte[4096];
112112
int x;
113113
boolean first = true;
114-
while ((x = is.read(buffer)) > -1) {
115-
if (first) {
116-
sampleResult.latencyEnd();
117-
first = false;
118-
}
119-
w.write(buffer, 0, x);
120-
if (useEolByte && (buffer[x - 1] == eolByte)) {
121-
break;
122-
}
114+
if(getLength() == -1) {
115+
while ((x = is.read(buffer)) > -1) {
116+
if (first) {
117+
sampleResult.latencyEnd();
118+
first = false;
119+
}
120+
w.write(buffer, 0, x);
121+
if (useEolByte && (buffer[x - 1] == eolByte)) {
122+
break;
123+
}
124+
}
125+
} else {
126+
buffer = new byte[length];
127+
if ((x = is.read(buffer, 0, length)) > -1) {
128+
sampleResult.latencyEnd();
129+
w.write(buffer, 0, x);
130+
}
123131
}
124132

125133
// do we need to close byte array (or flush it?)

‎tcp/src/protocol/tcp/org/apache/jmeter/protocol/tcp/sampler/TCPSampler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public class TCPSampler extends AbstractSampler implements ThreadListener, Inter
9090
public static final String SO_LINGER = "TCPSampler.soLinger"; //$NON-NLS-1$
9191

9292
public static final String EOL_BYTE = "TCPSampler.EolByte"; //$NON-NLS-1$
93+
94+
public static final String LENGTH = "TCPSampler.length"; //$NON-NLS-1$
9395

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

@@ -335,9 +337,13 @@ private TCPClient getProtocol() {
335337
}
336338
try {
337339
tcpClient = (TCPClient) javaClass.newInstance();
340+
/**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.**/
338341
if (getPropertyAsString(EOL_BYTE, "").length()>0){
339342
tcpClient.setEolByte(getEolByte());
340343
log.info("Using eolByte={}", getEolByte());
344+
} else if (getPropertyAsString(LENGTH, "").trim().length() > 0) {
345+
tcpClient.setLength(Integer.parseInt(getPropertyAsString(LENGTH, "").trim()));
346+
log.info("Using length={}", getPropertyAsString(LENGTH, ""));
341347
}
342348

343349
if (log.isDebugEnabled()) {

0 commit comments

Comments
 (0)
Please sign in to comment.