replace custom pool implementation with apache's

This commit is contained in:
Jonathan Leibiusky
2010-11-21 18:16:31 -03:00
parent e7582644b1
commit 71eb4c5b4a
6 changed files with 236 additions and 238 deletions

View File

@@ -1,8 +1,9 @@
package redis.clients.jedis.tests;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.NoSuchElementException;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.junit.Assert;
import org.junit.Test;
@@ -14,12 +15,9 @@ public class JedisPoolTest extends Assert {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@Test
public void checkConnections() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000);
pool.setResourcesNumber(10);
pool.init();
Jedis jedis = pool.getResource(200);
public void checkConnections() throws Exception {
JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port, 2000);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
@@ -28,12 +26,9 @@ public class JedisPoolTest extends Assert {
}
@Test
public void checkConnectionWithDefaultPort() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(10);
pool.init();
Jedis jedis = pool.getResource(200);
public void checkConnectionWithDefaultPort() throws Exception {
JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
@@ -42,17 +37,14 @@ public class JedisPoolTest extends Assert {
}
@Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
Jedis jedis = pool.getResource(200);
public void checkJedisIsReusedWhenReturned() throws Exception {
JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "0");
pool.returnResource(jedis);
jedis = pool.getResource(200);
jedis = pool.getResource();
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
@@ -60,35 +52,31 @@ public class JedisPoolTest extends Assert {
}
@Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
Jedis jedis = pool.getResource(200);
public void checkPoolRepairedWhenJedisIsBroken() throws Exception {
JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.quit();
pool.returnBrokenResource(jedis);
jedis = pool.getResource(200);
jedis = pool.getResource();
jedis.auth("foobared");
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
@Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
Jedis jedis = pool.getResource(200);
@Test(expected = NoSuchElementException.class)
public void checkPoolOverflow() throws Exception {
Config config = new Config();
config.maxActive = 1;
config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
JedisPool pool = new JedisPool(config, hnp.host, hnp.port);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "0");
Jedis newJedis = pool.getResource(200);
Jedis newJedis = pool.getResource();
newJedis.auth("foobared");
newJedis.incr("foo");
}

View File

@@ -4,8 +4,10 @@ import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.NoSuchElementException;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -42,12 +44,9 @@ public class ShardedJedisPoolTest extends Assert {
}
@Test
public void checkConnections() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(10);
pool.init();
ShardedJedis jedis = pool.getResource(200);
public void checkConnections() throws Exception {
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource();
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
@@ -55,12 +54,9 @@ public class ShardedJedisPoolTest extends Assert {
}
@Test
public void checkConnectionWithDefaultPort() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(1);
pool.init();
ShardedJedis jedis = pool.getResource(200);
public void checkConnectionWithDefaultPort() throws Exception {
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource();
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
@@ -68,48 +64,43 @@ public class ShardedJedisPoolTest extends Assert {
}
@Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(1);
pool.init();
ShardedJedis jedis = pool.getResource(200);
public void checkJedisIsReusedWhenReturned() throws Exception {
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource();
jedis.set("foo", "0");
pool.returnResource(jedis);
jedis = pool.getResource(200);
jedis = pool.getResource();
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
@Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(1);
pool.init();
ShardedJedis jedis = pool.getResource(200);
public void checkPoolRepairedWhenJedisIsBroken() throws Exception {
ShardedJedisPool pool = new ShardedJedisPool(new Config(), shards);
ShardedJedis jedis = pool.getResource();
jedis.disconnect();
pool.returnBrokenResource(jedis);
jedis = pool.getResource(200);
jedis = pool.getResource();
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
@Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(1);
pool.init();
@Test(expected = NoSuchElementException.class)
public void checkPoolOverflow() throws Exception {
Config config = new Config();
config.maxActive = 1;
config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
ShardedJedis jedis = pool.getResource(200);
ShardedJedisPool pool = new ShardedJedisPool(config, shards);
ShardedJedis jedis = pool.getResource();
jedis.set("foo", "0");
ShardedJedis newJedis = pool.getResource(200);
ShardedJedis newJedis = pool.getResource();
newJedis.incr("foo");
}
}

View File

@@ -1,29 +1,21 @@
package redis.clients.jedis.tests.benchmark;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.util.FixedResourcePool;
public class PoolBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 100000;
public static void main(String[] args) throws UnknownHostException,
IOException, TimeoutException, InterruptedException {
Logger logger = Logger.getLogger(FixedResourcePool.class.getName());
logger.setLevel(Level.OFF);
public static void main(String[] args) throws Exception {
Jedis j = new Jedis(hnp.host, hnp.port);
j.connect();
j.auth("foobared");
@@ -37,12 +29,9 @@ public class PoolBenchmark {
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
private static void withPool() throws InterruptedException {
final JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000,
"foobared");
pool.setResourcesNumber(50);
pool.setDefaultPoolWait(1000000);
pool.init();
private static void withPool() throws Exception {
final JedisPool pool = new JedisPool(new Config(), hnp.host, hnp.port,
2000, "foobared");
List<Thread> tds = new ArrayList<Thread>();
final AtomicInteger ind = new AtomicInteger();