测试服务器响应时间

所谓的响应时间,就是从客户端开始发出请求到服务器最后返回请求时所消耗的时间,见代码

package com.speed;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.URL;

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

 

package com.speed;

import java.lang.reflect.Array;
import java.net.URL;

public 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++;
	}
}

 

package com.speed;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public 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)/1000+"秒");
	}

}

 

由于这个是在命令行模式下运行的,我的附件是这样写的

URL=http://mail.163.com
METHOD=GET
User-Agent=Internet Explorer
Host=mail.163.com
Accept=image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*
Accept-Language=zh-cn
Content-Type=application/x-www-form-urlencoded
Accept-Encoding=gzip, deflate
User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727;MEGAUPLOAD 1.0)
Connection=Keep-Alive
Cache-Control=no-cache

 

我们来测试一下163的邮件服务器,最后响应时间为14秒,可是单独打开网页的话马上就出现页面了,难道是ajax的过吗?提前展现页面?如果真让用户等待14秒,那还不得疯了,这是个问题,有知道这是为什么的朋友,给我留言告诉我哦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值