一个用JAVA写的测算服务器响应速度程序.doc_第1页
一个用JAVA写的测算服务器响应速度程序.doc_第2页
一个用JAVA写的测算服务器响应速度程序.doc_第3页
一个用JAVA写的测算服务器响应速度程序.doc_第4页
一个用JAVA写的测算服务器响应速度程序.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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.keyi, param.valuei); 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(argsi); 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 Date: Aug 23, 2001 Usage: java TestServer file1 file2 . file format: URL=Full 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; 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(argsi); 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(p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论