日期:2014-05-16 浏览次数:20514 次
一共两个UnitTest,一个PUnit的Runner,代码如下
?
package test; import java.math.BigDecimal; import java.util.Iterator; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; //import org.springframework.jdbc.datasource.DriverManagerDataSource; import com.mchange.v2.c3p0.DriverManagerDataSource; import junit.framework.TestCase; public class InsertQueryJava extends TestCase { public void testAll() { DriverManagerDataSource ds = new DriverManagerDataSource(); // ds.setUsername("xx"); // ds.setPassword("xx"); // ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); // ds.setUrl("xx"); ds.setUser("xx"); ds.setPassword("xx"); ds.setJdbcUrl("xx"); ds.setDriverClass("oracle.jdbc.driver.OracleDriver"); SimpleJdbcTemplate tpl = new SimpleJdbcTemplate(ds); String sql1 = "insert into xxxx(a, b) values ('test', 100)"; String sql2 = "select a, b from xxxx"; tpl.update(sql1); int max = 0; List ll = tpl.queryForList(sql2); Iterator it = ll.iterator(); while(it.hasNext()){ Map item = (Map) it.next(); BigDecimal x = (BigDecimal) item.get("B"); if(x.intValue() > max) max = x.intValue(); } System.out.println(max); } }
?
?
package test import com.mchange.v2.c3p0.DriverManagerDataSource; import groovy.sql.Sql class InsertQuery extends GroovyTestCase{ void testAll(){ def p = [ url:'xx', u:'xx', p:'xx', driver:'oracle.jdbc.driver.OracleDriver' ] // def db = Sql.newInstance(p.url, p.u, p.p, p.driver) def ds = new DriverManagerDataSource() ds.setUser(p.u) ds.setPassword(p.p) ds.setJdbcUrl(p.url) ds.setDriverClass(p.driver) def db = new Sql(ds) String sql = "insert into xxxx(a, b) values ('test', 100)" String sql2 = "select a, b from xxxx" db.execute(sql) def ll = db.rows(sql2) int max = 0 for(one in ll){ if(one.b > max) max = one.b } println max // def item = ll.max{it.b} // println item.b } }
?
?
package test; import org.punit.runner.ConcurrentRunner; public class Runner { public static void main(String[] args) { int threadNum = 20; new ConcurrentRunner(threadNum).run(InsertQueryJava.class); new ConcurrentRunner(threadNum).run(InsertQuery.class); } }
?
windows下jdk1.6.20 groovy1.8.0
通过运行结果发现了几点表面现象:
?
groovy的Sql,直接newInstance比new Sql(datasource)的性能要好
c3p0的DriverManagerDataSource要比spring jdbc的DriverManagerDataSource的性能要好
都编译成class文件后groovy的性能和java的性能几乎一样
?
下面是Runner结果
?
[concurrent] Starting test.InsertQueryJava
test.InsertQueryJava
log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog).
log4j:WARN Please initialize the log4j system properly.
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
testAll() - [4292.373611ms]
total: 1, failures:0 (GREEN) - 4331.556105ms
[concurrent] Starting test.InsertQuery
test.InsertQuery
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
testAll() - [6448.14313ms]
total: 1, failures:0 (GREEN) - 6686.467204ms
?
后面为了使得数据源连接池有个池的效果,我用了一个全局的DataSource,结果更吃惊。。groovy竟然效率更高
?
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
[concurrent] Starting test.InsertQuery