• (转载)获取服务器响应时间


    Java代码  收藏代码
    1. package com.speed;  
    2.   
    3. import java.io.BufferedReader;  
    4. import java.io.File;  
    5. import java.io.FileInputStream;  
    6. import java.io.FileNotFoundException;  
    7. import java.io.IOException;  
    8. import java.io.InputStreamReader;  
    9. import java.lang.reflect.Array;  
    10. import java.net.URL;  
    11.   
    12. public class TestServer {  
    13.     static int loopTimes = 500;  
    14.   
    15.     public Parameter readFromArgFile(String str) {  
    16.         FileInputStream fileInput;  
    17.         BufferedReader br;  
    18.         Parameter param = new Parameter();  
    19.         try {  
    20.             fileInput = new FileInputStream(new File(str));  
    21.             br = new BufferedReader(new InputStreamReader(fileInput));  
    22.   
    23.             String line;  
    24.             while ((line = br.readLine()) != null) {  
    25.                 if (line.startsWith("URL") == true && line.indexOf("=") >= 3) {  
    26.                     int f = line.indexOf("=");  
    27.                     String urlstring = line.substring(f + 1);  
    28.                     urlstring.trim();  
    29.                     param.url = new URL(urlstring);  
    30.                 } else if (line.startsWith("METHOD") == true  
    31.                         && line.indexOf("=") >= 3) {  
    32.                     int f = line.indexOf("=");  
    33.                     String method = line.substring(f + 1);  
    34.                     method.trim();  
    35.                     param.method = method;  
    36.                 } else if (line.indexOf("=") != -1) {  
    37.                     int f = line.indexOf("=");  
    38.                     String key = line.substring(0, f - 1);  
    39.                     String value = line.substring(f + 1);  
    40.                     param.addPair(key.trim(), value.trim());  
    41.                 }  
    42.             }  
    43.             fileInput.close();  
    44.             br.close();  
    45.         } catch (FileNotFoundException e) {  
    46.             System.out.println("File" + str + "not found.");  
    47.         } catch (NullPointerException e) {  
    48.   
    49.         } catch (IOException e) {  
    50.             System.out.println(e);  
    51.         }  
    52.         return param;  
    53.     }  
    54.   
    55.     public static void main(String[] args) {  
    56.         int i;  
    57.         int j;  
    58.         Parameter param;  
    59.         TestServer tester = new TestServer();  
    60.         for (i = 0; i < Array.getLength(args); i++) {  
    61.             param = tester.readFromArgFile(args[i]);  
    62.             for (j = 0; j < loopTimes; j++) {  
    63.                 Thread th = new Thread(new TestThread(param));  
    64.                 th.start();  
    65.             }  
    66.         }  
    67.     }  
    68. }  
    Java代码  收藏代码
    1. package com.speed;  
    2.   
    3. import java.lang.reflect.Array;  
    4. import java.net.URL;  
    5.   
    6. public class Parameter {  
    7.     URL url;  
    8.     String[] key;  
    9.     String[] value;  
    10.     String method;  
    11.     int length = 0;  
    12.   
    13.     public void addPair(String k, String v) {  
    14.         Array.set(key, length, k);  
    15.         Array.set(value, length, v);  
    16.         length++;  
    17.     }  
    18. }  
    Java代码  收藏代码
    1. package com.speed;  
    2.   
    3. import java.io.BufferedReader;  
    4. import java.io.InputStreamReader;  
    5. import java.net.HttpURLConnection;  
    6. import java.net.URL;  
    7. import java.util.Date;  
    8.   
    9. public class TestThread implements Runnable {  
    10.     Parameter param;  
    11.   
    12.     TestThread(Parameter par) {  
    13.         param = par;  
    14.     }  
    15.   
    16.     public void run() {  
    17.         long time1 = new Date().getTime();  
    18.         try {  
    19.             URL target = param.url;  
    20.             HttpURLConnection conn = (HttpURLConnection) target  
    21.                     .openConnection();  
    22.             conn.setRequestMethod(param.method);  
    23.             int i;  
    24.             for (i = 0; i < param.length; i++) {  
    25.                 conn.setRequestProperty(param.key[i], param.value[i]);  
    26.             }  
    27.             conn.connect();  
    28.             BufferedReader in = new BufferedReader(new InputStreamReader(conn  
    29.                     .getInputStream()));  
    30.             String inputLine;  
    31.             while ((inputLine = in.readLine()) != null);  
    32.         } catch (Exception e) {  
    33.   
    34.         }  
    35.         long time2 = new Date().getTime();  
    36.         System.out.println((time2 - time1)/1000+"秒");  
    37.     }  
    38.   
    39. }  

    由于这个是在命令行模式下运行的,我的附件是这样写的

    Xml代码  收藏代码
    1. URL=http://mail.163.com  
    2. METHOD=GET  
    3. User-Agent=Internet Explorer  
    4. Host=mail.163.com  
    5. Accept=image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*  
    6. Accept-Language=zh-cn  
    7. Content-Type=application/x-www-form-urlencoded  
    8. Accept-Encoding=gzip, deflate  
    9. User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727;MEGAUPLOAD 1.0)  
    10. Connection=Keep-Alive  
    11. Cache-Control=no-cache  
  • 相关阅读:
    今日总结
    今日总结
    今日总结
    今日总结
    今日总结
    java自学
    java自学
    Java自学
    Java自学
    java自学
  • 原文地址:https://www.cnblogs.com/gaoxufei/p/6370879.html
Copyright © 2020-2023  润新知