diff --git a/src/main/java/com/alibaba/druid/stat/DruidStatService.java b/src/main/java/com/alibaba/druid/stat/DruidStatService.java index 1b510e0804..4494694218 100644 --- a/src/main/java/com/alibaba/druid/stat/DruidStatService.java +++ b/src/main/java/com/alibaba/druid/stat/DruidStatService.java @@ -299,7 +299,7 @@ public final class DruidStatService implements DruidStatServiceMBean { return returnJSONResult(RESULT_CODE_SUCCESS, result); } - private String returnJSONResult(int resultCode, Object content) { + public static String returnJSONResult(int resultCode, Object content) { Map dataMap = new LinkedHashMap(); dataMap.put("ResultCode", resultCode); dataMap.put("Content", content); diff --git a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java index 2342ccb306..2870421b72 100644 --- a/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java +++ b/src/main/java/com/alibaba/druid/support/http/StatViewServlet.java @@ -17,8 +17,15 @@ package com.alibaba.druid.support.http; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import javax.management.MBeanServerConnection; +import javax.management.ObjectName; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -40,31 +47,43 @@ import com.alibaba.druid.util.StringUtils; */ public class StatViewServlet extends HttpServlet { - private final static Log LOG = LogFactory.getLog(StatViewServlet.class); + private final static Log LOG = LogFactory.getLog(StatViewServlet.class); - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public static final String PARAM_NAME_RESET_ENABLE = "resetEnable"; - public static final String PARAM_NAME_ALLOW = "allow"; - public static final String PARAM_NAME_DENY = "deny"; + public static final String PARAM_NAME_RESET_ENABLE = "resetEnable"; + public static final String PARAM_NAME_ALLOW = "allow"; + public static final String PARAM_NAME_DENY = "deny"; - public static final String PARAM_NAME_USERNAME = "loginUsername"; - public static final String PARAM_NAME_PASSWORD = "loginPassword"; + public static final String PARAM_NAME_USERNAME = "loginUsername"; + public static final String PARAM_NAME_PASSWORD = "loginPassword"; - public static final String SESSION_USER_KEY = "druid-user"; + public static final String PARAM_NAME_JMX_URL = "jmxUrl"; + public static final String PARAM_NAME_JMX_USERNAME = "jmxUsername"; + public static final String PARAM_NAME_JMX_PASSWORD = "jmxPassword"; - private final static String RESOURCE_PATH = "support/http/resources"; - private final static String TEMPLATE_PAGE_RESOURCE_PATH = RESOURCE_PATH + "/template.html"; + public static final String SESSION_USER_KEY = "druid-user"; - private DruidStatService statService = DruidStatService.getInstance(); + private final static String RESOURCE_PATH = "support/http/resources"; + private final static String TEMPLATE_PAGE_RESOURCE_PATH = RESOURCE_PATH + "/template.html"; - public String templatePage; + private DruidStatService statService = DruidStatService.getInstance(); - private List allowList = new ArrayList(); - private List denyList = new ArrayList(); + public String templatePage; - private String username = null; - private String password = null; + private List allowList = new ArrayList(); + private List denyList = new ArrayList(); + + private String username = null; + private String password = null; + + /** web.xml中配置的jmx的连接地址 */ + private String jmxUrl = null; + /** web.xml中配置的jmx的用户名 */ + private String jmxUsername = null; + /** web.xml中配置的jmx的密码 */ + private String jmxPassword = null; + private MBeanServerConnection conn = null; public void init() throws ServletException { initAuthEnv(); @@ -127,6 +146,120 @@ public class StatViewServlet extends HttpServlet { String msg = "initParameter config error, deny : " + getInitParameter(PARAM_NAME_DENY); LOG.error(msg, e); } + + // 获取jmx的连接配置信息 + String param = readInitParam(PARAM_NAME_JMX_URL); + if (param != null) { + jmxUrl = param; + jmxUsername = readInitParam(PARAM_NAME_JMX_USERNAME); + jmxPassword = readInitParam(PARAM_NAME_JMX_PASSWORD); + try { + initJmxConn(); + } catch (IOException e) { + LOG.error("init jmx connection error", e); + } + } + + } + + /** + * 读取servlet中的配置参数. + * + * @param key 配置参数名 + * @return 配置参数值,如果不存在当前配置参数,或者为配置参数长度为0,将返回null + */ + private String readInitParam(String key) { + String value = null; + try { + String param = getInitParameter(key); + if (param != null) { + param = param.trim(); + if (param.length() > 0) { + value = param; + } + } + } catch (Exception e) { + String msg = "initParameter config [" + key + "] error"; + LOG.warn(msg, e); + } + return value; + } + + /** + * 初始化jmx连接 + * + * @throws IOException + */ + private void initJmxConn() throws IOException { + if (jmxUrl != null) { + JMXServiceURL url = new JMXServiceURL(jmxUrl); + Map env = null; + if (jmxUsername != null) { + env = new HashMap(); + String[] credentials = new String[] { jmxUsername, jmxPassword }; + env.put(JMXConnector.CREDENTIALS, credentials); + } + JMXConnector jmxc = JMXConnectorFactory.connect(url, env); + conn = jmxc.getMBeanServerConnection(); + } + } + + /** + * 根据指定的url来获取jmx服务返回的内容. + * + * @param connetion jmx连接 + * @param url url内容 + * @return the jmx返回的内容 + * @throws Exception the exception + */ + private String getJmxResult(MBeanServerConnection connetion, String url) throws Exception { + ObjectName name = new ObjectName(DruidStatService.MBEAN_NAME); + + String result = (String) conn.invoke(name, "service", new String[] { url }, + new String[] { String.class.getName() }); + return result; + } + + /** + * 程序首先判断是否存在jmx连接地址,如果不存在,则直接调用本地的duird服务; 如果存在,则调用远程jmx服务。在进行jmx通信,首先判断一下jmx连接是否已经建立成功,如果已经 + * 建立成功,则直接进行通信,如果之前没有成功建立,则会尝试重新建立一遍。. + * + * @param url 要连接的服务地址 + * @return 调用服务后返回的json字符串 + */ + private String genServiceResponse(String url) { + String resp = null; + if (jmxUrl == null) { + resp = statService.service(url); + } else { + if (conn == null) {// 连接在初始化时创建失败 + try {// 尝试重新连接 + initJmxConn(); + } catch (IOException e) { + LOG.error("init jmx connection error", e); + resp = DruidStatService.returnJSONResult(DruidStatService.RESULT_CODE_ERROR, + "init jmx connection error" + e.getMessage()); + } + if (conn != null) {// 连接成功 + try { + resp = getJmxResult(conn, url); + } catch (Exception e) { + LOG.error("get jmx data error", e); + resp = DruidStatService.returnJSONResult(DruidStatService.RESULT_CODE_ERROR, "get data error:" + + e.getMessage()); + } + } + } else {// 连接成功 + try { + resp = getJmxResult(conn, url); + } catch (Exception e) { + LOG.error("get jmx data error", e); + resp = DruidStatService.returnJSONResult(DruidStatService.RESULT_CODE_ERROR, + "get data error" + e.getMessage()); + } + } + } + return resp; } private void initAuthEnv() { @@ -242,7 +375,7 @@ public class StatViewServlet extends HttpServlet { if (request.getQueryString() != null && request.getQueryString().length() > 0) { fullUrl += "?" + request.getQueryString(); } - response.getWriter().print(statService.service(fullUrl)); + response.getWriter().print(genServiceResponse(fullUrl)); return; } @@ -257,7 +390,7 @@ public class StatViewServlet extends HttpServlet { if (bytes != null) { response.getOutputStream().write(bytes); } - + return; } diff --git a/src/test/java/com/alibaba/druid/benckmark/pool/Case4.java b/src/test/java/com/alibaba/druid/benckmark/pool/Case4.java new file mode 100644 index 0000000000..ddcf23156b --- /dev/null +++ b/src/test/java/com/alibaba/druid/benckmark/pool/Case4.java @@ -0,0 +1,287 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.benckmark.pool; + +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.text.NumberFormat; +import java.util.concurrent.CountDownLatch; + +import javax.sql.DataSource; + +import junit.framework.TestCase; + +import org.apache.commons.dbcp.BasicDataSource; +import org.logicalcobwebs.proxool.ProxoolDataSource; + +import com.alibaba.druid.TestUtil; +import com.alibaba.druid.benckmark.pool.model.TableOperator; +import com.alibaba.druid.pool.DruidDataSource; +import com.jolbox.bonecp.BoneCPDataSource; +import com.mchange.v2.c3p0.ComboPooledDataSource; +import com.taobao.datasource.LocalTxDataSourceDO; +import com.taobao.datasource.TaobaoDataSourceFactory; +import com.taobao.datasource.resource.adapter.jdbc.local.LocalTxDataSource; + +/** + * 测试各个连接池在进行批量插入时操作的性能 + * + * @author yunnysunny 2013-3-18 下午01:11:40 + */ +public class Case4 extends TestCase { + + private String jdbcUrl; + private String user; + private String password; + private String driverClass; + private int initialSize = 10; + private int minPoolSize = 10; + private int maxPoolSize = 50; + private int maxActive = 50; + private String validationQuery = "SELECT 1"; + private int maxOpenPreparedStatements = 100; + private int threadCount = 5; + private int loopCount = 10; + final int LOOP_COUNT = 100 * 1 * 1 / threadCount; + + protected void setUp() throws Exception { + user = "root"; + password = "aaaaaaaa"; + + jdbcUrl = "jdbc:mysql://localhost:3306/dragoon_v25_masterdb"; + driverClass = "com.mysql.jdbc.Driver"; + } + + public void test_druid() throws Exception { + DruidDataSource dataSource = new DruidDataSource(); + + dataSource.setInitialSize(initialSize); + dataSource.setMaxActive(maxActive); + dataSource.setMinIdle(minPoolSize); + dataSource.setMaxIdle(maxPoolSize); + dataSource.setPoolPreparedStatements(true); + dataSource.setDriverClassName(driverClass); + dataSource.setUrl(jdbcUrl); + dataSource.setPoolPreparedStatements(true); + dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements); + dataSource.setUsername(user); + dataSource.setPassword(password); + dataSource.setValidationQuery(validationQuery); + dataSource.setTestOnBorrow(false); + + for (int i = 0; i < loopCount; ++i) { + p0(dataSource, "druid", threadCount); + } + System.out.println(); + } + + public void test_jobss() throws Exception { + LocalTxDataSourceDO dataSourceDO = new LocalTxDataSourceDO(); + dataSourceDO.setBlockingTimeoutMillis(1000 * 60); + dataSourceDO.setMaxPoolSize(maxPoolSize); + dataSourceDO.setMinPoolSize(minPoolSize); + + dataSourceDO.setDriverClass(driverClass); + dataSourceDO.setConnectionURL(jdbcUrl); + dataSourceDO.setUserName(user); + dataSourceDO.setPassword(password); + + LocalTxDataSource tx = TaobaoDataSourceFactory.createLocalTxDataSource(dataSourceDO); + DataSource dataSource = tx.getDatasource(); + + for (int i = 0; i < loopCount; ++i) { + p0(dataSource, "jboss-datasource", threadCount); + } + System.out.println(); + } + + public void test_dbcp() throws Exception { + final BasicDataSource dataSource = new BasicDataSource(); + + dataSource.setInitialSize(initialSize); + dataSource.setMaxActive(maxActive); + dataSource.setMinIdle(minPoolSize); + dataSource.setMaxIdle(maxPoolSize); + dataSource.setPoolPreparedStatements(true); + dataSource.setDriverClassName(driverClass); + dataSource.setUrl(jdbcUrl); + dataSource.setPoolPreparedStatements(true); + dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements); + dataSource.setUsername(user); + dataSource.setPassword(password); + dataSource.setValidationQuery("SELECT 1"); + dataSource.setTestOnBorrow(false); + + for (int i = 0; i < loopCount; ++i) { + p0(dataSource, "dbcp", threadCount); + } + System.out.println(); + } + + public void test_bonecp() throws Exception { + BoneCPDataSource dataSource = new BoneCPDataSource(); + // dataSource.(10); + // dataSource.setMaxActive(50); + dataSource.setMinConnectionsPerPartition(minPoolSize); + dataSource.setMaxConnectionsPerPartition(maxPoolSize); + + dataSource.setDriverClass(driverClass); + dataSource.setJdbcUrl(jdbcUrl); + dataSource.setStatementsCacheSize(maxOpenPreparedStatements); + dataSource.setServiceOrder("LIFO"); + // dataSource.setMaxOpenPreparedStatements(100); + dataSource.setUsername(user); + dataSource.setPassword(password); + // dataSource.setConnectionTestStatement("SELECT 1"); + dataSource.setPartitionCount(1); + dataSource.setAcquireIncrement(5); + dataSource.setIdleConnectionTestPeriod(0L); + // dataSource.setDisableConnectionTracking(true); + + for (int i = 0; i < loopCount; ++i) { + p0(dataSource, "boneCP", threadCount); + } + System.out.println(); + } + + public void test_c3p0() throws Exception { + ComboPooledDataSource dataSource = new ComboPooledDataSource(); + // dataSource.(10); + // dataSource.setMaxActive(50); + dataSource.setMinPoolSize(minPoolSize); + dataSource.setMaxPoolSize(maxPoolSize); + + dataSource.setDriverClass(driverClass); + dataSource.setJdbcUrl(jdbcUrl); + dataSource.setMaxStatements(maxOpenPreparedStatements); + + dataSource.setUser(user); + dataSource.setPassword(password); + + for (int i = 0; i < loopCount; ++i) { + p0(dataSource, "c3p0", threadCount); + } + System.out.println(); + } + + public void test_proxool() throws Exception { + ProxoolDataSource dataSource = new ProxoolDataSource(); + // dataSource.(10); + // dataSource.setMaxActive(50); + dataSource.setMinimumConnectionCount(minPoolSize); + dataSource.setMaximumConnectionCount(maxPoolSize); + + dataSource.setDriver(driverClass); + dataSource.setDriverUrl(jdbcUrl); + + dataSource.setUser(user); + dataSource.setPassword(password); + + for (int i = 0; i < loopCount; ++i) { + p0(dataSource, "proxool", threadCount); + } + System.out.println(); + } + + public void test_tomcat_jdbc() throws Exception { + org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); + // dataSource.(10); + dataSource.setMaxIdle(maxPoolSize); + dataSource.setMinIdle(minPoolSize); + dataSource.setMaxActive(maxPoolSize); + + dataSource.setDriverClassName(driverClass); + dataSource.setUrl(jdbcUrl); + // dataSource.setPoolPreparedStatements(true); + // dataSource.setMaxOpenPreparedStatements(100); + dataSource.setUsername(user); + dataSource.setPassword(password); + + for (int i = 0; i < loopCount; ++i) { + p0(dataSource, "tomcat-jdbc", threadCount); + } + System.out.println(); + } + + private void p0(final DataSource dataSource, String name, int threadCount) throws Exception { + + final CountDownLatch startLatch = new CountDownLatch(1); + final CountDownLatch endLatch = new CountDownLatch(threadCount); + final CountDownLatch dumpLatch = new CountDownLatch(1); + + Thread[] threads = new Thread[threadCount]; + final TableOperator operator = new TableOperator(); + operator.setDataSource(dataSource); + operator.createTable(); + for (int i = 0; i < threadCount; ++i) { + Thread thread = new Thread() { + + public void run() { + try { + startLatch.await(); + + for (int i = 0; i < LOOP_COUNT; ++i) { + operator.insert(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + endLatch.countDown(); + + try { + dumpLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }; + threads[i] = thread; + thread.start(); + } + long startMillis = System.currentTimeMillis(); + long startYGC = TestUtil.getYoungGC(); + long startFullGC = TestUtil.getFullGC(); + startLatch.countDown(); + endLatch.await(); + + long[] threadIdArray = new long[threads.length]; + for (int i = 0; i < threads.length; ++i) { + threadIdArray[i] = threads[i].getId(); + } + ThreadInfo[] threadInfoArray = ManagementFactory.getThreadMXBean().getThreadInfo(threadIdArray); + + dumpLatch.countDown(); + operator.dropTable(); + + long blockedCount = 0; + long waitedCount = 0; + for (int i = 0; i < threadInfoArray.length; ++i) { + ThreadInfo threadInfo = threadInfoArray[i]; + blockedCount += threadInfo.getBlockedCount(); + waitedCount += threadInfo.getWaitedCount(); + } + + long millis = System.currentTimeMillis() - startMillis; + long ygc = TestUtil.getYoungGC() - startYGC; + long fullGC = TestUtil.getFullGC() - startFullGC; + + System.out.println("thread " + threadCount + " " + name + " millis : " + + NumberFormat.getInstance().format(millis) + "; YGC " + ygc + " FGC " + fullGC + + " blocked " + NumberFormat.getInstance().format(blockedCount) // + + " waited " + NumberFormat.getInstance().format(waitedCount)); + + } +} diff --git a/src/test/java/com/alibaba/druid/benckmark/pool/model/TableOperator.java b/src/test/java/com/alibaba/druid/benckmark/pool/model/TableOperator.java new file mode 100644 index 0000000000..002c207266 --- /dev/null +++ b/src/test/java/com/alibaba/druid/benckmark/pool/model/TableOperator.java @@ -0,0 +1,95 @@ +package com.alibaba.druid.benckmark.pool.model; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; + +import javax.sql.DataSource; + +public class TableOperator { + + private DataSource dataSource; + + public void setDataSource(DataSource dataSource) { + this.dataSource = dataSource; + } + + private static final int COUNT = 2; + + public TableOperator(){ + + } + + public void insert() throws Exception { + + StringBuffer ddl = new StringBuffer(); + ddl.append("INSERT INTO t_big ("); + for (int i = 0; i < COUNT; ++i) { + if (i != 0) { + ddl.append(", "); + } + ddl.append("F" + i); + } + ddl.append(") VALUES ("); + for (int i = 0; i < COUNT; ++i) { + if (i != 0) { + ddl.append(", "); + } + ddl.append("?"); + } + ddl.append(")"); + + Connection conn = dataSource.getConnection(); + + // System.out.println(ddl.toString()); + try { + PreparedStatement stmt = conn.prepareStatement(ddl.toString()); + + for (int i = 0; i < COUNT; ++i) { + stmt.setInt(i + 1, i); + } + + stmt.execute(); + stmt.close(); + } finally { + + conn.close(); + } + } + + public void dropTable() throws SQLException { + + Connection conn = dataSource.getConnection(); + try { + Statement stmt = conn.createStatement(); + stmt.execute("DROP TABLE t_big"); + stmt.close(); + } finally { + conn.close(); + } + } + + public void createTable() throws SQLException { + StringBuffer ddl = new StringBuffer(); + ddl.append("CREATE TABLE t_big (FID INT AUTO_INCREMENT PRIMARY KEY "); + for (int i = 0; i < COUNT; ++i) { + ddl.append(", "); + ddl.append("F" + i); + ddl.append(" BIGINT NULL"); + } + ddl.append(")"); + + Connection conn = dataSource.getConnection(); + try { + Statement stmt = conn.createStatement(); + stmt.addBatch("DROP TABLE IF EXISTS t_big"); + stmt.addBatch(ddl.toString()); + stmt.executeBatch(); + stmt.close(); + } finally { + conn.close(); + } + + } +}