一個用JAVA寫的測算服務(wù)器響應(yīng)速度程序_第1頁
一個用JAVA寫的測算服務(wù)器響應(yīng)速度程序_第2頁
一個用JAVA寫的測算服務(wù)器響應(yīng)速度程序_第3頁
一個用JAVA寫的測算服務(wù)器響應(yīng)速度程序_第4頁
一個用JAVA寫的測算服務(wù)器響應(yīng)速度程序_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、 1. 任務(wù)描述 需要做一個程序,對某一服務(wù)器運行的web server進行測算,看對提出的request做出相應(yīng)的時間,并且在多個request同時提出時的響應(yīng)時間。 2. 計劃 因為java sdk中包含有比較全面的class能夠?qū)ttp等多種協(xié)議的處理方法進行了封裝,用起來比較方便,能夠在比較短的時間內(nèi)快速開發(fā)出這一測算工具。 需要2個功能: a. 因為不是僅僅對一個web server或者一個form進行測算,所以需要程序能夠靈活處理,完成各種工作。我采用了配置文件的形式,讓程序從配置文件中讀取數(shù)據(jù),并作相應(yīng)動作。 b.需要采用多線程方式,對同一個web server提交多次requ

2、est. 3.開發(fā)過程 (讀者可以跟隨這一過程,自己動手寫代碼,到全文結(jié)束,就能有一個完整可用的程序了) 主要的工作都有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) t

3、arget.openConnection(); conn.setRequestMethod(param.method); int i; for( i = 0; i param.length; i+ ) conn.setRequestProperty(param.keyi, param.valuei); conn.connect(); BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream(); String inputLine; while( (inputLine = in.readLi

4、ne() != null ); catch(Exception e) long time2 = new Date().getTime(); System.out.println(time2 - time1); class TestThread implements Runnable, 而不是用extends Thread, 的好處是獨立設(shè)計一個類,這個類還可以extends其它的class, 而不是單獨的extends Thread. 另外一個好處是,可以把處理方法放在各個不同的方法中,然后在void run()中調(diào)用,程序結(jié)構(gòu)比較清晰。 程序工作如下: 在初始化一個TestThread實例的

5、時候,接受一個Parameter參數(shù)(稍候介紹),并在線程啟動時,計算開始的時間,向目標(biāo)機器發(fā)送請求包,接受目標(biāo)機器的返回結(jié)果,再次計算時間,并得到兩次時間之差,這就是服務(wù)器的響應(yīng)時間。 具體程序可以自己看懂,就不多說了。 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+; 是

6、用來傳遞參數(shù)的一個類。參數(shù)是主程序從文件中讀出來并存入這個類的一個對象里,然后通過初始化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

7、 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(

8、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

9、.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 Test

10、Server(); for(i = 0; i Array.getLength(args); i+) param = tester.readFromArgFile(argsi); for(j = 0; j loopTimes; j+) Thread th = new Thread(new TestThread(param); th.start(); 主程序main也比較簡單,從命令行參數(shù)中讀取文件名,并依次打開,讀取其中的配置參數(shù),創(chuàng)建Parameter對象,并傳遞給TestThread對象,然后啟動TestThread線程。需要注意的是其中的錯誤處理,當(dāng)發(fā)現(xiàn)某個文件讀寫錯誤的時候,是跳過這個文

11、件而讀取下一個文件,而不是簡單的退出。 就這么簡單。(當(dāng)然,適當(dāng)?shù)母膶懸幌?,就可以做一個加貼機或者灌水機之類的東東,那是你的愛好,和我無關(guān):-) 程序全文列在最后,并附上了說明 - /* Program: TestServer.java Description: send requests in multiple threads to server to test its responses delayance Author: ariesram Date: Aug 23, 2001 Usage: java TestServer file1 file2 . file format: URL=Fu

12、ll URL of form METHOD=GETPOST. key1=value1 key2=value2 and so on . */ import java.io.*; import java.lang.reflect.Array; import .*; import java.util.*; public class TestServer static int loopTimes = 500; public Parameter readFromArgFile(String str) FileInputStream fileInput; BufferedReader br; Parame

13、ter 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.sub

14、string(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 = lin

15、e.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 s

16、tatic 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(argsi); for(j = 0; j loopTimes; j+) Thread th = new Thread(new TestThread(param); th.start(); class Parameter URL url; String key;

17、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(p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論