连接池c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool对比测试

来源:互联网 发布:淘宝客快速打造爆款 编辑:程序博客网 时间:2024/05/18 00:46

这次所要做的测试是比较几种我们常用的数据库连接池的性能,他们分别是:c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool这四种,测试将采用统一的参数配置力求比较“公平”的体现统一水平下几种连接池的不同,有网友回复说测试不公平会互相干扰,那我就把代码分开,代码是死的人是活的,做事情不动脑只能吃别人剩下的,世界上没有绝对公平的事情,我在此只提供了一个思路,更多的测试还需要你自己去完成。

 

        1.创建类TestDAO,封装统一的查询方法 :

Java代码  收藏代码
  1. import java.sql.Connection;  
  2. import java.sql.ResultSet;  
  3. import java.sql.SQLException;  
  4. import java.sql.Statement;  
  5.   
  6. public class TestDAO {  
  7.   
  8.     private final static String sql = "SELECT * FROM USER u WHERE u.USR_ID=9999";  
  9.   
  10.     public void query(Connection conn) {  
  11.         try {  
  12.             Statement st = conn.createStatement();  
  13.             ResultSet result = st.executeQuery(sql);  
  14.             result.close();  
  15.             st.close();  
  16.             conn.close();  
  17.         } catch (SQLException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21. }  

 

 

        2.创建测试类TestMain,其中包含:统一参数配置属性、获取各种连接池数据源方法、各种数据源调用方法等,下面分别是各种数据源的测试Main方法,开始测试之前做100次查询操作以初始化连接池并起到稳定测试结果作用:

 

        (1)c3p0测试代码:

Java代码  收藏代码
  1. import java.beans.PropertyVetoException;  
  2. import java.io.IOException;  
  3. import java.sql.SQLException;  
  4. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  5.   
  6. public class C3p0Test {  
  7.   
  8.     // 数据库驱动名称  
  9.     final static String driver = "com.mysql.jdbc.Driver";  
  10.     // 数据库连接地址  
  11.     final static String jdbcUrl = "jdbc:mysql://192.168.0.1:3306/test";  
  12.     // 数据库用户名  
  13.     final static String user = "dba";  
  14.     // 数据库密码  
  15.     final static String passwd = "dba";  
  16.     // 连接池初始化大小  
  17.     final static int initialSize = 5;  
  18.     // 连接池最小空闲  
  19.     final static int minPoolSize = 10;  
  20.     // 连接池最大连接数量  
  21.     final static int maxPoolSize = 50;  
  22.     // 最小逐出时间,100秒  
  23.     final static int maxIdleTime = 100000;  
  24.     // 连接失败重试次数  
  25.     final static int retryAttempts = 10;  
  26.     // 当连接池连接耗尽时获取连接数  
  27.     final static int acquireIncrement = 5;  
  28.     // c3p0数据源  
  29.     final static ComboPooledDataSource c3p0DataSource = getC3p0DataSource();  
  30.   
  31.     // 查询次数  
  32.     final static int count = 10;  
  33.   
  34.     /** 
  35.      * 测试方式: 每种数据源配置信息尽量相同,以求结果更加准确 
  36.      * 每种数据源做10次、100次、500次、1000次、2000次、4000次、8000次查询操作 每种查询重复100次,查看100次执行时间的波动图 
  37.      * @param args 
  38.      * @throws IOException 
  39.      * @throws SQLException 
  40.      */  
  41.     public static void main(String[] args) throws IOException, SQLException {  
  42.   
  43.         TestDAO testDAO = new TestDAO();  
  44.         System.out.println("查询次数为:" + count);  
  45.         System.out.println();  
  46.         System.out.println("==========================c3p0 测试开始==========================");  
  47.         // 测试c3p0  
  48.         for (int i = 0; i < 100; i++) {  
  49.             queryC3p0(testDAO, c3p0DataSource, count);  
  50.         }  
  51.         System.out.println("==========================c3p0 测试结束==========================");  
  52.     }  
  53.   
  54.     /** 
  55.      * c3p0测试 
  56.      * @param testDAO 
  57.      * @param ds 
  58.      * @param count 
  59.      * @throws SQLException 
  60.      */  
  61.     public static void queryC3p0(TestDAO testDAO, ComboPooledDataSource ds, int count) throws SQLException {  
  62.         // 查询100次以初始化连接池  
  63.         for (int i = 0; i < 100; i++) {  
  64.             testDAO.query(ds.getConnection());  
  65.         }  
  66.         // 开始时间  
  67.         long startMillis = System.currentTimeMillis();  
  68.         // 循环查询  
  69.         for (int i = 0; i < count; i++) {  
  70.             testDAO.query(ds.getConnection());  
  71.         }  
  72.         // 结束时间  
  73.         long endMillis = System.currentTimeMillis();  
  74.         // 输出结束时间  
  75.         System.out.println(endMillis - startMillis);  
  76.     }  
  77.   
  78.     /** 
  79.      * 获取c3p0数据源 
  80.      * @throws PropertyVetoException 
  81.      */  
  82.     public static ComboPooledDataSource getC3p0DataSource() {  
  83.         // 设置参数  
  84.         ComboPooledDataSource cpds = new ComboPooledDataSource();  
  85.         try {  
  86.             cpds.setDriverClass(driver);  
  87.         } catch (PropertyVetoException e) {  
  88.             e.printStackTrace();  
  89.         }  
  90.         cpds.setJdbcUrl(jdbcUrl);  
  91.         cpds.setUser(user);  
  92.         cpds.setPassword(passwd);  
  93.         cpds.setInitialPoolSize(initialSize);  
  94.         cpds.setMinPoolSize(minPoolSize);  
  95.         cpds.setMaxPoolSize(maxPoolSize);  
  96.         cpds.setMaxIdleTime(maxIdleTime);  
  97.         cpds.setAcquireRetryAttempts(retryAttempts);  
  98.         cpds.setAcquireIncrement(acquireIncrement);  
  99.         cpds.setTestConnectionOnCheckin(false);  
  100.         cpds.setTestConnectionOnCheckout(false);  
  101.         return cpds;  
  102.     }  
  103. }  

 

 

        (2)Proxool测试代码: 

Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.sql.SQLException;  
  3. import org.logicalcobwebs.proxool.ProxoolDataSource;  
  4.   
  5. public class ProxoolTest {  
  6.   
  7.     // 数据库驱动名称  
  8.     final static String driver = "com.mysql.jdbc.Driver";  
  9.     // 数据库连接地址  
  10.     final static String jdbcUrl = "jdbc:mysql://192.168.0.1:3306/test";  
  11.     // 数据库用户名  
  12.     final static String user = "dba";  
  13.     // 数据库密码  
  14.     final static String passwd = "dba";  
  15.     // 连接池初始化大小  
  16.     final static int initialSize = 5;  
  17.     // 连接池最小空闲  
  18.     final static int minPoolSize = 10;  
  19.     // 连接池最大连接数量  
  20.     final static int maxPoolSize = 50;  
  21.     // 最小逐出时间,100秒  
  22.     final static int maxIdleTime = 100000;  
  23.     // 连接失败重试次数  
  24.     final static int retryAttempts = 10;  
  25.     // 当连接池连接耗尽时获取连接数  
  26.     final static int acquireIncrement = 5;  
  27.     // Proxool数据源  
  28.     final static ProxoolDataSource proxoolDataSource = getProxoolDataSource();  
  29.     // 查询次数  
  30.     final static int count = 10;  
  31.   
  32.     /** 
  33.      * 测试方式: 每种数据源配置信息尽量相同,以求结果更加准确 
  34.      * 每种数据源做10次、100次、500次、1000次、2000次、4000次、8000次查询操作 每种查询重复100次,查看100次执行时间的波动图 
  35.      * @param args 
  36.      * @throws IOException 
  37.      * @throws SQLException 
  38.      */  
  39.     public static void main(String[] args) throws IOException, SQLException {  
  40.   
  41.         TestDAO testDAO = new TestDAO();  
  42.         System.out.println("查询次数为:" + count);  
  43.         System.out.println();  
  44.         System.out.println("==========================Proxool 测试开始==========================");  
  45.         // 测试Proxool  
  46.         for (int i = 0; i < 100; i++) {  
  47.             queryProxxool(testDAO, proxoolDataSource, count);  
  48.         }  
  49.         System.out.println("==========================Proxool 测试结束==========================");  
  50.     }  
  51.   
  52.     /** 
  53.      * Proxxool测试 
  54.      * @param testDAO 
  55.      * @param ds 
  56.      * @param count 
  57.      * @throws SQLException 
  58.      */  
  59.     public static void queryProxxool(TestDAO testDAO, ProxoolDataSource ds, int count) throws SQLException {  
  60.         // 查询100次以初始化连接池  
  61.         for (int i = 0; i < 100; i++) {  
  62.             testDAO.query(ds.getConnection());  
  63.         }  
  64.         // 开始时间  
  65.         long startMillis = System.currentTimeMillis();  
  66.         // 循环查询  
  67.         for (int i = 0; i < count; i++) {  
  68.             testDAO.query(ds.getConnection());  
  69.         }  
  70.         // 结束时间  
  71.         long endMillis = System.currentTimeMillis();  
  72.         // 输出结束时间  
  73.         System.out.println(endMillis - startMillis);  
  74.     }  
  75.   
  76.     /** 
  77.      * 获取Proxool数据源 
  78.      * @return 
  79.      */  
  80.     public static ProxoolDataSource getProxoolDataSource() {  
  81.         ProxoolDataSource pds = new ProxoolDataSource();  
  82.         pds.setAlias("mysql");  
  83.         pds.setUser(user);  
  84.         pds.setPassword(passwd);  
  85.         pds.setDriverUrl(jdbcUrl);  
  86.         pds.setDriver(driver);  
  87.         pds.setMaximumActiveTime(maxIdleTime);  
  88.         pds.setMaximumConnectionCount(maxPoolSize);  
  89.         pds.setMinimumConnectionCount(initialSize);  
  90.         pds.setPrototypeCount(minPoolSize);  
  91.         pds.setTestBeforeUse(false);  
  92.         pds.setTestAfterUse(false);  
  93.         return pds;  
  94.     }  
  95.   
  96. }  

 

 

        (3)Druid测试代码:

Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.sql.SQLException;  
  3. import com.alibaba.druid.pool.DruidDataSource;  
  4.   
  5. public class DruidTest {  
  6.   
  7.     // 数据库驱动名称  
  8.     final static String driver = "com.mysql.jdbc.Driver";  
  9.     // 数据库连接地址  
  10.     final static String jdbcUrl = "jdbc:mysql://192.168.0.1:3306/test";  
  11.     // 数据库用户名  
  12.     final static String user = "dba";  
  13.     // 数据库密码  
  14.     final static String passwd = "dba";  
  15.     // 连接池初始化大小  
  16.     final static int initialSize = 5;  
  17.     // 连接池最小空闲  
  18.     final static int minPoolSize = 10;  
  19.     // 连接池最大连接数量  
  20.     final static int maxPoolSize = 50;  
  21.     // 最小逐出时间,100秒  
  22.     final static int maxIdleTime = 100000;  
  23.     // 连接失败重试次数  
  24.     final static int retryAttempts = 10;  
  25.     // 当连接池连接耗尽时获取连接数  
  26.     final static int acquireIncrement = 5;  
  27.     // Druid数据源  
  28.     final static DruidDataSource druidDataSource = getDruidDataSource();  
  29.     // 查询次数  
  30.     final static int count = 10;  
  31.   
  32.     /** 
  33.      * 测试方式: 每种数据源配置信息尽量相同,以求结果更加准确 
  34.      * 每种数据源做10次、100次、500次、1000次、2000次、4000次、8000次查询操作 每种查询重复100次,查看100次执行时间的波动图 
  35.      * @param args 
  36.      * @throws IOException 
  37.      * @throws SQLException 
  38.      */  
  39.     public static void main(String[] args) throws IOException, SQLException {  
  40.   
  41.         TestDAO testDAO = new TestDAO();  
  42.         System.out.println("查询次数为:" + count);  
  43.         System.out.println();  
  44.         System.out.println("==========================Druid 测试开始==========================");  
  45.         // 测试Druid  
  46.         for (int i = 0; i < 100; i++) {  
  47.             queryDruid(testDAO, druidDataSource, count);  
  48.         }  
  49.         System.out.println("==========================Druid 测试结束==========================");  
  50.     }  
  51.   
  52.     /** 
  53.      * Druid测试 
  54.      * @param testDAO 
  55.      * @param ds 
  56.      * @param count 
  57.      * @throws SQLException 
  58.      */  
  59.     public static void queryDruid(TestDAO testDAO, DruidDataSource ds, int count) throws SQLException {  
  60.         // 查询100次以初始化连接池  
  61.         for (int i = 0; i < 100; i++) {  
  62.             testDAO.query(ds.getConnection());  
  63.         }  
  64.         // 开始时间  
  65.         long startMillis = System.currentTimeMillis();  
  66.         // 循环查询  
  67.         for (int i = 0; i < count; i++) {  
  68.             testDAO.query(ds.getConnection());  
  69.         }  
  70.         // 结束时间  
  71.         long endMillis = System.currentTimeMillis();  
  72.         // 输出结束时间  
  73.         System.out.println(endMillis - startMillis);  
  74.     }  
  75.   
  76.     /** 
  77.      * 获取Druid数据源 
  78.      * @return 
  79.      */  
  80.     public static DruidDataSource getDruidDataSource() {  
  81.         DruidDataSource dds = new DruidDataSource();  
  82.         dds.setUsername(user);  
  83.         dds.setUrl(jdbcUrl);  
  84.         dds.setPassword(passwd);  
  85.         dds.setDriverClassName(driver);  
  86.         dds.setInitialSize(initialSize);  
  87.         dds.setMaxActive(maxPoolSize);  
  88.         dds.setMaxWait(maxIdleTime);  
  89.         dds.setTestWhileIdle(false);  
  90.         dds.setTestOnReturn(false);  
  91.         dds.setTestOnBorrow(false);  
  92.         return dds;  
  93.     }  
  94.   
  95. }  

 

 

        (4)Tomcat Jdbc Pool测试代码:

Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.sql.SQLException;  
  3. import org.apache.tomcat.jdbc.pool.DataSource;  
  4.   
  5. public class TomcatTest {  
  6.   
  7.     // 数据库驱动名称  
  8.     final static String driver = "com.mysql.jdbc.Driver";  
  9.     // 数据库连接地址  
  10.     final static String jdbcUrl = "jdbc:mysql://192.168.0.1:3306/test";  
  11.     // 数据库用户名  
  12.     final static String user = "dba";  
  13.     // 数据库密码  
  14.     final static String passwd = "dba";  
  15.     // 连接池初始化大小  
  16.     final static int initialSize = 5;  
  17.     // 连接池最小空闲  
  18.     final static int minPoolSize = 10;  
  19.     // 连接池最大连接数量  
  20.     final static int maxPoolSize = 50;  
  21.     // 最小逐出时间,100秒  
  22.     final static int maxIdleTime = 100000;  
  23.     // 连接失败重试次数  
  24.     final static int retryAttempts = 10;  
  25.     // 当连接池连接耗尽时获取连接数  
  26.     final static int acquireIncrement = 5;  
  27.     // Tomcat Jdbc Pool数据源  
  28.     final static DataSource tomcatDataSource = getTomcatDataSource();  
  29.     // 查询次数  
  30.     final static int count = 100;  
  31.   
  32.     /** 
  33.      * 测试方式: 每种数据源配置信息尽量相同,以求结果更加准确 
  34.      * 每种数据源做10次、100次、500次、1000次、2000次、4000次、8000次查询操作 每种查询重复100次,查看100次执行时间的波动图 
  35.      * @param args 
  36.      * @throws IOException 
  37.      * @throws SQLException 
  38.      */  
  39.     public static void main(String[] args) throws IOException, SQLException {  
  40.   
  41.         TestDAO testDAO = new TestDAO();  
  42.         System.out.println("查询次数为:" + count);  
  43.         System.out.println();  
  44.         System.out.println("==========================Tomcat Jdbc Pool 测试开始==========================");  
  45.         // 测试Tomcat Jdbc Pool  
  46.         for (int i = 0; i < 100; i++) {  
  47.             queryTomcatJDBC(testDAO, tomcatDataSource, count);  
  48.         }  
  49.         System.out.println("==========================Tomcat Jdbc Pool 测试结束==========================");  
  50.     }  
  51.   
  52.     /** 
  53.      * Tomcat Jdbc Pool测试 
  54.      * @param testDAO 
  55.      * @param ds 
  56.      * @param count 
  57.      * @throws SQLException 
  58.      */  
  59.     public static void queryTomcatJDBC(TestDAO testDAO, DataSource ds, int count) throws SQLException {  
  60.         // 查询100次以初始化连接池  
  61.         for (int i = 0; i < 100; i++) {  
  62.             testDAO.query(ds.getConnection());  
  63.         }  
  64.         // 开始时间  
  65.         long startMillis = System.currentTimeMillis();  
  66.         // 循环查询  
  67.         for (int i = 0; i < count; i++) {  
  68.             testDAO.query(ds.getConnection());  
  69.         }  
  70.         // 结束时间  
  71.         long endMillis = System.currentTimeMillis();  
  72.         // 输出结束时间  
  73.         System.out.println(endMillis - startMillis);  
  74.     }  
  75.   
  76.     /** 
  77.      * 获取Apache tomcat jdbc pool数据源 
  78.      * @return 
  79.      */  
  80.     public static DataSource getTomcatDataSource() {  
  81.         DataSource ds = new DataSource();  
  82.         ds.setUrl(jdbcUrl);  
  83.         ds.setUsername(user);  
  84.         ds.setPassword(passwd);  
  85.         ds.setDriverClassName(driver);  
  86.         ds.setInitialSize(initialSize);  
  87.         ds.setMaxIdle(minPoolSize);  
  88.         ds.setMaxActive(maxPoolSize);  
  89.         ds.setTestWhileIdle(false);  
  90.         ds.setTestOnBorrow(false);  
  91.         ds.setTestOnConnect(false);  
  92.         ds.setTestOnReturn(false);  
  93.         return ds;  
  94.     }  
  95. }  

               

 

 

        3.将测试结果粘贴到excel中,生成图表进行对比,可以很直观的看出几种数据源的性能差异,本文底部有此次测试的结果文档。

 

 

        以下就是此次测试结果(本结果只供参考,更具体更准确的测试可以自行进行):

 

        1.测试次数为10次时:



        平均用时(单位:毫秒):

c3p032.26Proxool33.42Druid30.43Tomcat Jdbc Pool37.61

 

 

        2.测试次数为100次时:



        平均用时(单位:毫秒):

c3p0409.94Proxool447.49Druid382.7Tomcat Jdbc Pool386.3

 

 

        3.测试次数为500次时:


        平均用时(单位:毫秒):

c3p01700.95Proxool2053.85Druid1777.36Tomcat Jdbc Pool1749.02

 

 

        4.测试次数为1000次时:



        平均用时(单位:毫秒):

c3p0 3549.29Proxool 3435.8Druid 3167.59Tomcat Jdbc Pool 3162.25

 

        因为测试耗时很长,所以我只做到了1000次查询测试,感兴趣的朋友可以继续更大规模的测试或修改相应参数来符合项目自身情况,这里不比去追究测试数据的准确性,比较测试程度还不够,且存在很大的偶然性,大家可以修改以上代码进行更精准的测试,只需要将数据导入到本文末尾的excel表格中即可生成相应测试图表。

        很多人总是会回复在问“你的测试准不准啊?”,“你的测试一点都不准!”等等,其实我想说的是:不要太在意别人的结果,重要的是自己去做!

0 0
原创粉丝点击