简体   繁体   中英

java http server implementation and JMEter testing

the control is reaching the put,get,post requests but i am unable to get the reply back from server, the http://localhost:8080 is throwing invalid ip error and jmeter test case is showing error status

I have configured a server as below: public class HttpServer {

private static final int MAX_THREADS = 64;

private static final Map<Integer, AtomicInteger> threadCounts = new ConcurrentHashMap<>();

private static final ExecutorService threadPool = Executors.newFixedThreadPool(MAX_THREADS);



public static void main(String[] args) {



    ArrayList<Integer> portNodes;

    ArrayList<Integer> timeout;



    XMLFileHandler xmlFileHandler = new XMLFileHandler();

    xmlFileHandler.readFromXML("config.xml");

    portNodes = xmlFileHandler.getPortNodes();

    timeout = xmlFileHandler.getTimeout();



    for (int i = 0; i < portNodes.size(); i++) {

        int port = portNodes.get(i);

        int timeouts = timeout.get(i);

                  if (!threadCounts.containsKey(port)) {

                             threadCounts.put(port, new AtomicInteger(0));

                  }         

                  try {

                             ServerSocket serverSocket = new ServerSocket(port);

                             serverSocket.setSoTimeout(timeouts);

                             threadPool.submit(() -> {

                                        while (true) {

                                                   try {

                                                              Socket socket = serverSocket.accept();

                                                              OutputStream os = socket.getOutputStream();

                                                              os.write("welcome to server".getBytes());

                                                              System.out.println("connected successfully" + port);

                                                              threadCounts.get(port).incrementAndGet();

                                                              threadPool.submit(new RequestHandler(socket));

                                                   }

                                                   catch (SocketTimeoutException e) {

                                                              System.out.println("Timeout occurred on port: " + port);

                                                   }

                                                   catch (IOException e) {

                                                              e.printStackTrace();

                                                   }

                                        }

                             });

                             }

                  catch (BindException e) {

                System.out.println("Port " + port + " is already in use. Please choose a different port.");

            }catch(IOException e) {

                  System.out.println("erro"+port);

            }

    }

}

private static class RequestHandler implements Runnable {

private final Socket socket;



public RequestHandler(Socket socket) {

    this.socket = socket;

}



public void run() {

    try {

       System.out.println("in request handler");

        // Handle HTTP request

        InputStream input = socket.getInputStream();

        OutputStream output = socket.getOutputStream();



        // Parse the request

        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        String[] requestLine = reader.readLine().split(" ");

        String method = requestLine[0];

        String url = requestLine[1];

        String httpVersion = requestLine[2];



        // Handle GET request

                        if (method.equals("GET")) {

            //TODO: Implement handling of GET request

        }

        // Handle POST request

        else if (method.equals("POST")) {

            //TODO: Implement handling of POST request

        }

        // Handle PUT request

        else if (method.equals("PUT")) {

            //TODO: Implement handling of PUT request

        }

        else {

            // Send error message for unsupported method

            output.write("HTTP/1.1 400 Bad Request\r\n\r\n".getBytes());

        }



        // Close socket and release resources

        input.close();

        output.close();

        socket.close();

        threadCounts.get(socket.getPort()).decrementAndGet();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

}

}

Your "http server" doesn't implement HTTP protocol hence you won't be able to use HTTP Request sampler for conducting the load.

Consider moving either to TCP Sampler or HTTP Raw Request sampler (can be installed using JMeter Plugins Manager )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM