对tomcat7模拟并发请求及相关配置参数的含义

 这里的并不是真正的并发请求,因为for循环是间隔10毫秒,并且线程初始化也需要时间的,到真正执行http请求的时刻是不确定的。

 tomcat 的运行状态可以在webapps下的manage项目查看,可以看到tomcat的运行模式,当前线程总数,占用内存大小等。 

1  tomcat版本

2 tomcat配置文件server.xml设置

3 各参数的含义

  acceptCount 等待队列的长度 , maxConnections 最大连接的长度,maxThreads 可以使用的最大线程数量

4 tomcat7运行模式

     默认是BIO模式

 5  服务端接收请求的方法

    @RequestMapping(value = { "/testHttp" })
    public void testHttpConcurrentRequest(HttpServletRequest req,HttpServletResponse resp){
        String current = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss").format(LocalDateTime.now());
        System.out.println(current + " 有客户端连接,参数:"+req.getParameter("name"));
        try {
            //保持连接
            Thread.sleep(30*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out("server response:"+req.getParameter("name"), resp);
    }

6  客户端发送多线程请求

public class HttpClient {
    
    public final static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    
    
    public static void main(String[] args) throws Exception {
        HttpUtils helper = new HttpUtils();
        for (int i = 0; i < 800; i++) { //根据测试不同更改数量
            ExcuteHttpRequest ehr = new ExcuteHttpRequest(i + 1, helper);
            cachedThreadPool.execute(ehr);
            Thread.sleep(10);
        }
        Thread.sleep(600 * 1000);
 
    }
}
class ExcuteHttpRequest implements Runnable {
 
    private int clid;
    private HttpUtils helper;
 
    public ExcuteHttpRequest(int clid, HttpUtils helper) {
        this.clid = clid;
        this.helper = helper;
    }
 
    @Override
    public void run() {
        String current = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss").format(LocalDateTime.now());
        System.out.println(current + "  " + this.clid + "开始连接..");
        try {
            String responseText = this.helper.sendGet("http://127.0.0.1:25080/abc/testHttp?name="+ this.clid);
            System.out.println(current + "  "  + this.clid + "接受到的数据:" + responseText);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(current + "  "  + this.clid +"连接出错:"+e.getMessage());
        }
 
    }
}

7测试结果

由于在服务端里设置了线程sleep30秒,而且maxConnections为200,acceptCount为100,所以在服务端接收了400个请求后,就拒接接收请求了。

由于maxThread为150,服务端同时能处理的请求是由maxThread决定的。所以在13分33秒时,正在处理请求是150个,每个请求是30秒完成,所以等到14分02秒时,又可以处理接下来的150个请求了,再等30秒处理最后的100个请求。

 从客户端收到的返回值可以看出,只要请求发送到服务端了,在不超时的情况下就可以收到响应,由于服务端共接收到了400个请求,所以客户端接收到了400个响应。

转载自:https://blog.csdn.net/kaka20099527/article/details/53285348/

猜你喜欢

转载自www.cnblogs.com/moris5013/p/10629324.html