package com.cea.repository.test;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.InputStream;
public class CapabilityForConnection {
private static Log log = LogFactory.getLog(CapabilityForConnection.class);
/**
* 计算一次测试所消耗的时间
*/
public static long times = 0;
/**
* 连接数
*/
public static long psize = 0;
public static void main(String[] args) throws Exception {
/**
* 运行的次数
*/
int size = 1;
/**
* 见POOLTEST说明
*/
int execsum = 0;
/**
* 见POOLTEST说明
*/
int opencon = 0;
/**
* execsum对应properties的命名
*/
String execs = null;
/**
* opencon对应properties的命名
*/
String openc = null;
long sumtime = 0;
Properties prop = initProperty("reference.properties");
Properties init = initProperty("init.properties");
if (init.size() > 0) {
Object o = init.get("init");
size = Integer.parseInt(o.toString());
execs = init.get("name0").toString();
openc = init.get("name1").toString();
}
for (int i = 0; i < prop.size() / 2; i++) {
execsum = Integer.parseInt(prop.getProperty(execs + i).toString());
opencon = Integer.parseInt(prop.getProperty(openc + i).toString());
sumtime = 0;
psize = 0;
log.info("第" + (i + 1) + "组数据:");
log.info("并发应用数:" + execsum + " 模拟连接数:" + opencon);
String[] reference = {"" + execsum, "" + opencon};
for (int j = 0; j < size; j++) {
times = 0;
PoolTest.main(reference);
sumtime += times;
}
log.info("第" + (i + 1) + "组数据共执行" + size + "次;平均耗时为:" +
sumtime / (size * execsum) + "毫秒");
log.info("平均使用" + psize / size + "个连接");
}
}
private static Properties initProperty(String filename) throws Exception {
InputStream is = new FileInputStream(filename);
Properties prop = new Properties();
prop.load(is);
return prop;
}
}
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.InputStream;
public class CapabilityForConnection {
private static Log log = LogFactory.getLog(CapabilityForConnection.class);
/**
* 计算一次测试所消耗的时间
*/
public static long times = 0;
/**
* 连接数
*/
public static long psize = 0;
public static void main(String[] args) throws Exception {
/**
* 运行的次数
*/
int size = 1;
/**
* 见POOLTEST说明
*/
int execsum = 0;
/**
* 见POOLTEST说明
*/
int opencon = 0;
/**
* execsum对应properties的命名
*/
String execs = null;
/**
* opencon对应properties的命名
*/
String openc = null;
long sumtime = 0;
Properties prop = initProperty("reference.properties");
Properties init = initProperty("init.properties");
if (init.size() > 0) {
Object o = init.get("init");
size = Integer.parseInt(o.toString());
execs = init.get("name0").toString();
openc = init.get("name1").toString();
}
for (int i = 0; i < prop.size() / 2; i++) {
execsum = Integer.parseInt(prop.getProperty(execs + i).toString());
opencon = Integer.parseInt(prop.getProperty(openc + i).toString());
sumtime = 0;
psize = 0;
log.info("第" + (i + 1) + "组数据:");
log.info("并发应用数:" + execsum + " 模拟连接数:" + opencon);
String[] reference = {"" + execsum, "" + opencon};
for (int j = 0; j < size; j++) {
times = 0;
PoolTest.main(reference);
sumtime += times;
}
log.info("第" + (i + 1) + "组数据共执行" + size + "次;平均耗时为:" +
sumtime / (size * execsum) + "毫秒");
log.info("平均使用" + psize / size + "个连接");
}
}
private static Properties initProperty(String filename) throws Exception {
InputStream is = new FileInputStream(filename);
Properties prop = new Properties();
prop.load(is);
return prop;
}
}
POOLTEST计算一次完整过程耗时,统计消耗的连接
package com.cea.repository.test;
import com.cea.repository.test.testdata.MainExecute;
import java.util.HashMap;
import java.util.Map;
import com.cea.repository.connection.PoolSupper;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
/**
*
*
*
*
*
*
*
*
*
* @author 小舟
* @version 1.0
*/
public class PoolTest extends Thread {
private static Log log = LogFactory.getLog(PoolTest.class);
/**
* 并发执行MainExecute的数量
*/
private static int EXECUTESUM = 35;
/**
* 一次MainExecute执行所请求的连接数
*/
public static int CONNECTIONS = 3;
/**
* 记录所使用的连接
*/
public static Map poolTestMap = new HashMap();
/**
* 第sum次执行MainExecute所需要的时间
*/
public int sum = 0;
public void run() {
try {
long s = System.currentTimeMillis();
com.cea.repository.test.testdata.MainExecute.main(null);
long t = System.currentTimeMillis() - s;
CapabilityForConnection.times +=t;
// if(CapabilityForConnection.times < t){
// CapabilityForConnection.times = t;
// }
// log.info("time" + (++sum) + ":" +
// (System.currentTimeMillis() - s));
} catch (Exception ex) {
}
}
public static void main(String[] args) throws Exception {
if(args!= null && args.length>1){
EXECUTESUM = Integer.parseInt(args[0]);
CONNECTIONS = Integer.parseInt(args[1]);
}
PoolSupper.initPool();
startExec(EXECUTESUM);
//设定足够长的时间等待所有程序执行完,得到准确的并发执行所消耗的时间
try {
Thread.sleep(6000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
log.info("运行平均耗时:" + CapabilityForConnection.times/EXECUTESUM);
//如果条件成立,证明连接没有被回收,只要存在一个相同的,就证明连接被重复利用了
CapabilityForConnection.psize +=poolTestMap.size();
if (poolTestMap.size() == EXECUTESUM) {
log.info("不存在重复使用的连接,共创建" + poolTestMap.size()+ "个连接" );
} else {
log.info("共使用" + poolTestMap.size()+ "个连接" );
}
clear();
}
private static void startExec(int EXECUTESUM) {
int i = 0;
while (i < EXECUTESUM) {
if (i++ < EXECUTESUM) {
try {
new PoolTest().start();
} catch (Exception ex2) {
}
}
}
}
private static void clear() {
poolTestMap = new HashMap();
}
}
简单的不能再简单的测试代码:import com.cea.repository.test.testdata.MainExecute;
import java.util.HashMap;
import java.util.Map;
import com.cea.repository.connection.PoolSupper;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
/**
*
*
Title: 连接池性能测试
*
*
Description: 测试不合理的利用连接对WEB应用所造成影响.
*
*
Copyright: Copyright (c) 2005
*
*
Company:
*
* @author 小舟
* @version 1.0
*/
public class PoolTest extends Thread {
private static Log log = LogFactory.getLog(PoolTest.class);
/**
* 并发执行MainExecute的数量
*/
private static int EXECUTESUM = 35;
/**
* 一次MainExecute执行所请求的连接数
*/
public static int CONNECTIONS = 3;
/**
* 记录所使用的连接
*/
public static Map poolTestMap = new HashMap();
/**
* 第sum次执行MainExecute所需要的时间
*/
public int sum = 0;
public void run() {
try {
long s = System.currentTimeMillis();
com.cea.repository.test.testdata.MainExecute.main(null);
long t = System.currentTimeMillis() - s;
CapabilityForConnection.times +=t;
// if(CapabilityForConnection.times < t){
// CapabilityForConnection.times = t;
// }
// log.info("time" + (++sum) + ":" +
// (System.currentTimeMillis() - s));
} catch (Exception ex) {
}
}
public static void main(String[] args) throws Exception {
if(args!= null && args.length>1){
EXECUTESUM = Integer.parseInt(args[0]);
CONNECTIONS = Integer.parseInt(args[1]);
}
PoolSupper.initPool();
startExec(EXECUTESUM);
//设定足够长的时间等待所有程序执行完,得到准确的并发执行所消耗的时间
try {
Thread.sleep(6000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
log.info("运行平均耗时:" + CapabilityForConnection.times/EXECUTESUM);
//如果条件成立,证明连接没有被回收,只要存在一个相同的,就证明连接被重复利用了
CapabilityForConnection.psize +=poolTestMap.size();
if (poolTestMap.size() == EXECUTESUM) {
log.info("不存在重复使用的连接,共创建" + poolTestMap.size()+ "个连接" );
} else {
log.info("共使用" + poolTestMap.size()+ "个连接" );
}
clear();
}
private static void startExec(int EXECUTESUM) {
int i = 0;
while (i < EXECUTESUM) {
if (i++ < EXECUTESUM) {
try {
new PoolTest().start();
} catch (Exception ex2) {
}
}
}
}
private static void clear() {
poolTestMap = new HashMap();
}
}
package com.cea.repository.test.testdata;
import com.cea.repository.connection.drive.ConnectionFactory;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import com.cea.repository.test.PoolTest;
import com.cea.repository.connection.poolpository.PoolFactory;
/**
*
*
*
*
*
* @author 小舟
* @version 1.0
*/
public class MainExecute {
public static void main(String[] args) throws Exception {
testConnection();
}
static void testConnection() throws Exception {
for (int i = 0; i < PoolTest.CONNECTIONS; i++) {
Connection con = PoolFactory.newInstance();
//这里的改变直接影响连接的复用
Thread.sleep(50);
PoolTest.poolTestMap.put(con.toString(), "");
con.close();
}
}
}
import com.cea.repository.connection.drive.ConnectionFactory;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import com.cea.repository.test.PoolTest;
import com.cea.repository.connection.poolpository.PoolFactory;
/**
*
*
Title:
*
Description:
*
Copyright: Copyright (c) 2004
*
Company: cea
* @author 小舟
* @version 1.0
*/
public class MainExecute {
public static void main(String[] args) throws Exception {
testConnection();
}
static void testConnection() throws Exception {
for (int i = 0; i < PoolTest.CONNECTIONS; i++) {
Connection con = PoolFactory.newInstance();
//这里的改变直接影响连接的复用
Thread.sleep(50);
PoolTest.poolTestMap.put(con.toString(), "");
con.close();
}
}
}
三个配置文件的内容:
init.properties文件
#运行的次数
init=5
#并发执行MainExecute的数量所匹配的名字
name0=execsum
#一次MainExecute执行所请求的连接数所匹配的名字
name1=opencon
init=5
#并发执行MainExecute的数量所匹配的名字
name0=execsum
#一次MainExecute执行所请求的连接数所匹配的名字
name1=opencon
reference.properties文件
#过滤数据
execsum0=10
opencon0=1
#第一次测试数据
execsum1=100
opencon1=6
#第二次测试数据
execsum2=85
opencon2=9
#第三次测试数据
execsum3=140
opencon3=3
execsum0=10
opencon0=1
#第一次测试数据
execsum1=100
opencon1=6
#第二次测试数据
execsum2=85
opencon2=9
#第三次测试数据
execsum3=140
opencon3=3
最后一个是pool-config.xml数据源配置:
xml version="1.0" encoding="GB2312"?>
<DataResources>
<ResourceParams dateIndentity="boat1">
<defaultAutoCommit>false< SPAN>defaultAutoCommit>
<initialSize>30< SPAN>initialSize>
<maxActive>40< SPAN>maxActive>
<minIdle>0< SPAN>minIdle>
<maxIdle>18< SPAN>maxIdle>
<maxWait>10000< SPAN>maxWait>
<username>forum< SPAN>username>
<password>king< SPAN>password>
<driverClassName>oracle.jdbc.driver.OracleDriver< SPAN>driverClassName>
<url>jdbc:oracle:thin:@192.168.1.3:1521:gzest< SPAN>url>
<removeAbandoned>true< SPAN>removeAbandoned>
<removeAbandonedTimeout>10< SPAN>removeAbandonedTimeout>
<logAbandoned>true< SPAN>logAbandoned>
< SPAN>ResourceParams>
< SPAN>DataResources>
<DataResources>
<ResourceParams dateIndentity="boat1">
<defaultAutoCommit>false< SPAN>defaultAutoCommit>
<initialSize>30< SPAN>initialSize>
<maxActive>40< SPAN>maxActive>
<minIdle>0< SPAN>minIdle>
<maxIdle>18< SPAN>maxIdle>
<maxWait>10000< SPAN>maxWait>
<username>forum< SPAN>username>
<password>king< SPAN>password>
<driverClassName>oracle.jdbc.driver.OracleDriver< SPAN>driverClassName>
<url>jdbc:oracle:thin:@192.168.1.3:1521:gzest< SPAN>url>
<removeAbandoned>true< SPAN>removeAbandoned>
<removeAbandonedTimeout>10< SPAN>removeAbandonedTimeout>
<logAbandoned>true< SPAN>logAbandoned>
< SPAN>ResourceParams>
< SPAN>DataResources>
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/