mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add dynamic change maxActive support
This commit is contained in:
@@ -49,14 +49,14 @@ public class Case0 extends TestCase {
|
||||
public final int COUNT = 1000 * 1000 * 1;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
// jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
|
||||
// user = "dragoon25";
|
||||
// password = "dragoon25";
|
||||
// driverClass = "com.alibaba.druid.mock.MockDriver";
|
||||
jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
|
||||
user = "dragoon25";
|
||||
password = "dragoon25";
|
||||
driverClass = "com.alibaba.druid.mock.MockDriver";
|
||||
|
||||
jdbcUrl = "jdbc:mysql://10.20.159.104:3306/masterdb";
|
||||
user = "dragoon";
|
||||
password = "dragoon";
|
||||
// jdbcUrl = "jdbc:mysql://10.20.159.104:3306/masterdb";
|
||||
// user = "dragoon";
|
||||
// password = "dragoon";
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.alibaba.druid.benckmark.pool.druid;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidCase0 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setMaxActive(8);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMinEvictableIdleTimeMillis(1000 * 60 * 5);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_benchmark() throws Exception {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
long startMillis = System.currentTimeMillis();
|
||||
benchmark();
|
||||
long millis = System.currentTimeMillis() - startMillis;
|
||||
|
||||
System.out.println("millis : " + millis);
|
||||
}
|
||||
}
|
||||
|
||||
public void benchmark() throws Exception {
|
||||
for (int i = 0; i < 1000 * 1000 * 10; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.GetConnectionTimeoutException;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class MaxActiveChangeTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setMaxActive(3);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMinEvictableIdleTimeMillis(1000 * 60 * 5);
|
||||
dataSource.setMaxWait(20);
|
||||
dataSource.init();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_maxActive() throws Exception {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Assert.assertEquals(1, connect(1));
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
}
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Assert.assertEquals(2, connect(2));
|
||||
Assert.assertEquals(2, dataSource.getPoolingCount());
|
||||
}
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Assert.assertEquals(3, connect(3));
|
||||
Assert.assertEquals(3, dataSource.getPoolingCount());
|
||||
}
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Assert.assertEquals(3, connect(4));
|
||||
Assert.assertEquals(3, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
dataSource.setMaxActive(4);
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Assert.assertEquals(4, connect(4));
|
||||
Assert.assertEquals(4, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
dataSource.shrink();
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Assert.assertEquals(4, connect(4));
|
||||
Assert.assertEquals(4, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
Assert.assertEquals(4, dataSource.getPoolingCount());
|
||||
dataSource.setMaxActive(3);
|
||||
|
||||
Assert.assertEquals(4, dataSource.getPoolingCount());
|
||||
|
||||
dataSource.shrink();
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
|
||||
// 确保收缩之后不会再长上去
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Assert.assertEquals(3, connect(4));
|
||||
Assert.assertEquals(3, dataSource.getPoolingCount());
|
||||
}
|
||||
}
|
||||
|
||||
public int connect(int count) throws Exception {
|
||||
int successCount = 0;
|
||||
Connection[] connections = new Connection[count];
|
||||
for (int i = 0; i < count; ++i) {
|
||||
try {
|
||||
connections[i] = dataSource.getConnection();
|
||||
successCount++;
|
||||
} catch (GetConnectionTimeoutException e) {
|
||||
// skip
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
JdbcUtils.close(connections[i]);
|
||||
}
|
||||
|
||||
return successCount;
|
||||
}
|
||||
}
|
||||
@@ -148,15 +148,7 @@ public class DataSourceTest3 extends TestCase {
|
||||
|
||||
conn.close();
|
||||
|
||||
{
|
||||
Exception error = null;
|
||||
try {
|
||||
dataSource.setMaxActive(1);
|
||||
} catch (Exception ex) {
|
||||
error = ex;
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
dataSource.setMaxActive(1);
|
||||
}
|
||||
|
||||
public void test_error_7() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user