使用JDBC插入多数据测试的3种实现方法
2010-11-04 20:06
806 查看
闲来无事,就想测试一下我的mysql插入数据的效率到底如何。
现在不清楚我的测试方法到位否,所以与大家分享一下,看看大家的意见。
测试的思路:
多次向表中插入1000条数据,看看每次使用了多少毫秒。现在我打算使用JDBC的方式去插入。
外话:
思路很简单,不过我这里使用了Spring JUnit,因为我上瘾了,非常地方便。想看看大家
是不是也这样子使用。所以我在后文中附贴出我一些关于JUnit Spring的配置代码,
想更多人来抛砖。
开始先看看表结构,非常简单的一个表,id是自动增长,ct里就是我会随加入内容的字段:
create table test (id int,ct chart);
Java代码
第一个我给出了所有代码,这里使用的jdbc批处理还有手工提交事务。
测试结果为:
Java代码
测试代码2:
Java代码
只给出与测试不同的地方,其他地方基本相同。
这里只是将batch方式换成直接executeUpdate.
输出结果 如下:
Java代码
使用Spring框架作为数据源:
Java代码
Java代码
testContext.xml文件:
Java代码
现在不清楚我的测试方法到位否,所以与大家分享一下,看看大家的意见。
测试的思路:
多次向表中插入1000条数据,看看每次使用了多少毫秒。现在我打算使用JDBC的方式去插入。
外话:
思路很简单,不过我这里使用了Spring JUnit,因为我上瘾了,非常地方便。想看看大家
是不是也这样子使用。所以我在后文中附贴出我一些关于JUnit Spring的配置代码,
想更多人来抛砖。
开始先看看表结构,非常简单的一个表,id是自动增长,ct里就是我会随加入内容的字段:
create table test (id int,ct chart);
Java代码
import mumu.junit.Base.BaseTest; public class MangInsertTest extends BaseTest { private static Logger log = Logger.getLogger(MangInsertTest.class); private static final int INSERT_COUNT = 5000; private static final int TEST_COUNT = 10; @Autowired private UnitTestDAO unitDao; @Test public void test() throws Exception{ Connection conn = null; try{ conn = unitDao.getConnection(); conn.setAutoCommit(false);//将事务改成手工提交 String inSql = "insert into mang(ct)value(?)"; for(int i = 0; i < TEST_COUNT; ++i){ long s = System.currentTimeMillis(); long d = 0; PreparedStatement inPs = conn.prepareStatement(inSql); for(int k = 0; k < INSERT_COUNT; ++k){ inPs.setString(1, "prefix string " + System.currentTimeMillis()); inPs.addBatch();//放入批处理 } inPs.executeBatch(); conn.commit();//记得要提交喔 inPs.close(); d = System.currentTimeMillis() - s; log.info("used time:" + d); } conn.setAutoCommit(true);//将conn改成自动提交 }catch(Exception excep){ throw excep; }finally{ if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } } } } }
第一个我给出了所有代码,这里使用的jdbc批处理还有手工提交事务。
测试结果为:
Java代码
INFO (MangInsertTest.java:42) - used time:822 INFO (MangInsertTest.java:42) - used time:522 INFO (MangInsertTest.java:42) - used time:540 INFO (MangInsertTest.java:42) - used time:531 INFO (MangInsertTest.java:42) - used time:500 INFO (MangInsertTest.java:42) - used time:541 INFO (MangInsertTest.java:42) - used time:500 INFO (MangInsertTest.java:42) - used time:521 INFO (MangInsertTest.java:42) - used time:490 INFO (MangInsertTest.java:42) - used time:721
测试代码2:
Java代码
conn = unitDao.getConnection(); conn.setAutoCommit(false);//依然使用手动提交 String inSql = "insert into mang(ct)value(?)"; for(int i = 0; i < TEST_COUNT; ++i){ long s = System.currentTimeMillis(); long d = 0; PreparedStatement inPs = conn.prepareStatement(inSql); for(int k = 0; k < INSERT_COUNT; ++k){ inPs.setString(1, "prefix string " + System.currentTimeMillis()); inPs.executeUpdate(); } conn.commit(); inPs.close(); d = System.currentTimeMillis() - s; log.info("used time:" + d);
只给出与测试不同的地方,其他地方基本相同。
这里只是将batch方式换成直接executeUpdate.
输出结果 如下:
Java代码
INFO (MangInsertTest2.java:41) - used time:692 INFO (MangInsertTest2.java:41) - used time:590 INFO (MangInsertTest2.java:41) - used time:531 INFO (MangInsertTest2.java:41) - used time:560 INFO (MangInsertTest2.java:41) - used time:681 INFO (MangInsertTest2.java:41) - used time:500 INFO (MangInsertTest2.java:41) - used time:511 INFO (MangInsertTest2.java:41) - used time:510 INFO (MangInsertTest2.java:41) - used time:683 INFO (MangInsertTest2.java:41) - used time:510
使用Spring框架作为数据源:
Java代码
package mumu.compass.unittest; import java.sql.Connection; import java.sql.SQLException; import org.springframework.jdbc.core.JdbcTemplate; public class UnitTestDAO { private JdbcTemplate jdbcTemplate; public Connection getConnection() throws SQLException{ return jdbcTemplate.getDataSource().getConnection(); } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } }
Java代码
package mumu.junit.Base; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class}) @Transactional @ContextConfiguration(locations={"classpath*:/testContext.xml"}) public class BaseTest{ }
testContext.xml文件:
Java代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="mumu.compass.unittest" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mutest" /> <property name="username" value="root" /> <property name="password" value="mysql" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="unitTestDao" class="mumu.compass.unittest.UnitTestDAO"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> </beans>
相关文章推荐
- 使用JDBC插入多数据测试的3种实现方法
- 使用JDBC向MySQL数据库批次插入10W条数据测试效率
- 使用JDBC插入大量数据的性能测试
- ajax-3种数据传输的实现方法(使用jQuery)
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
- 使用JDBC插入大量数据的性能测试
- 使用JDBC插入大量数据的性能测试
- JDBC 3种获得mysql插入数据的自增字段值的方法
- PHP使用3种方法实现数据采集
- php使用PDO事务配合表格读取大量数据插入操作实现方法
- JDBC 3种获得mysql插入数据的自增字段值的方法
- sparkSQL里 sql语句,dataframe,Thrift Server JDBC都可以实现对数据的查询,过滤等操作, 哪这3种情况分别是什么情况下使用
- PHP使用3种方法实现数据采集
- JDBC 3种获得mysql插入数据的自增字段值的方法
- jdbc、Mybatis插入数据主键回显的实现方法
- 使用JDBC插入大量数据的性能测试
- 使用jdbc获取插入数据时的主键的方法
- PHP使用3种方法实现数据采集
- Java使用JDBC向MySQL数据库批次插入10W条数据(测试效率)
- JDBC 3种获得mysql插入数据的自增字段值的方法