1. 任務(wù)描述 需要做一個(gè)程序,對(duì)某一服務(wù)器運(yùn)行的web server進(jìn)行測(cè)算,看對(duì)提出的request做出相應(yīng)的時(shí)間,并且在多個(gè)request同時(shí)提出時(shí)的響應(yīng)時(shí)間。
2. 計(jì)劃 因?yàn)閖ava sdk中包含有比較全面的class能夠?qū)ttp等多種協(xié)議的處理方法進(jìn)行了封裝,用起來(lái)比較方便,能夠在比較短的時(shí)間內(nèi)快速開(kāi)發(fā)出這一測(cè)算工具。
需要2個(gè)功能: a. 因?yàn)椴皇莾H僅對(duì)一個(gè)web server或者一個(gè)form進(jìn)行測(cè)算,所以需要程序能夠靈活處理,完成各種工作。我采用了配置文件的形式,讓程序從配置文件中讀取數(shù)據(jù),并作相應(yīng)動(dòng)作。 b.需要采用多線(xiàn)程方式,對(duì)同一個(gè)web server提交多次request.
3.開(kāi)發(fā)過(guò)程 (讀者可以跟隨這一過(guò)程,自己動(dòng)手寫(xiě)代碼,到全文結(jié)束,就能有一個(gè)完整可用的程序了) 主要的工作都有TestThread來(lái)完成。代碼如下: 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, 的好處是獨(dú)立設(shè)計(jì)一個(gè)類(lèi),這個(gè)類(lèi)還可以extends其它的class, 而不是單獨(dú)的extends Thread. 另外一個(gè)好處是,可以把處理方法放在各個(gè)不同的方法中,然后在void run()中調(diào)用,程序結(jié)構(gòu)比較清晰。 程序工作如下: 在初始化一個(gè)TestThread實(shí)例的時(shí)候,接受一個(gè)Parameter參數(shù)(稍候介紹),并在線(xiàn)程啟動(dòng)時(shí),計(jì)算開(kāi)始的時(shí)間,向目標(biāo)機(jī)器發(fā)送請(qǐng)求包,接受目標(biāo)機(jī)器的返回結(jié)果,再次計(jì)算時(shí)間,并得到兩次時(shí)間之差,這就是服務(wù)器的響應(yīng)時(shí)間。 具體程序可以自己看懂,就不多說(shuō)了。 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++; } } 是用來(lái)傳遞參數(shù)的一個(gè)類(lèi)。參數(shù)是主程序從文件中讀出來(lái)并存入這個(gè)類(lèi)的一個(gè)對(duì)象里,然后通過(guò)初始化TestThread傳遞給它的對(duì)象。 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也比較簡(jiǎn)單,從命令行參數(shù)中讀取文件名,并依次打開(kāi),讀取其中的配置參數(shù),創(chuàng)建Parameter對(duì)象,并傳遞給TestThread對(duì)象,然后啟動(dòng)TestThread線(xiàn)程。需要注意的是其中的錯(cuò)誤處理,當(dāng)發(fā)現(xiàn)某個(gè)文件讀寫(xiě)錯(cuò)誤的時(shí)候,是跳過(guò)這個(gè)文件而讀取下一個(gè)文件,而不是簡(jiǎn)單的退出。
就這么簡(jiǎn)單。(當(dāng)然,適當(dāng)?shù)母膶?xiě)一下,就可以做一個(gè)加貼機(jī)或者灌水機(jī)之類(lèi)的東東,那是你的愛(ài)好,和我無(wú)關(guān):-)) 程序全文列在最后,并附上了說(shuō)明 ------------------------------------------------------------------------------- /**************************************************************** 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); } }
|