<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      一個用JAVA寫的測算服務器響應速度的程序

      [摘要]1. 任務描述 需要做一個程序,對某一服務器運行的web server進行測算,看對提出的request做出相應的時間,并且在多個request同時提出時的響應時間。 2. 計劃 因為java sd...
      1. 任務描述
      需要做一個程序,對某一服務器運行的web server進行測算,看對提出的request做出相應的時間,并且在多個request同時提出時的響應時間。

      2. 計劃
      因為java sdk中包含有比較全面的class能夠對http等多種協議的處理方法進行了封裝,用起來比較方便,能夠在比較短的時間內快速開發出這一測算工具。

      需要2個功能:
      a. 因為不是僅僅對一個web server或者一個form進行測算,所以需要程序能夠靈活處理,完成各種工作。我采用了配置文件的形式,讓程序從配置文件中讀取數據,并作相應動作。
      b.需要采用多線程方式,對同一個web server提交多次request.

      3.開發過程
      (讀者可以跟隨這一過程,自己動手寫代碼,到全文結束,就能有一個完整可用的程序了)
      主要的工作都有TestThread來完成。代碼如下:
      class TestThread implements Runnable {
      Parameter param;
      TestThread(Parameter par) {
      param = par;
      }
      public void run() {
      long time1 = new Date().getTime();
      try {
      URL target = param.url;
      HttpURLConnection conn = (HttpURLConnection) target.openConnection();
      conn.setRequestMethod(param.method);
      int i;
      for( i = 0; i < param.length; i++ ) {
      conn.setRequestProperty(param.key[i], param.value[i]);
      }
      conn.connect();
      BufferedReader in = new BufferedReader(
      new InputStreamReader(conn.getInputStream()));
      String inputLine;
      while( (inputLine = in.readLine()) != null );
      }
      catch(Exception e) {
      }
      long time2 = new Date().getTime();
      System.out.println(time2 - time1);
      }
      }

      class TestThread implements Runnable, 而不是用extends Thread, 的好處是獨立設計一個類,這個類還可以extends其它的class, 而不是單獨的extends Thread. 另外一個好處是,可以把處理方法放在各個不同的方法中,然后在void run()中調用,程序結構比較清晰。
      程序工作如下:
      在初始化一個TestThread實例的時候,接受一個Parameter參數(稍候介紹),并在線程啟動時,計算開始的時間,向目標機器發送請求包,接受目標機器的返回結果,再次計算時間,并得到兩次時間之差,這就是服務器的響應時間。
      具體程序可以自己看懂,就不多說了。
      class Parameter {
      URL url;
      String[] key;
      String[] value;
      String method;
      int length = 0;
      public void addPair(String k, String v) {
      Array.set(key, length, k);
      Array.set(value, length, v);
      length++;
      }
      }
      是用來傳遞參數的一個類。參數是主程序從文件中讀出來并存入這個類的一個對象里,然后通過初始化TestThread傳遞給它的對象。
      public class TestServer {
      static int loopTimes = 500;
      public Parameter readFromArgFile(String str){
      FileInputStream fileInput;
      BufferedReader br;
      Parameter param = new Parameter();
      try {
      fileInput = new FileInputStream(new File(str));
      br = new BufferedReader(
      new InputStreamReader( fileInput ));

      String line;
      while( (line = br.readLine()) != null ) {
      if( line.startsWith("URL") == true && line.indexOf("=") >= 3) {
      int f = line.indexOf("=");
      String urlstring = line.substring(f+1);
      urlstring.trim();
      param.url = new URL(urlstring);
      }
      else if( line.startsWith("METHOD") == true && line.indexOf("=") >= 3) {
      int f = line.indexOf("=");
      String method = line.substring(f+1);
      method.trim();
      param.method = method;
      }
      else if( line.indexOf("=") != -1 ) {
      int f = line.indexOf("=");
      String key = line.substring(0, f-1);
      String value = line.substring(f+1);
      param.addPair(key.trim(), value.trim());
      }
      }
      fileInput.close();
      br.close();
      }
      catch(FileNotFoundException e) {
      System.out.println("File " + str + " not found.");
      }
      catch(NullPointerException e) {
      }
      catch(IOException e) {
      System.out.println(e);
      }
      return param;
      }
      public static void main(String[] args) {
      int i;
      int j;
      Parameter param;
      TestServer tester = new TestServer();
      for(i = 0; i < Array.getLength(args); i++) {
      param = tester.readFromArgFile(args[i]);
      for(j = 0; j < loopTimes; j++) {
      Thread th = new Thread(new TestThread(param));
      th.start();
      }
      }
      }
      }

      主程序main也比較簡單,從命令行參數中讀取文件名,并依次打開,讀取其中的配置參數,創建Parameter對象,并傳遞給TestThread對象,然后啟動TestThread線程。需要注意的是其中的錯誤處理,當發現某個文件讀寫錯誤的時候,是跳過這個文件而讀取下一個文件,而不是簡單的退出。

      就這么簡單。(當然,適當的改寫一下,就可以做一個加貼機或者灌水機之類的東東,那是你的愛好,和我無關:-))
      程序全文列在最后,并附上了說明
      -------------------------------------------------------------------------------
      /****************************************************************
      Program: TestServer.java
      Description: send requests in multiple threads to server to test
      its responses delayance
      Author: ariesram <ariesram@may10.ca>
      Date: Aug 23, 2001
      Usage: java TestServer file1 file2 ...
      file format:
      URL=[Full URL of form]
      METHOD=GET POST ...
      key1=value1
      key2=value2
      and so on ...
      ****************************************************************/
      import java.io.*;
      import java.lang.reflect.Array;
      import java.net.*;
      import java.util.*;

      public class TestServer {
      static int loopTimes = 500;
      public Parameter readFromArgFile(String str){
      FileInputStream fileInput;
      BufferedReader br;
      Parameter param = new Parameter();
      try {
      fileInput = new FileInputStream(new File(str));
      br = new BufferedReader(
      new InputStreamReader( fileInput ));

      String line;
      while( (line = br.readLine()) != null ) {
      if( line.startsWith("URL") == true && line.indexOf("=") >= 3) {
      int f = line.indexOf("=");
      String urlstring = line.substring(f+1);
      urlstring.trim();
      param.url = new URL(urlstring);
      }
      else if( line.startsWith("METHOD") == true && line.indexOf("=") >= 3) {
      int f = line.indexOf("=");
      String method = line.substring(f+1);
      method.trim();
      param.method = method;
      }
      else if( line.indexOf("=") != -1 ) {
      int f = line.indexOf("=");
      String key = line.substring(0, f-1);
      String value = line.substring(f+1);
      param.addPair(key.trim(), value.trim());
      }
      }
      fileInput.close();
      br.close();
      }
      catch(FileNotFoundException e) {
      System.out.println("File " + str + " not found.");
      }
      catch(NullPointerException e) {
      }
      catch(IOException e) {
      System.out.println(e);
      }
      return param;
      }
      public static void main(String[] args) {
      int i;
      int j;
      Parameter param;
      TestServer tester = new TestServer();
      for(i = 0; i < Array.getLength(args); i++) {
      param = tester.readFromArgFile(args[i]);
      for(j = 0; j < loopTimes; j++) {
      Thread th = new Thread(new TestThread(param));
      th.start();
      }
      }
      }
      }
      class Parameter {
      URL url;
      String[] key;
      String[] value;
      String method;
      int length = 0;
      public void addPair(String k, String v) {
      Array.set(key, length, k);
      Array.set(value, length, v);
      length++;
      }
      }
      class TestThread implements Runnable {
      Parameter param;
      TestThread(Parameter par) {
      param = par;
      }
      public void run() {
      long time1 = new Date().getTime();
      try {
      URL target = param.url;
      HttpURLConnection conn = (HttpURLConnection) target.openConnection();
      conn.setRequestMethod(param.method);
      int i;
      for( i = 0; i < param.length; i++ ) {
      conn.setRequestProperty(param.key[i], param.value[i]);
      }
      conn.connect();
      BufferedReader in = new BufferedReader(
      new InputStreamReader(conn.getInputStream()));
      String inputLine;
      while( (inputLine = in.readLine()) != null );
      }
      catch(Exception e) {
      }
      long time2 = new Date().getTime();
      System.out.println(time2 - time1);
      }



      日韩精品一区二区三区高清