Search in sources :

Example 1 with KeyedObjectPool

use of org.apache.commons.pool.KeyedObjectPool in project voldemort by voldemort.

the class ResourcePoolPerfTest method main.

public static void main(String[] args) throws Exception {
    final int numKeys = 10;
    final int numThreads = 10;
    final int numRequests = 10000000;
    NumberFormat format = NumberFormat.getInstance();
    format.setMaximumFractionDigits(2);
    for (int poolSize : new int[] { 1, 5, 10 }) {
        System.out.println("Perf test for voldemort pool with numThreads = " + numThreads + ", poolSize = " + poolSize + ":");
        final KeyedResourcePool<Integer, String> pool = KeyedResourcePool.create(new StringResourceFactory(), new ResourcePoolConfig().setMaxPoolSize(poolSize).setIsFair(true));
        PerformanceTest test = new PerformanceTest() {

            @Override
            public void doOperation(int id) throws Exception {
                Integer key = id % numKeys;
                String s = pool.checkout(key);
                pool.checkin(key, s);
            }
        };
        test.run(numRequests, numThreads);
        test.printStats();
        System.out.println();
    }
    System.out.println("--------------------------------------");
    System.out.println();
    for (int poolSize : new int[] { 1, 5, 10 }) {
        System.out.println("Perf test for commons pool with numThreads = " + numThreads + ", poolSize = " + poolSize + ":");
        GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
        config.maxActive = poolSize;
        config.testOnBorrow = true;
        config.whenExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK;
        config.maxWait = 10000;
        StringPoolableObjectFactory objFactory = new StringPoolableObjectFactory();
        final KeyedObjectPool pool = new GenericKeyedObjectPool(objFactory, config);
        PerformanceTest test = new PerformanceTest() {

            @Override
            public void doOperation(int id) throws Exception {
                Integer key = id % numKeys;
                String s = (String) pool.borrowObject(key);
                pool.returnObject(key, s);
            }
        };
        test.run(numRequests, numThreads);
        test.printStats();
        System.out.println();
    }
}
Also used : GenericKeyedObjectPool(org.apache.commons.pool.impl.GenericKeyedObjectPool) ResourcePoolConfig(voldemort.utils.pool.ResourcePoolConfig) KeyedObjectPool(org.apache.commons.pool.KeyedObjectPool) GenericKeyedObjectPool(org.apache.commons.pool.impl.GenericKeyedObjectPool) NumberFormat(java.text.NumberFormat) ResourcePoolConfig(voldemort.utils.pool.ResourcePoolConfig)

Aggregations

NumberFormat (java.text.NumberFormat)1 KeyedObjectPool (org.apache.commons.pool.KeyedObjectPool)1 GenericKeyedObjectPool (org.apache.commons.pool.impl.GenericKeyedObjectPool)1 ResourcePoolConfig (voldemort.utils.pool.ResourcePoolConfig)1