-
压力测试原理有两种,一种是连接测试,一种是负载测试
1,连接测试,就是多线程,不停的请求直到服务器死机或达到预期效果即可!
2,负载测试,就是一个线程的多步操作,每个线程占用服务器cpu 内存是否符合节能高效的标准,如果不是,那就优化吧源文档 <http://zhidao.baidu.com/question/156506527.html>
-
一个简单的压力测试代码:
根据网上的一段代码改的,需要添加commons-httpclient.jar包
package com.miracle.testcase;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* @author miracle
* @version 2010.10.18
*/
public class LoadTestCase {
public static void main(String[] args) {
int i = 0;
int num = 1500;//启用的线程数
while (i < num) {
i++;
TClient client = new TClient();
client.setNum(i);
System.out.println("^^^^^^^^线程:" + i + "启动^^^^^^^^^^^^");
client.start();
}
}
}
//每个线程执行的内容
class TClient extends Thread {
int num;
public void setNum(int n) {
this.num = n;
}
public void run() {
int i = 0;
HttpClient client = new HttpClient();
while (true) {
System.out.println("========The: " + num + " Thread of " + i
+ "START=======");
try {
//设置访问地址
HttpMethod method = new PostMethod(
"http://10.5.16.118:8080/tdliveCenter/ServerControlServlet");
//设置参数
NameValuePair ip = new NameValuePair("ip", ""+(num%200));
NameValuePair data = new NameValuePair("data",
"9 2 2 3 4 5 6 7 8 9 1 0 0 0 0 0");
//
method.setQueryString(new NameValuePair[] { ip, data });
client.executeMethod(method);
System.out.println(ip.getValue()+"| " + method.getResponseBodyAsString());
method.releaseConnection();
//每一次访问sleep两秒
this.sleep(2000);
} catch (Exception ex) {
ex.printStackTrace();
}i++;
}
}
}
代码运行时偶尔会出现以下错误:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at weblogic.servlet.internal.PostInputStream.read(PostInputStream.java:170)
at weblogic.servlet.internal.ServletInputStreamImpl$1.read(ServletInputStreamImpl.java:115)
at weblogic.servlet.internal.ServletInputStreamImpl.read(ServletInputStreamImpl.java:180)
at weblogic.servlet.internal.ServletRequestImpl.mergePostParams(ServletRequestImpl.java:1257)
at weblogic.servlet.internal.ServletRequestImpl.parseQueryParams(ServletRequestImpl.java:1124)
at weblogic.servlet.internal.ServletRequestImpl.getParameter(ServletRequestImpl.java:1335)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:383)
.........
在网上找了些说法:
a。maybe:
1,网络编程时未正确捕获java.net.SocketException,客户端先关闭就会报这个异常;
2,数据库和应用服务器的网段不同.
b。好像说防火墙的比较多。。。。
大部分是网络的原因。提交post的东西多,然后服务器接收就超时了。。。
c。如果weblogic server和数据库服务器不在同一台主机,会经常有这种情况,我也遇到过,不过好像的确不怎么影响使用
d。从根本上说是weblogic sp2的bug
e。一般是有些客户端已关闭,一些线程因为延迟等原因觉察不到此连接已结束,继续等到到出错或是 客户端那里不停刷或一个访问/刷新没完成前再刷,要让这个无用的线程死掉和这个错误如果不影响应用运行的话大可不必理会。有一种抛出这个异常的情况是:
浏览器请求了一个服务器端资源,在响应回来之前转而请求其他的资源。这个时候最初的浏览器跟服务器的连接中断,服务器的peer程序就会reset那个connection。
总之这个没啥大事~
java.net.BindException: Address already in use: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket. (Socket.java:366)
at java.net.Socket. (Socket.java:240)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:79)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:121)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:706)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:386)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at com.miracle.testcase.TClient.run(LoadTestCase.java:59)
短时间内new socket操作过多
而socket.close()操作并不能立即释放绑定的端口
而是把端口设置为TIME_WAIT状态
过段时间(默认240s)才释放(用netstat -na可以看到)
最后系统资源耗尽
源文档 <http://blog.csdn.net/miraclestar/archive/2010/10/18/5948797.aspx>
这个不需要添加任何包就可以执行,不过不能传参数:
没找到传参数的接口。。。那位知道的可以mail我
package com.miracle.testcase;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
/**
* @author miracle
* @version 2010.10.18
*/
public class LoadTest2 {
public static void main(String[] args) {
int i = 0;
int num = 1500;//启用的线程数
while (i < num) {
i++;
ThClient client = new ThClient();
client.setNum(i);
System.out.println("^^^^^^^^线程:" + i + "启动^^^^^^^^^^^^");
client.start();
}
}
}
//每个线程执行的内容
class ThClient extends Thread {
int num;
public void setNum(int n) {
this.num = n;
}
public void run() {
int i = 0;
while (true) {
System.out.println("========The: " + num + " Thread of " + i
+ "START=======");
URL url = null;
InputStream is = null;
try {
//每一次访问sleep两秒
this.sleep(2000);
url = new URL(
"http://10.5.16.118:8080/tdliveCenter/ServerControlServlet");
is = url.openStream(); BufferedReader br = new
BufferedReader( new InputStreamReader(is)); String line;
while ((line = br.readLine()) != null) {
System.out.println(line); }
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
i++;
}
}
}