jmeter tcpclient classname_文本自定义

(5) 2024-06-15 20:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
jmeter tcpclient classname_文本自定义,希望能够帮助你!!!。

JMeter提供纯TCP协议级别Sampler,如果你不觉得麻烦可以通过它来完成所有基于TCP协议的性能测试,这样一个万金油式的Sampler由于很少进入使用者的视野,因此,在实际使用中存在着许多隐藏特性和误解,本篇将对TCP Sampler使用中的一些特点进行讲解。

Mock TCP Server

为了更好的理解TCP Sampler的使用,我们将构建一个Mock TCP Server用于测试,并根据不同的TCP Sampler类型和设置进行调整,以达到直观易懂的目的,一个典型的TCP Server代码参考如下:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TCPServer { 
    private int port = 10009; private ServerSocket serverSocket; private ExecutorService executorService; private final int POOL_SIZE = 20; public TCPServer() throws IOException { 
    serverSocket = new ServerSocket(port); executorService = Executors.newFixedThreadPool(Runtime.getRuntime() .availableProcessors() * POOL_SIZE); } public void service() { 
    while (true) { 
    Socket socket = null; try { 
    socket = serverSocket.accept(); executorService.execute(new Handler(socket)); } catch (Exception e) { 
    e.printStackTrace(); } } } public static void main(String[] args) throws IOException { 
    new TCPServer().service(); } } class Handler implements Runnable { 
    private Socket socket = null; public Handler(Socket socket) { 
    this.socket = socket; } private BufferedReader getReader(Socket socket) throws IOException { 
    InputStream in = socket.getInputStream(); return new BufferedReader(new InputStreamReader(in)); } public void run() { 
    BufferedReader br = null; PrintWriter out = null; System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort()); try { 
    br = getReader(socket); String recv = null; recv = br.readLine(); System.out.println(recv); out = new PrintWriter(socket.getOutputStream()); out.write("Hello Client\r\n"); out.flush(); } catch (IOException e) { 
    e.printStackTrace(); } finally { 
    try { 
    if (br != null) { 
    br.close(); } if (out != null) { 
    out.close(); } 

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复