天天看點

通用池化架構GenericKeyedObjectPool性能測試

上次我測試了通用池化架構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

等待

線程數 執行次數(k) 單線程QPS
20 10 416
50 10 393
100 5 222
200 2 79
300 2 27

Have Fun ~ Tester !