zoukankan      html  css  js  c++  java
  • java小程序检测web的并发数---HttpClient和util包的concurrent

    1.下载org.apache.commons.httpclient.jar文件。

    2.利用HttpClient访问web网站(url)。

    3.利用多线程测试并发数。java.util.concurrent包实现并发。

    代码如下:

     1 import java.io.IOException;
     2 import java.util.concurrent.ExecutorService;
     3 import java.util.concurrent.Executors;
     4 import java.util.concurrent.TimeUnit;
     5 
     6 import org.apache.commons.httpclient.HttpClient;
     7 import org.apache.commons.httpclient.HttpException;
     8 import org.apache.commons.httpclient.HttpMethod;
     9 import org.apache.commons.httpclient.methods.GetMethod;
    10 
    11 
    12 
    13 
    14 public class Ceshi {
    15 
    16     /**
    17      * @param args
    18      * @throws IOException 
    19      * @throws HttpException 
    20      * @throws InterruptedException 
    21      */
    22     public static void main(String[] args) throws HttpException, IOException, InterruptedException {
    23         ExecutorService service=Executors.newFixedThreadPool(Integer.MAX_VALUE);
    24         int i = 0;
    25         for ( i= 0; i < 4000; i++) {
    26             System.out.println("number " + (i+1) + " starts");
    27             service.execute(new Runnable() {
    28                 @Override
    29                 public void run() {
    30                     try {
    31                         ceshi();
    32                     } catch (HttpException e) {
    33                         System.out.println("HttpException");
    34                         e.printStackTrace();
    35                     } catch (IOException e) {
    36                         System.out.println("IOException");
    37                         e.printStackTrace();
    38                     }
    39                 }
    40             });
    41             System.out.println("number " + (i+1) + " ends");
    42         }
    43          
    44         service.shutdown();
    45         
    46          
    47         service.awaitTermination(300,TimeUnit.SECONDS);
    48         
    49         System.out.println("ok");
    50 
    51     }
    52     
    53     
    54     private static void ceshi() throws HttpException, IOException{
    55         HttpClient client = new HttpClient();
    56 
    57         client.getHostConfiguration().setHost("9.186.62.58",8080,"http");
    58 
    59         HttpMethod method = getGetMethod();//使用POST方式提交数据
    60 
    61         client.executeMethod(method);
    62 
    63         //打印服务器返回的状态
    64 
    65         System.out.println(method.getStatusLine());
    66 
    67         //打印结果页面
    68 
    69         String response = new String(method.getResponseBodyAsString().getBytes("GB2312"));
    70 
    71         //打印返回的信息
    72 
    73         System.out.println(response);
    74 
    75         method.releaseConnection(); 
    76     }
    77     
    78     private static HttpMethod getGetMethod(){
    79 
    80         return new GetMethod("/BiMaiApp/airdetailpage?cityIDs=1");
    81 
    82         } 
    83 
    84 }
    View Code

     在压力测试的环境下拥有大量的线程,当本地内存耗尽时,企图创建新的线程时抛出java.lang.OutOfMemoryError: unable to create new native thread异常。

    系统创建的线程数的计算公式:

    (MaxProcessMemory - JVMMemory - ReservedOsMemory) / (ThreadStackSize) = Number of threads 
    MaxProcessMemory 指的是一个进程的最大内存
    JVMMemory         JVM内存
    ReservedOsMemory  保留的操作系统内存
    ThreadStackSize      线程栈的大小

    针对无法创建更多本地线程的情况,调整线程栈的大小,添加-Xss选项(线程栈的大小),设置为256k后再跑自动化,发现问题解决。
     JAVA_OPTS="-Xms2048M -Xmx2048M -Xmn512M -Xss256k -XX:PermSize=512M….”

    对于一般的内存泄漏导致的堆栈溢出,通常的错误信息主要有以下几种。
    1. java.lang.OutOfMemoryError: Java heap space
    2. java.lang.OutOfMemoryError: PermGen space
    3. java.lang.OutOfMemoryError: Requested array size exceeds VM limit
    4. java.lang.OutOfMemoryError: <reason> <stack trace> (Native method)

    参考:http://bbs.csdn.net/topics/350248404

  • 相关阅读:
    2017博普杯 东北大学邀请赛(B. Drink too much water)(贪心+树链剖分)
    AGC018D Tree and Hamilton Path(树+树的重心)
    BZOJ2843:极地旅行社
    P++ 1.0.5
    BZOJ1052:[HAOI2007]覆盖问题
    BZOJ3098:Hash Killer II
    BZOJ2784:[JLOI2012]时间流逝
    BZOJ2282:[SDOI2011]消防
    BZOJ1875:[SDOI2009]HH去散步
    Codeforces 504 A (Round #285 div.1 A) Misha and Forest
  • 原文地址:https://www.cnblogs.com/dobestself-994395/p/4587571.html
Copyright © 2011-2022 走看看