第1页共12页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第1页共12页1.任务描述需要做一个程序,对某一服务器运行的webserver进行测算,看对提出的request做出相应的时间,并且在多个request同时提出时的响应时间。2.计划因为javasdk中包含有比较全面的class能够对http等多种协议的处理方法进行了封装,用起来比较方便,能够在比较短的时间内快速开发出这一测算工具。需要2个功能:a.因为不是仅仅对一个webserver或者一个form进行测算,所以需要程序能够灵活处理,完成各种工作。我采用了配置文件的形式,让程序从配置文件中读取数据,并作相应动作。b.需要采用多线程方式,对同一个webserver提交多次request.3.开发过程(读者可以跟随这一过程,自己动手写代码,到全文结束,就能有一个完整可用的程序了)主要的工作都有TestThread来完成。代码如下:classTestThreadimplementsRunnable{Parameterparam;TestThread(Parameterpar){param=par;}publicvoidrun(){longtime1=newDate().getTime();try{URLtarget=param.url;第2页共12页第1页共12页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第2页共12页HttpURLConnectionconn=(HttpURLConnection)target.openConnection();conn.setRequestMethod(param.method);inti;for(i=0;i<param.length;i++){conn.setRequestProperty(param.key[i],param.value[i]);}conn.connect();BufferedReaderin=newBufferedReader(newInputStreamReader(conn.getInputStream()));StringinputLine;while((inputLine=in.readLine())!=null);}catch(Exceptione){}longtime2=newDate().getTime();System.out.println(time2-time1);}}classTestThreadimplementsRunnable,而不是用extendsThread,的好处是独立设计一个类,这个类还可以extends其它的class,而不是单独的extendsThread.另外一个好处是,可以把处理方法放在各个不同的方法中,然后在voidrun()中调用,程序结构比较清晰。程序工作如下:在初始化一个TestThread实例的时候,接受一个Parameter参数(稍候介绍),并在线第3页共12页第2页共12页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第3页共12页程启动时,计算开始的时间,向目标机器发送请求包,接受目标机器的返回结果,再次计算时间,并得到两次时间之差,这就是服务器的响应时间。具体程序可以自己看懂,就不多说了。classParameter{URLurl;String[]key;String[]value;Stringmethod;intlength=0;publicvoidaddPair(Stringk,Stringv){Array.set(key,length,k);Array.set(value,length,v);length++;}}是用来传递参数的一个类。参数是主程序从文件中读出来并存入这个类的一个对象里然后通过初始化TestThread传递给它的对象。publicclassTestServer{staticintloopTimes=500;publicParameterreadFromArgFile(Stringstr){FileInputStreamfileInput;BufferedReaderbr;Parameterparam=newParameter();第4页共12页第3页共12页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第4页共12页try{fileInput=newFileInputStream(newFile(str));br=newBufferedReader(newInputStreamReader(fileInput));Stringline;while((line=br.readLine())!=null){if(line.startsWith("URL")==true&&line.indexOf("=")>=3){intf=line.indexOf("=");Stringurlstring=line.substring(f+1);urlstring.trim();param.url=newURL(urlstring);}elseif(line.startsWith("METHOD")==true&&line.indexOf("=")>=3){intf=line.indexOf("=");Stringmethod=line.substring(f+1);method.trim();param.method=method;}elseif(line.indexOf("=")!=-1){intf=line.indexOf("=");Stringkey=line.substring(0,f-1);第5页共12页第4页共12页编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第5页共12页Stringvalue=line.substring(f+1);param.addPair(key.trim(),value.trim());}}fileInput.close();br.close();}catch(FileNotFoundExceptione){System.out.println("File"+str+"notfound.");}catch(NullPointe...