Jmeter TCP取样器


Jmeter TCP取样器

测试TCP服务器Socket链接

步骤

  • 创建线程组,在取样器执行错误时,选择 停止测试。
  • 创建TCP java服务器,代码如下:

主类

package com.codingdict;

  import java.io.*;
  import java.net.ServerSocket;
  import java.net.Socket;

  public class MyServer {

      public static void main(String[] args) {

          try {

              // 初始化服务端socket连接,并监听8888端口的socket请求
              ServerSocket serverSocket = new ServerSocket(8888);

              System.out.println("****** I am Server, now begin to wait the client ******");

              int count = 0;

              // 处理socket请求
              Socket socket = null;
              while (true) {

                  socket = serverSocket.accept();
                  ServerThread serverThread = new ServerThread(socket);
                  System.out.println("client host address is: " + socket.getInetAddress().getHostAddress());
                  serverThread.start();
                  count++;
                  System.out.println("now client count is: " + count);

              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }

线程类,每客户端一线程实现读写

package com.codingdict;

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.Socket;

  public class ServerThread extends Thread {
      private Socket socket;
      public ServerThread(Socket socket) {
          this.socket = socket;
      }

      @Override
      public void run() {

          InputStream inputStream = null;
          InputStreamReader inputStreamReader = null;
          BufferedReader bufferedReader = null;
          OutputStream outputStream = null;
          PrintWriter printWriter = null;

          try {

              // server接收消息
              inputStream = socket.getInputStream();
              inputStreamReader = new InputStreamReader(inputStream);
              bufferedReader = new BufferedReader(inputStreamReader);

              String str;
              if ((str = bufferedReader.readLine()) != null) {
                  System.out.println("I am Server, now get message from Client: " + str);
              }
              socket.shutdownInput();

              // server发送消息
              outputStream = socket.getOutputStream();
              printWriter = new PrintWriter(outputStream);
              printWriter.write("[welcome to you!]");
              printWriter.flush();

          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              // 关闭资源
              try {
                  if (printWriter != null) {
                      printWriter.close();
                  }
                  if (outputStream != null) {
                      outputStream.close();

                  }
                  if (bufferedReader != null) {
                      bufferedReader.close();
                  }
                  if (inputStreamReader != null) {
                      inputStreamReader.close();
                  }
                  if (inputStream != null) {
                      inputStream.close();
                  }
                  if (socket != null) {
                      socket.close();
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  • 修改jemter配置文件 jmeter.properties 发送信息为文本,也可以是16进制
tcp.handler=TCPClientImpl
  • 添加TCP取样器,设置ip和端口

注意: 输入的内容一定要有回车,否则不能进行