0

I am making a Java-based web server. But when I am testing it with ApacheBench, it sometimes stop responding.

On a Macbook Air:

ab -n 20000 -c 40 -d  http://localhost:1080/

is guaranteed to timeout after 16400 or more requests were done.

On Ubuntu desktop

ab -n 20000 -c 1000 -d  http://localhost:1080/

could done successfully most of the time, but sometimes stop responding after several runs.

I've identified (using Eclipse) that when the server stop responding, it is waiting for BufferedReader.readline() which I use it to read HTTP request header. But I have no idea why is it waiting.

Test code is here:

import java.io.BufferedReader;
import java.io.IOException;
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 TestServer {
    public static void main(String[] args){
        ServerSocket socket = null;

        try{
            socket = new ServerSocket(1080);
            ExecutorService pool = Executors.newFixedThreadPool(10);
            while(true){
                Socket s = socket.accept();
                pool.execute(new RequestHandler(s) );
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            if(null!=socket){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

class RequestHandler implements Runnable{
    final Socket s;
    public RequestHandler(Socket s) {
        this.s = s;
    }

    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line = br.readLine();

            PrintWriter pw = new PrintWriter(s.getOutputStream());
            pw.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n");
            pw.print(line);
            pw.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            if(s!=null){
                try {
                    s.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

BTW, when writing the test code, I found something else strange

If

BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = br.readLine();

is replaced with

String line = "don't read the socket";

ab will fail with such message: "apr_socket_recv: Connection refused (111)Connection reset by peer (104)"

But open localhost:1080 with Firefox 4 will see the "don't read the socket" mess show up.

4

1 回答 1

1

我想知道这是否是 ApacheBench 测试的故意部分:查看您的服务器在打开连接但没有发送数据时的行为。大概 ApacheBench 是开源的,所以你可以看看它是否在 16400 次尝试后调用了一些特殊行为(我的赌注是它打开套接字然后不发送请求)。

无论如何,您可能希望确保在套接字上设置显式超时,以防您的 Java 版本默认为 0 (=infinite)。不要假设每个客户都会表现完美,并且总是会准确地向您发送您期望的数据。

因此,作为一般规则,您需要确保您的 Web 服务器不会在“发生异常情况”时崩溃——网络就是这样,有时数据包/连接会随机丢弃,您需要处理它。操作系统可能会对例如连接可以打开多长时间施加限制,因此您的服务器可能会突然看到操作系统“从脚下拉扯地毯”。我想 ApacheBench 测试可能会模拟一些像这样的 gremlins(这甚至可能是您在 Ubuntu 中看到的,尽管 readLine() 挂起可能是模拟不在开放连接上发送请求,正如我提到的)。

于 2011-04-08T15:50:16.803 回答