jsp 页面响应时间计算

1.java代码实现

   

public class LoadTime {

	public static void main(String[] args) {
		long time1 = System.currentTimeMillis();
		String urls = "http://www.iteye.com/";
		int i = 0;
		try {
			URL target = new URL(urls);

			HttpURLConnection conn = (HttpURLConnection) target
					.openConnection();
			conn.setRequestMethod("GET");

			conn.connect();

			BufferedReader in = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				i++;
			}

		} catch (Exception e) {

		}
		long time2 = new Date().getTime();
		System.out.println((time2 - time1) / 1000 + "秒" + i+"行");
	}
}

 

 

2. js 实现

 

 在页面Head部分加入初始时间

<script language=javascript> 
var t1 = new Date().getTime(); 
</script> 

 

 

在Body中加入

<SCRIPT LANGUAGE="JavaScript"> 
window.onload = function() 
{ 
document.getElementById("TimeShow").innerHTML = "加载本页耗时 "+ (new Date().getTime()-t1) +" 毫秒"; 
} 
</SCRIPT> 

 

在需要显示的地方插入一个ID为TimeShow的标签即可
<div id="TimeShow"></div>

 

服务器代码写法:

protected DateTime dt; 
protected void Page_Load(object sender, EventArgs e) 
{ 
} 
protected override void OnPreInit(EventArgs e) 
{ 
dt = DateTime.Now; 
base.OnPreInit(e); 
} 
protected override void OnLoadComplete(EventArgs e) 
{ 
base.OnLoadComplete(e); 
TimeSpan ts = DateTime.Now - dt; 
Response.Write("加载页面用时:" + ts.TotalMilliseconds); 
} 

 

你可能感兴趣的:(jsp)