From 37606d7b1349c32f28677b51ee01742d9016a02c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E5=B0=91?= Date: Thu, 12 Apr 2012 16:02:28 +0800 Subject: [PATCH] add test --- .../filter/StatFilterConcurrentTest.java | 71 +++++++++++++++++++ .../druid/sql/parser/SQLLexerTest2.java | 4 ++ 2 files changed, 75 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/proxy/filter/StatFilterConcurrentTest.java diff --git a/src/test/java/com/alibaba/druid/bvt/proxy/filter/StatFilterConcurrentTest.java b/src/test/java/com/alibaba/druid/bvt/proxy/filter/StatFilterConcurrentTest.java new file mode 100644 index 0000000000..1d6c12316a --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/proxy/filter/StatFilterConcurrentTest.java @@ -0,0 +1,71 @@ +package com.alibaba.druid.bvt.proxy.filter; + +import java.sql.Connection; +import java.sql.Statement; +import java.util.concurrent.CountDownLatch; + +import junit.framework.TestCase; + +import com.alibaba.druid.filter.stat.StatFilter; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.stat.JdbcSqlStat; + +public class StatFilterConcurrentTest extends TestCase { + + private DruidDataSource dataSource; + private StatFilter statFilter; + private int LOOP_COUNT = 1000 * 1; + + public void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setMaxActive(100); + + statFilter = new StatFilter(); + dataSource.getProxyFilters().add(statFilter); + dataSource.setConnectionProperties("executeSleep=1"); + } + + public void tearDown() throws Exception { + dataSource.close(); + } + + public void test_stat() throws Exception { + concurrent(100); + for (JdbcSqlStat sqlStat : statFilter.getDataSourceStat().getSqlStatMap().values()) { + System.out.println(sqlStat.getConcurrentMax()); + } + } + + public void concurrent(int threadCount) throws Exception { + Thread[] threads = new Thread[threadCount]; + final CountDownLatch endLatch = new CountDownLatch(threadCount); + + for (int i = 0; i < threadCount; ++i) { + threads[i] = new Thread() { + + public void run() { + try { + for (int i = 0; i < LOOP_COUNT; ++i) { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + stmt.executeUpdate("select 1"); + stmt.close(); + conn.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + endLatch.countDown(); + } + } + }; + } + + for (int i = 0; i < threadCount; ++i) { + threads[i].start(); + } + endLatch.await(); + + } +} diff --git a/src/test/java/com/alibaba/druid/sql/parser/SQLLexerTest2.java b/src/test/java/com/alibaba/druid/sql/parser/SQLLexerTest2.java index 1e778c62be..d8e3d8973c 100644 --- a/src/test/java/com/alibaba/druid/sql/parser/SQLLexerTest2.java +++ b/src/test/java/com/alibaba/druid/sql/parser/SQLLexerTest2.java @@ -18,6 +18,10 @@ public class SQLLexerTest2 extends TestCase { } else { System.out.println(tok.name() + "\t\t\t" + tok.name); } + + if (tok == Token.WHERE) { + System.out.println("where pos : " + lexer.pos()); + } if (tok == Token.EOF) { break;