mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
配置和apache dbcp一致
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@5 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.alibaba.druid;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
public class TestUtil {
|
||||
public static long getYoungGC() {
|
||||
try {
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
ObjectName objectName;
|
||||
if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=ParNew"))) {
|
||||
objectName = new ObjectName("java.lang:type=GarbageCollector,name=ParNew");
|
||||
} else if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=Copy"))) {
|
||||
objectName = new ObjectName("java.lang:type=GarbageCollector,name=Copy");
|
||||
} else {
|
||||
objectName = new ObjectName("java.lang:type=GarbageCollector,name=PS Scavenge");
|
||||
}
|
||||
|
||||
return (Long) mbeanServer.getAttribute(objectName, "CollectionCount");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error");
|
||||
}
|
||||
}
|
||||
|
||||
public static long getFullGC() {
|
||||
try {
|
||||
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
|
||||
ObjectName objectName;
|
||||
|
||||
if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep"))) {
|
||||
objectName = new ObjectName("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep");
|
||||
} else if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=MarkSweepCompact"))) {
|
||||
objectName = new ObjectName("java.lang:type=GarbageCollector,name=MarkSweepCompact");
|
||||
} else {
|
||||
objectName = new ObjectName("java.lang:type=GarbageCollector,name=PS MarkSweep");
|
||||
}
|
||||
|
||||
return (Long) mbeanServer.getAttribute(objectName, "CollectionCount");
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("error");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,10 +43,10 @@ public class Case0 extends TestCase {
|
||||
dataSource.setMinIdle(minPoolSize);
|
||||
dataSource.setMaxIdle(maxPoolSize);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setDriverClass(driverClass);
|
||||
dataSource.setDriverClassName(driverClass);
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setUser(user);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
|
||||
final int LOOP_COUNT = 1000 * 1000;
|
||||
|
||||
@@ -41,10 +41,10 @@ public class ConcurrentTest extends TestCase {
|
||||
dataSource.setMinIdle(minPoolSize);
|
||||
dataSource.setMaxIdle(maxPoolSize);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setDriverClass(driverClass);
|
||||
dataSource.setDriverClassName(driverClass);
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setUser(user);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
|
||||
final int THREAD_COUNT = 2;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class TestDruidDataSource extends TestCase {
|
||||
|
||||
dataSource.setInitialSize(10);
|
||||
dataSource.setMaxIdle(10);
|
||||
dataSource.setDriverClass("com.alibaba.druid.mock.MockDriver");
|
||||
dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
|
||||
Assert.assertEquals(0, dataSource.getConnectCount());
|
||||
|
||||
@@ -42,7 +42,7 @@ public class Test_C2 extends TestCase {
|
||||
dataSource.setMinIdle(minPoolSize);
|
||||
dataSource.setMaxIdle(maxPoolSize);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setDriverClass(driverClass);
|
||||
dataSource.setDriverClassName(driverClass);
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import junit.framework.TestCase;
|
||||
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
|
||||
import com.alibaba.druid.TestUtil;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class Case0 extends TestCase {
|
||||
@@ -38,15 +39,16 @@ public class Case0 extends TestCase {
|
||||
dataSource.setMinIdle(minPoolSize);
|
||||
dataSource.setMaxIdle(maxPoolSize);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setDriverClass(driverClass);
|
||||
dataSource.setDriverClassName(driverClass);
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setUser(user);
|
||||
dataSource.setUsername(user);
|
||||
dataSource.setPassword(password);
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
p0(dataSource);
|
||||
p0(dataSource, "druid");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
@@ -64,18 +66,25 @@ public class Case0 extends TestCase {
|
||||
dataSource.setPassword(password);
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
p0(dataSource);
|
||||
p0(dataSource, "dbcp");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private void p0(DataSource dataSource) throws SQLException {
|
||||
private void p0(DataSource dataSource, String name) throws SQLException {
|
||||
long startMillis = System.currentTimeMillis();
|
||||
long startYGC = TestUtil.getYoungGC();
|
||||
long startFullGC = TestUtil.getFullGC();
|
||||
|
||||
final int COUNT = 1000 * 1000;
|
||||
for (int i = 0; i < COUNT; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.close();
|
||||
}
|
||||
long millis = System.currentTimeMillis() - startMillis;
|
||||
System.out.println(dataSource.getClass().getName() + " millis : " + NumberFormat.getInstance().format(millis));
|
||||
long ygc = TestUtil.getYoungGC() - startYGC;
|
||||
long fullGC = TestUtil.getFullGC() - startFullGC;
|
||||
|
||||
System.out.println(name + " millis : " + NumberFormat.getInstance().format(millis) + ", YGC " + ygc + " FGC " + fullGC);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user