mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
implemention of console monitor tool
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package com.alibaba.druid.console;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Statement;
|
||||
import com.alibaba.druid.support.console.Option;
|
||||
import org.junit.runner.notification.Failure;
|
||||
import org.junit.runner.JUnitCore;
|
||||
import org.junit.runner.Result;
|
||||
import com.alibaba.druid.support.console.DruidStat;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class DruidStatTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
private DruidDataSource dataSource2;
|
||||
|
||||
private static String getSelfPid() {
|
||||
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|
||||
return pid;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setUrl("jdbc:h2:mem:test;");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setFilters("stat");
|
||||
dataSource.setRemoveAbandoned(true);
|
||||
dataSource.setRemoveAbandonedTimeout(600);
|
||||
JdbcUtils.execute(dataSource, "CREATE TABLE user (id INT, name VARCHAR(40))");
|
||||
JdbcUtils.execute(dataSource, "insert into user values(20,'name1')");
|
||||
JdbcUtils.execute(dataSource, "insert into user values(30,'name2')");
|
||||
|
||||
dataSource2 = new DruidDataSource();
|
||||
dataSource2.setMinIdle(1);
|
||||
dataSource2.setUrl("jdbc:h2:mem:test2;");
|
||||
dataSource2.setTestOnBorrow(false);
|
||||
dataSource2.setFilters("stat");
|
||||
dataSource2.setRemoveAbandoned(true);
|
||||
dataSource2.setRemoveAbandonedTimeout(600);
|
||||
|
||||
JdbcUtils.execute(dataSource2, "CREATE TABLE user (id INT, name VARCHAR(40))");
|
||||
JdbcUtils.execute(dataSource2, "insert into user values(20,'name1')");
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.execute(dataSource, "DROP TABLE user");
|
||||
JdbcUtils.close(dataSource);
|
||||
|
||||
JdbcUtils.execute(dataSource2, "DROP TABLE user");
|
||||
JdbcUtils.close(dataSource2);
|
||||
}
|
||||
|
||||
public void test_printDruidStat() throws Exception {
|
||||
String pid = getSelfPid();
|
||||
String[] cmdArray = {"-sql", pid};
|
||||
Option opt = Option.parseOptions(cmdArray);
|
||||
DruidStat.printDruidStat(opt);
|
||||
}
|
||||
|
||||
public void test_printDruidStat2() throws Exception {
|
||||
String pid = getSelfPid();
|
||||
String[] cmdArray = {"-ds","-s2", pid};
|
||||
Option opt = Option.parseOptions(cmdArray);
|
||||
DruidStat.printDruidStat(opt);
|
||||
}
|
||||
|
||||
public void test_printDruidStat3() throws Exception {
|
||||
String pid = getSelfPid();
|
||||
String[] cmdArray = {"-act", pid};
|
||||
Option opt = Option.parseOptions(cmdArray);
|
||||
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
stmt = conn.createStatement();
|
||||
stmt.execute("insert into user values(30,'name2')");
|
||||
DruidStat.printDruidStat(opt);
|
||||
} finally {
|
||||
if (stmt != null ) try { stmt.close(); } catch (Exception e) {}
|
||||
if (conn != null ) try { conn.close(); } catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Result result = JUnitCore.runClasses(DruidStatTest.class);
|
||||
for (Failure failure : result.getFailures()) {
|
||||
System.out.println(failure.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.alibaba.druid.console;
|
||||
|
||||
import com.alibaba.druid.support.console.OptionParseException;
|
||||
import com.alibaba.druid.support.console.Option;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.Assert;
|
||||
|
||||
public class OptionTest extends TestCase {
|
||||
|
||||
|
||||
public void test_parseOptions() throws Exception {
|
||||
String[] cmdArray = {"-sql","-ds", "200"};
|
||||
Option opt = Option.parseOptions(cmdArray);
|
||||
Assert.assertNotNull(opt);
|
||||
Assert.assertTrue(opt.printSqlData());
|
||||
Assert.assertTrue(opt.printDataSourceData());
|
||||
Assert.assertFalse(opt.printActiveConn());
|
||||
Assert.assertEquals(opt.getVmid(), 200);
|
||||
|
||||
cmdArray = new String[] {"-act", "738"};
|
||||
opt = Option.parseOptions(cmdArray);
|
||||
Assert.assertNotNull(opt);
|
||||
Assert.assertFalse(opt.printSqlData());
|
||||
Assert.assertFalse(opt.printDataSourceData());
|
||||
Assert.assertTrue(opt.printActiveConn());
|
||||
Assert.assertEquals(opt.getStyle(), Option.PrintStyle.HORIZONTAL);
|
||||
Assert.assertEquals(opt.getVmid(), 738);
|
||||
|
||||
cmdArray = new String[] {"-ds", "-s2", "1319"};
|
||||
opt = Option.parseOptions(cmdArray);
|
||||
Assert.assertNotNull(opt);
|
||||
Assert.assertFalse(opt.printSqlData());
|
||||
Assert.assertTrue(opt.printDataSourceData());
|
||||
Assert.assertFalse(opt.printActiveConn());
|
||||
Assert.assertEquals(opt.getStyle(), Option.PrintStyle.VERTICAL);
|
||||
Assert.assertEquals(opt.getVmid(), 1319);
|
||||
|
||||
|
||||
//not enough arguments
|
||||
cmdArray = new String[] {};
|
||||
try {
|
||||
opt = Option.parseOptions(cmdArray);
|
||||
} catch (OptionParseException e) {
|
||||
System.out.println(e.getMessage());
|
||||
Assert.assertNotNull(e);
|
||||
}
|
||||
|
||||
//need vmid
|
||||
cmdArray = new String[] {"-ds"};
|
||||
try {
|
||||
opt = Option.parseOptions(cmdArray);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
Assert.assertNotNull(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void test_printHelp() throws Exception {
|
||||
Option.printHelp();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user