采样器地址为
1.结构图

还有两个文件
- ReadException:响应的异常,举例子就是服务端发生读取文本的问题,会产生异常
- TCPSampler:采样器的具体实现
2.方法理解
2.1、
1 //在线程启动时调用 2 void setupTest(); 3 4 //在线程结束时调用 5 void teardownTest(); 6 7 //抛出异常 8 void write(OutputStream os, InputStream is) throws IOException; 9 void write(OutputStream os, String s) throws IOException; 10 11 //返回一个从socket中读出的String(v3.3后已经过时) 12 String read(InputStream is) throws ReadException; 13 14 //返回一个从socket中读出的String 15 String read(InputStream is, SampleResult sampleResult) throws ReadException; 16 17 //获取行尾消息尾字节+返回 18 byte getEolByte(); 19 20 //字符集 21 String getCharset(); 22 23 //设置行尾/消息尾的字节,如果值超过了一个字节,就会被忽略 24 void setEolByte(int eolInt);
1 private String charset; //字符集
2 protected byte eolByte; //消息尾的
3 protected boolean useEolByte = false; //判定行尾字节是否超过限制的flag
4
5 public byte getEolByte(){}
6
7 public void setEolByte(int eolInt){}
8
9 public void setupTest(){}
10 public void teardownTest(){}
11
12 public void setCharset(String charset){} //设置字符集
13
14 //返回一个从socket中读出的String
15 public String read(InputStream is, SampleResult sampleResult) throws ReadException{}
1 //日志
2 private static final Logger log = LoggerFactory.getLogger(BinaryTCPClientImpl.class);
3
4 //EOM字节由属性“tcp.BinaryTCPClient.eomByte”定义,默认值1000
5 private static final int EOM_INT = JMeterUtils.getPropDefault("tcp.BinaryTCPClient.eomByte", 1000); // $NON_NLS-1$
6
7 //构造函数,设置消息尾的字节,没超过限制的话写入日志
8 public BinaryTCPClientImpl(){}
9
10 //把十六进制字符串转换成二进制字节数组
11 public static byte[] hexStringToByteArray(String hexEncodedBinary){}
12
13 //输入十六进制字符串通过hexStringToByteArray()转换成二进制数组,写入输出流。
14 public void write(OutputStream os, String hexEncodedBinary) throws IOException{}
15
16 //方法不支持过长的前缀?
17 public void write(OutputStream os, InputStream is){}
18
19 //过时的方法,警告写入日志,自动进入新方法read(InputStream is, SampleResult sampleResult)
20 public String read(InputStream is) throws ReadException{}
21
22 //***
23 //读数据直到碰到EOM byte,没有这个的话一直读到流结束
24 public String read(InputStream is, SampleResult sampleResult) throws ReadException{}
1 //日志
2 private static final Logger log = LoggerFactory.getLogger(TCPClientImpl.class);
3
4 //EOl字节由属性“tcp.eolByte”定义,默认值1000
5 //charset由属性“tcp.charset”定义
6 private static final int EOL_INT =
7 JMeterUtils.getPropDefault("tcp.eolByte", 1000);
8 private static final String CHARSET =
9 JMeterUtils.getPropDefault("tcp.charset", Charset.defaultCharset().name());
10
11 //构造函数
12 public TCPClientImpl(){}
13
14 //字符集写入流
15 public void write(OutputStream os, String s) throws IOException{
16 os.write(s.getBytes(CHARSET));
17 }
18
19 //输入流的缓冲区有东西,就写到输出流中
20 public void write(OutputStream os, InputStream is) throws IOException{}
21
22 //方法过时
23 public String read(InputStream is) throws ReadException{}
24
25 //读数据直到碰到EOl byte,没有这个的话一直读到流结束
26 public String read(InputStream is, SampleResult sampleResult) throws ReadException{}
27
28 //组装显示
29 private String showEOL(final String input){}
1 //把int数组转换成byte数组
2 public static byte[] intToByteArray(int value, int len){}
3
4 //把byte数组转换成int数组
5 public static int byteArrayToInt(byte[] b){}
1 //构造函数
2 public LengthPrefixedBinaryTCPClientImpl(){}
3
4 //byte数组写入输出流
5 public void write(OutputStream os, String s) throws IOException{}
6
7 //写入输出流
8 public void write(OutputStream os, InputStream is) throws IOException
9
10 //过时方法
11 public String read(InputStream is) throws ReadException{}
12
13 //
14 public String read(InputStream is, SampleResult sampleResult) throws ReadException{}
TCPClient classname:
有三种设置格式
TCPClientImpl:文本数据。 BinaryTCPClientImpl:传输二进制数据,指定包结束符。 LengthPrefixedBinaryTCPClientImpl:数据包中前2个字节为数据长度。 可在bin/jmeter.properties配置文件中tcp.binarylength.prefix.length设置。
TCP的数据以二进制之类的居多,所以为了方便则选择第二个进行设置。
所以填写org.apache.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl
先选用org.apache.jmeter.protocol.tcp.sampler.TCPClientImpl
**GUI对应表**Name**:在查看结果树中用于区分元素。保留,使用。**TCPClient classname**:最终选择的实现TCPClient的实现类,默认先去查看 tcp.handler,没有的话默认选择TCPClientImpl,当前只写一种方法,以防后续变更。保留,不使用。**ServerName or IP(必需)**:TCP服务端的IP。**Port Number(必需)**:服务端要使用的端口号。**Re-use connection(必需)**:选中则代表这个连接将会保持开启,知道数据读完才会关闭。默认开启**Close connection**:选中则代表连接在采样器运行完之后关闭。默认关闭**Set NoDelay**:选中则禁用Nagle算法(禁用适用于需要实时预览的通讯程序),nagle算法用于处理小报文段(微小分组)的发送。保留,程序内写死不禁用。**SO_LINGER**:在创建socket时,启用/禁用指定的挂起时间(以秒为单位)。可以将“SO LINGER”值设置为0,则可以防止大量套接字处于时间等待状态。保留,程序内写死设置为0。**End of line(EOL) byte value**:结束读取的值,设置在-128~127之外,以跳过EOL检查。保留,不设置。**Connect Timeout**:连接时延,不设置。**Response Timeout**:回复时延,不设置。**Text to Send(必需)**:要发送的文本。**Login User**:删除**Password**:删除