上次我测试了通用池化框架GenericObjectPool性能测试,效果还行,对后面使用commons-pool2框架的使用提供了非常有效的参考依据。

对于另外一个更复杂的GenericKeyedObjectPool也得安排上了,这就献上。

硬件软件配置&准备工作&可池化对象

这部分内容与上期相同,这里不再赘述了。

池化工厂

这里用到了org.apache.commons.pool2.BaseKeyedPooledObjectFactory<Integer, FunTesterPooled>,下面分享一下具体的代码。

    /**
     * 池化工厂
     */
    private static class FunFactory extends BaseKeyedPooledObjectFactory<Integer, FunTesterPooled> {


        @Override
        FunTesterPooled create(Integer key) throws Exception {
            def pooled = new FunTesterPooled()
            pooled.setAge(key)
            return pooled
        }

        @Override
        PooledObject<FunTesterPooled> wrap(FunTesterPooled value) {
            return new DefaultPooledObject<FunTesterPooled>(value)
        }

        @Override
        void destroyObject(Integer key, PooledObject<FunTesterPooled> p, DestroyMode destroyMode) throws Exception {
            p.getObject().setAge(0)//资源回收
            super.destroyObject(key, p, destroyMode)
        }
    }

对象池

虽然这个GenericKeyedObjectPool理论上是可以存储不同的对象的,但是这个在创建的时候还是需要确定一个可池化对象类型。所以后面所有创建的对象必需这个可池化对象类或者子类。

    /**
     * 初始化对象池
     * @return
     */
    static def initPool() {
        def config = new GenericKeyedObjectPoolConfig<FunTesterPooled>()
        config.setMaxTotal(thread * 2)
        config.setMinIdlePerKey(10)
        config.setMaxIdlePerKey(100)
        config.setMaxWait(Duration.ofMillis(1000))
        config.setMaxIdlePerKey(thread)
        config.setMaxIdlePerKey(10)
        config.setMinIdlePerKey(2)
        return new GenericKeyedObjectPool<Integer, FunTesterPooled>(new FunFactory(), config)
    }

这里的设置分成了两类,就是每个对象池和总对象池的各类参数设置。

性能测试用例

跟上期的用例很像,只是请求参数增加了key,这里return的时候也需要增加key参数。测试用例如下:


    static GenericKeyedObjectPool<Integer, FunTesterPooled> pool

    static def desc = "池化框架性能测试"

    static int times = 200

    static int thread = 3

    public static void main(String[] args) {
        this.pool = initPool()
        ThreadBase.COUNT = true
        RUNUP_TIME = 0
        POOL_SIZE = thread
        thread.times {
            pool.addObjects(it, thread)
        }
        output("对象创建完毕 创建数量${pool.getNumIdle()}")
        new Concurrent(new FunTester(), thread, desc).start()
        pool.close()
    }

    private static class FunTester extends FixedThread {


        FunTester() {
            super(null, times, true)
        }

        @Override
        protected void doing() throws Exception {
            def randomInt = getRandomInt(thread)
            def object = pool.borrowObject(randomInt)
            pool.returnObject(randomInt, object)
        }

        @Override
        FunTester clone() {
            return new FunTester()
        }
    }

测试结果

用例设计也是跟上期的文章一样,为了尽可能有对比价值,使用了尽可能相同的参数。

无等待

线程数 执行次数(万) QPS
1 300 189501
2 300 322603
5 300 120334
10 100 96861
20 50 81440

可以看出,一旦线程数增加,QPS就是降低非常快,线程较低的时候性能跟GenericObjectPool相差无几,这很可能是java.util.concurrent.ConcurrentHashMap导致的。有时间我再对java.util.concurrent.ConcurrentHashMap进行性能测试。

等待

使用了休眠2ms的配置。

线程数 执行次数(k) 单线程QPS
20 10 416
50 10 393
100 5 222
200 2 79
300 2 27

后面就不测了,再测可能回归个位数了。结论如上,线程数增加的的话,粗略估计100以上,org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig的性能就会变得奇差无比,可能是多种原因导致的。更不用提线程数增加500以后,会遇到org.apache.commons.pool2.impl.LinkedBlockingDequejava.util.concurrent.atomic.AtomicLong性能大幅下降。看来以后还是不能简单使用对象池代替创建对象。

Have Fun ~ Tester !