Hikaricp和Druid对⽐
HikariCP和Druid对⽐
1、版本
mysql版本:8.0.27
HikariCP版本:3.2.0
Druid版本:1.1.9
2、对⽐结果
类别Druid HikariCP 获取和关闭Connection速度较慢较快
获取关闭Statement速度较慢较快
lru cache⽀持不⽀持
ps chace⽀持不⽀持
ExceptionSorter⽀持不⽀持
Filter扩展⽀持不⽀持
监控⽀持(jmx/log/http)⽀持(jmx/metrics)
诊断⽀持LogFilter不⽀持
连接泄露诊断logAbandoned不⽀持
SQL注⼊检查⽀持不⽀持
配置加密⽀持不⽀持
代码量较多较少
3、详细对⽐
1、获取和关闭连接速度对⽐
HikariCP的作者的brettwooldridge给出的获取Connection和获取Statement性能对⽐。(没有到对应的配置)
本地测试时,为了减少⽹络IO对测试结果的影响,数据库使⽤本地的mysql。两个连接池保持同样的配置:初始化连接数量10,最⼤连接数量20。
1、获取和关闭连接池
获取和关闭连接池时随着线程数量的上升,druid ops上升越来越慢。
线程数量hikaricp(ops/ms)druid(ops/ms)druid/hikaricp 114084.5978101.6150.575210991 226669.66215588.4560.584501446 454955.19827153.8270.494108437 898677.27549823.0810.504909372 16176690.56283694.7850.473679998原始数据:
TestHikariCP.openAndClose1 thrpt 10 14084.597 ± 263.273 ops/ms
TestHikariCP.openAndClose2 thrpt 10 26669.662 ± 1979.910 ops/ms
TestHikariCP.openAndClose4 thrpt 10 54955.198 ± 533.937 ops/ms
TestHikariCP.openAndClose8 thrpt 10 98677.275 ± 6270.774 ops/ms
TestHikariCP.openAndClose16 thrpt 10 176690.562 ± 6083.721 ops/ms
TestDruid.openAndClose1 thrpt 10 8101.615 ± 126.243 ops/ms
TestDruid.openAndClose2 thrpt 10 15588.456 ± 252.512 ops/ms
TestDruid.openAndClose4 thrpt 10 27153.827 ± 381.426 ops/ms
TestDruid.openAndClose8 thrpt 10 49823.081 ± 5481.129 ops/ms
TestDruid.openAndClose16 thrpt 10 83694.785 ± 12806.862 ops/ms
2、获取和关闭Statement
获取Statement时候,hikaricp略强于druid,但是并没有明显的差距。
线程数量hikaricp(ops/ms)druid(ops/ms)druid/hikaricp 13245.3053171.8870.977377165 25941.5545664.910.953439117 49402.5918139.1160.865624805 813509.17413538.226  1.002150539 1624027.37622007.9580.915953452
原始数据:
druid连接池配置详解TestHikariCP.openAndClose thrpt 10 3245.305 ± 39.683 ops/ms
TestHikariCP.openAndClose2 thrpt 10 5941.554 ± 100.023 ops/ms
TestHikariCP.openAndClose4 thrpt 10 9402.591 ± 125.380 ops/ms
TestHikariCP.openAndClose8 thrpt 10 13509.174 ± 216.521 ops/ms
TestHikariCP.openAndClose16 thrpt 10 24027.376 ± 971.336 ops/ms
TestDruid.openAndClose1 thrpt 10 3171.887 ± 73.370 ops/ms
TestDruid.openAndClose2 thrpt 10 5664.910 ± 137.471 ops/ms
TestDruid.openAndClose4 thrpt 10 8139.116 ± 205.789 ops/ms
TestDruid.openAndClose8 thrpt 10 13538.226 ± 68.399 ops/ms
TestDruid.openAndClose16 thrpt 10 22007.958 ± 1059.670 ops/ms
2、PScache
#druid⽀持PScache,配置如下:
poolPreparedStatements=true
maxOpenPreparedStatements=20
HikariCP不⽀持PScache,作者认为缓存Statement需要存储⼤量对象,并且PostgreSQL, Oracle, Derby, MySQL, DB2以及其他很多数据库的JDBC驱动都有实现PScache,在连接池上重复实现是不获益的,并且可能存在负⾯的影响。
3、代码量对⽐
Pool Files line size
HikariCP448798143,577 字节(140 KB)
Druid138********,715,961 字节(2.58 MB)
4、监控功能对⽐
Druid
开启页⾯监控
springboot使⽤druid监控可以通过在配置⽂件中配置StatFilter开启
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.abled= #是否启⽤StatFilter默认值false
spring.datasource.druid.web-stat-filter.url-pattern=
spring.datasource.lusions=
spring.datasource.druid.web-stat-filter.session-stat-enable=
spring.datasource.druid.web-stat-filter.session-stat-max-count=
spring.datasource.druid.web-stat-filter.principal-session-name=
spring.datasource.druid.web-stat-filter.principal-cookie-name=
spring.datasource.druid.web-stat-filter.profile-enable=
# StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
spring.datasource.abled= #是否启⽤StatViewServlet(监控页⾯)默认值为false(考虑到安全问题默认并未启动,如需启⽤建议设置密码或⽩名单以保障安全)
spring.datasource.druid.stat-view-servlet.url-pattern=
spring.datasource.set-enable=
spring.datasource.druid.stat-view-servlet.login-username=
spring.datasource.druid.stat-view-servlet.login-password=
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=
或者可以通过注⼊Bean的⽅式来开启监控
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean servletRegistrationBean =new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//设置ip⽩名单
servletRegistrationBean.addInitParameter("allow","");
//设置ip⿊名单,优先级⾼于⽩名单
servletRegistrationBean.addInitParameter("deny","");
//设置控制台管理⽤户
servletRegistrationBean.addInitParameter("loginUsername","root");
servletRegistrationBean.addInitParameter("loginPassword","root");
//是否可以重置数据
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean statFilter(){
//创建过滤器
FilterRegistrationBean filterRegistrationBean =new FilterRegistrationBean(new WebStatFilter());
/
/设置过滤器过滤路径
filterRegistrationBean.addUrlPatterns("/*");
//忽略过滤的形式
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
HikariCP
Metrics
HikariCP从2.2.0版本以后⽀持Metrics,收集了Wait(获取连接耗时信息),Usage(使⽤连接时间信息),TotalConnections(总连接数),IdleConnections(空闲连接数),ActiveConnections(活跃连接数),PendingConnections(等待连接数)等指标,需要调⽤HikariConfig或者HikariDataSource来开启使⽤。
@Bean
public DataSource dataSource(){
Properties dsProps =new Properties();
dsProps.setProperty("jdbcUrl", url);
dsProps.setProperty("username", username);
dsProps.setProperty("password", password);
dsProps.setProperty("driverClassName", driverClassName);
HikariConfig config =new HikariConfig(dsProps);
MetricRegistry metricRegistry =new MetricRegistry();
config.setMetricRegistry(metricRegistry);
Slf4jReporter reporter = Slf4jReporter.forRegistry(metricRegistry).build();
reporter.start(1, TimeUnit.MINUTES);
return new HikariDataSource(config);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。