网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  一个用java写的测算服务器响应速度的程序     
  文章作者:未知  文章来源:水木森林  
  查看:71次  录入:管理员--2007-11-17  
 


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);
}

 
 
上一篇: 最新java手机群聊软件msg横空出世    下一篇: java1.5中新的静态导入特性
  相关文档
应用rms实现用户自动登陆功能解析 11-17
jdk1.5使用总结 --《java 1.5 tiger: a developers notebook》 11-17
peer-to-peer sockets 工程入门 11-17
j2me的无线消息传递概述与应用程序示例 11-17
java socket编程(四) 重复和并发服务器 11-16
Lucene基础排序算法 03-21
java 方法 11-17
用spring aop实现开发中松散耦合 11-17
java连接mysql数据库! 11-17
java学习经验之谈 11-17
巧用java语言简单实现文件的分割与合并 11-16
轻量组件与重量组件的比较 11-17
ant的一些偏门技巧 11-17
xml和jsp 11-17
jbuilder5+weblogic 6.0安装配置全攻略 11-17
linux7.2+postgres7.2+resin2.0.4__jsp安装 11-17
学习java的笔记(一) 11-17
第一次接触ejb 11-17
jdo能给我们带来什么 11-17
用java的jwindow实现程序运行出的splash画面 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息