最近为了使用更加高效的connection jdbc pool,在网上发现有个叫hikariCP的貌似不错,里面有篇文章讲了关于配置pool size的魔法,不是越多越好,而是应该恰到好处。
https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing
考虑整体系统性能,考虑线程执行需要的等待时间,设计合理的线程数目。(Connection Pool Size)
我自己也做了一组测试。
测试描述:
对一个spring4+spring security3+hibernate4 (ehcache+hikariCP)+mysql的系统的某个业务方法(save user,insert new user) 性能测试,统计多线程多执行时候save方法的执行时间。
性能收集代码,在spring里面加入aop,统计方法执行时间
package com.elulian.CustomerSecurityManagementSystem.service;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PerfInterceptor implements MethodInterceptor {
private static final Logger logger = LoggerFactory.getLogger(PerfInterceptor.class);
private static ConcurrentHashMap<String, MethodStats> methodStats = new ConcurrentHashMap<String, MethodStats>();
public static Enumeration<MethodStats> getMethodStats(){
return methodStats.elements();
}
public static void clearMethodStats(){
methodStats.clear();
}
public static void printMethodStats(){
Enumeration<MethodStats> e = methodStats.elements();
while(e.hasMoreElements()){
logger.info(e.nextElement().toString());
}
}
/*
* performance monitor for service layer
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public Object invoke(MethodInvocation method) throws Throwable {
long start = System.nanoTime();
try {
return method.proceed();
} finally {
updateStats(method.getMethod().getName(),
(System.nanoTime() - start));
}
}
private void updateStats(String methodName, long elapsedTime) {
MethodStats stats = methodStats.get(methodName);
if (stats == null) {
stats = new MethodStats(methodName);
methodStats.put(methodName, stats);
}
stats.count++;
stats.totalTime += elapsedTime;
if (elapsedTime > stats.maxTime) {
stats.maxTime = elapsedTime;
}
if(elapsedTime < stats.minTime || 0 == stats.minTime){
stats.minTime = elapsedTime;
}
}
class MethodStats {
public String methodName;
public long count;
public long totalTime;
public long maxTime;
public long minTime;
public MethodStats(String methodName) {
this.methodName = methodName;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("method: ");
sb.append(met