This commit is contained in:
Jonathan Leibiusky
2010-09-15 14:44:56 -03:00
11 changed files with 220 additions and 110 deletions

15
pom.xml
View File

@@ -12,6 +12,11 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<redis-hosts>localhost:6379,localhost:6380</redis-hosts>
</properties>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
@@ -23,6 +28,16 @@
<target>1.6</target> <target>1.6</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<systemPropertyVariables>
<redis-hosts>${redis-hosts}</redis-hosts>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@@ -0,0 +1,58 @@
package redis.clients.jedis.tests;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.Protocol;
public class HostAndPortUtil {
private static List<HostAndPort> hostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(2);
static {
final HostAndPort defaulthnp1 = new HostAndPort();
defaulthnp1.host = "localhost";
defaulthnp1.port = Protocol.DEFAULT_PORT;
hostAndPortList.add(defaulthnp1);
final HostAndPort defaulthnp2 = new HostAndPort();
defaulthnp2.host = "localhost";
defaulthnp2.port = Protocol.DEFAULT_PORT + 1;
hostAndPortList.add(defaulthnp2);
final String envHosts = System.getProperty("redis-hosts");
if (null != envHosts && 0 < envHosts.length()) {
final String[] hostDefs = envHosts.split(",");
if (null != hostDefs && 2 <= hostDefs.length) {
hostAndPortList = new ArrayList<HostAndPortUtil.HostAndPort>(hostDefs.length);
for(String hostDef : hostDefs) {
final String[] hostAndPort = hostDef.split(":");
if (null != hostAndPort && 2 == hostAndPort.length) {
final HostAndPort hnp = new HostAndPort();
hnp.host = hostAndPort[0];
try {
hnp.port = Integer.parseInt(hostAndPort[1]);
} catch(final NumberFormatException nfe){
hnp.port = Protocol.DEFAULT_PORT;
}
hostAndPortList.add(hnp);
}
}
}
}
final StringBuilder strb = new StringBuilder("Redis hosts to be used : ");
for(HostAndPort hnp : hostAndPortList){
strb.append('[').append(hnp.host).append(':').append(hnp.port).append(']').append(' ');
}
System.out.println(strb);
}
public static List<HostAndPort> getRedisServers() {
return hostAndPortList;
}
public static class HostAndPort {
public String host;
public int port;
}
}

View File

@@ -8,83 +8,88 @@ import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Protocol; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class JedisPoolTest extends Assert { public class JedisPoolTest extends Assert {
@Test private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
public void checkConnections() throws TimeoutException {
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT, @Test
2000, "foobared"); public void checkConnections() throws TimeoutException {
pool.setResourcesNumber(10); JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000);
pool.init(); pool.setResourcesNumber(10);
pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.set("foo", "bar"); jedis.auth("foobared");
assertEquals("bar", jedis.get("foo")); jedis.set("foo", "bar");
pool.returnResource(jedis); assertEquals("bar", jedis.get("foo"));
pool.destroy(); pool.returnResource(jedis);
} pool.destroy();
}
@Test @Test
public void checkConnectionWithDefaultPort() throws TimeoutException { public void checkConnectionWithDefaultPort() throws TimeoutException {
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT, JedisPool pool = new JedisPool(hnp.host, hnp.port);
2000, "foobared"); pool.setResourcesNumber(10);
pool.setResourcesNumber(10); pool.init();
pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.set("foo", "bar"); jedis.auth("foobared");
assertEquals("bar", jedis.get("foo")); jedis.set("foo", "bar");
pool.returnResource(jedis); assertEquals("bar", jedis.get("foo"));
pool.destroy(); pool.returnResource(jedis);
} pool.destroy();
}
@Test @Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException { public void checkJedisIsReusedWhenReturned() throws TimeoutException {
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT, JedisPool pool = new JedisPool(hnp.host, hnp.port);
2000, "foobared"); pool.setResourcesNumber(1);
pool.setResourcesNumber(1); pool.init();
pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.set("foo", "0"); jedis.auth("foobared");
pool.returnResource(jedis); jedis.set("foo", "0");
pool.returnResource(jedis);
jedis = pool.getResource(200); jedis = pool.getResource(200);
jedis.incr("foo"); jedis.auth("foobared");
pool.returnResource(jedis); jedis.incr("foo");
pool.destroy(); pool.returnResource(jedis);
} pool.destroy();
}
@Test @Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException, public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException { IOException {
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT, JedisPool pool = new JedisPool(hnp.host, hnp.port);
2000, "foobared"); pool.setResourcesNumber(1);
pool.setResourcesNumber(1); pool.init();
pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.quit(); jedis.auth("foobared");
pool.returnBrokenResource(jedis); jedis.quit();
pool.returnBrokenResource(jedis);
jedis = pool.getResource(200); jedis = pool.getResource(200);
jedis.incr("foo"); jedis.auth("foobared");
pool.returnResource(jedis); jedis.incr("foo");
pool.destroy(); pool.returnResource(jedis);
} pool.destroy();
}
@Test(expected = TimeoutException.class) @Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException { public void checkPoolOverflow() throws TimeoutException {
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT, JedisPool pool = new JedisPool(hnp.host, hnp.port);
2000, "foobared"); pool.setResourcesNumber(1);
pool.setResourcesNumber(1); pool.init();
pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.set("foo", "0"); jedis.auth("foobared");
jedis.set("foo", "0");
Jedis newJedis = pool.getResource(200); Jedis newJedis = pool.getResource(200);
newJedis.incr("foo"); newJedis.auth("foobared");
} newJedis.incr("foo");
}
} }

View File

@@ -6,19 +6,29 @@ import java.util.List;
import junit.framework.Assert; import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPipeline; import redis.clients.jedis.JedisPipeline;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PipeliningTest extends Assert { public class PipeliningTest extends Assert {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private Jedis jedis;
@Before
public void setUp() throws Exception {
jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
}
@Test @Test
public void pipeline() throws UnknownHostException, IOException { public void pipeline() throws UnknownHostException, IOException {
Jedis jedis = new Jedis("localhost");
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
List<Object> results = jedis.pipelined(new JedisPipeline() { List<Object> results = jedis.pipelined(new JedisPipeline() {
public void execute() { public void execute() {
client.set("foo", "bar"); client.set("foo", "bar");

View File

@@ -54,7 +54,6 @@ public class ProtocolTest extends Assert {
Protocol protocol = new Protocol(); Protocol protocol = new Protocol();
String response = (String) protocol.read(new RedisInputStream(fis)); String response = (String) protocol.read(new RedisInputStream(fis));
assertEquals("012345678901234567890123456789", response); assertEquals("012345678901234567890123456789", response);
// assertEquals(3, fis.getReadMethodCallCount());
} }

View File

@@ -8,17 +8,20 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.util.Hashing; import redis.clients.util.Hashing;
import redis.clients.util.ShardInfo; import redis.clients.util.ShardInfo;
public class ShardedJedisTest extends Assert { public class ShardedJedisTest extends Assert {
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers().get(0);
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers().get(1);
@Test @Test
public void checkSharding() throws IOException { public void checkSharding() throws IOException {
List<ShardInfo> shards = new ArrayList<ShardInfo>(); List<ShardInfo> shards = new ArrayList<ShardInfo>();
shards.add(new ShardInfo("localhost", Protocol.DEFAULT_PORT)); shards.add(new ShardInfo(redis1.host, redis1.port));
shards.add(new ShardInfo("localhost", Protocol.DEFAULT_PORT + 1)); shards.add(new ShardInfo(redis2.host, redis2.port));
ShardedJedis jedis = new ShardedJedis(shards); ShardedJedis jedis = new ShardedJedis(shards);
ShardInfo s1 = jedis.getShardInfo("a"); ShardInfo s1 = jedis.getShardInfo("a");
ShardInfo s2 = jedis.getShardInfo("b"); ShardInfo s2 = jedis.getShardInfo("b");
@@ -28,10 +31,10 @@ public class ShardedJedisTest extends Assert {
@Test @Test
public void trySharding() throws IOException { public void trySharding() throws IOException {
List<ShardInfo> shards = new ArrayList<ShardInfo>(); List<ShardInfo> shards = new ArrayList<ShardInfo>();
ShardInfo si = new ShardInfo("localhost", Protocol.DEFAULT_PORT); ShardInfo si = new ShardInfo(redis1.host, redis1.port);
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
si = new ShardInfo("localhost", Protocol.DEFAULT_PORT + 1); si = new ShardInfo(redis2.host, redis2.port);
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards); ShardedJedis jedis = new ShardedJedis(shards);
@@ -55,10 +58,10 @@ public class ShardedJedisTest extends Assert {
@Test @Test
public void tryShardingWithMurmure() throws IOException { public void tryShardingWithMurmure() throws IOException {
List<ShardInfo> shards = new ArrayList<ShardInfo>(); List<ShardInfo> shards = new ArrayList<ShardInfo>();
ShardInfo si = new ShardInfo("localhost", Protocol.DEFAULT_PORT); ShardInfo si = new ShardInfo(redis1.host, redis1.port);
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
si = new ShardInfo("localhost", Protocol.DEFAULT_PORT + 1); si = new ShardInfo(redis2.host, redis2.port);
si.setPassword("foobared"); si.setPassword("foobared");
shards.add(si); shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMURE_HASH); ShardedJedis jedis = new ShardedJedis(shards, Hashing.MURMURE_HASH);

View File

@@ -5,13 +5,16 @@ import java.net.UnknownHostException;
import java.util.Calendar; import java.util.Calendar;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class GetSetBenchmark { public class GetSetBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 100000; private static final int TOTAL_OPERATIONS = 100000;
public static void main(String[] args) throws UnknownHostException, public static void main(String[] args) throws UnknownHostException,
IOException { IOException {
Jedis jedis = new Jedis("localhost"); Jedis jedis = new Jedis(hnp.host, hnp.port);
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();

View File

@@ -6,13 +6,16 @@ import java.util.Calendar;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPipeline; import redis.clients.jedis.JedisPipeline;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PipelinedGetSetBenchmark { public class PipelinedGetSetBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 200000; private static final int TOTAL_OPERATIONS = 200000;
public static void main(String[] args) throws UnknownHostException, public static void main(String[] args) throws UnknownHostException,
IOException { IOException {
Jedis jedis = new Jedis("localhost"); Jedis jedis = new Jedis(hnp.host, hnp.port);
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();

View File

@@ -9,14 +9,16 @@ import java.util.concurrent.atomic.AtomicInteger;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Protocol; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PoolBenchmark { public class PoolBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 100000; private static final int TOTAL_OPERATIONS = 100000;
public static void main(String[] args) throws UnknownHostException, public static void main(String[] args) throws UnknownHostException,
IOException, TimeoutException, InterruptedException { IOException, TimeoutException, InterruptedException {
Jedis j = new Jedis("localhost"); Jedis j = new Jedis(hnp.host, hnp.port);
j.connect(); j.connect();
j.auth("foobared"); j.auth("foobared");
j.flushAll(); j.flushAll();
@@ -37,7 +39,7 @@ public class PoolBenchmark {
Thread hj = new Thread(new Runnable() { Thread hj = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
Jedis j = new Jedis("localhost"); Jedis j = new Jedis(hnp.host, hnp.port);
try { try {
j.connect(); j.connect();
j.auth("foobared"); j.auth("foobared");
@@ -61,8 +63,8 @@ public class PoolBenchmark {
} }
private static void withPool() throws InterruptedException { private static void withPool() throws InterruptedException {
final JedisPool pool = new JedisPool("localhost", final JedisPool pool = new JedisPool(hnp.host, hnp.port,
Protocol.DEFAULT_PORT, 2000, "foobared"); 2000, "foobared");
pool.setResourcesNumber(50); pool.setResourcesNumber(50);
pool.setDefaultPoolWait(1000000); pool.setDefaultPoolWait(1000000);
pool.init(); pool.init();

View File

@@ -9,34 +9,36 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol; import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public abstract class JedisCommandTestBase extends Assert { public abstract class JedisCommandTestBase extends Assert {
protected static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
protected Jedis jedis; protected Jedis jedis;
public JedisCommandTestBase() { public JedisCommandTestBase() {
super(); super();
} }
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
jedis = new Jedis("localhost", Protocol.DEFAULT_PORT, 500); jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.flushAll(); jedis.flushAll();
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
jedis.disconnect(); jedis.disconnect();
} }
protected Jedis createJedis() throws UnknownHostException, IOException { protected Jedis createJedis() throws UnknownHostException, IOException {
Jedis j = new Jedis("localhost"); Jedis j = new Jedis(hnp.host, hnp.port);
j.connect(); j.connect();
j.auth("foobared"); j.auth("foobared");
j.flushAll(); j.flushAll();
return j; return j;
} }
} }

View File

@@ -5,6 +5,7 @@ import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
@@ -13,6 +14,17 @@ import redis.clients.jedis.Transaction;
import redis.clients.jedis.TransactionBlock; import redis.clients.jedis.TransactionBlock;
public class TransactionCommandsTest extends JedisCommandTestBase { public class TransactionCommandsTest extends JedisCommandTestBase {
Jedis nj;
@Before
public void setUp() throws Exception {
super.setUp();
nj = new Jedis(hnp.host, hnp.port, 500);
nj.connect();
nj.auth("foobared");
nj.flushAll();
}
@Test @Test
public void multi() { public void multi() {
Transaction trans = jedis.multi(); Transaction trans = jedis.multi();
@@ -62,7 +74,6 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
jedis.watch("mykey"); jedis.watch("mykey");
Transaction t = jedis.multi(); Transaction t = jedis.multi();
Jedis nj = new Jedis("localhost");
nj.connect(); nj.connect();
nj.auth("foobared"); nj.auth("foobared");
nj.set("mykey", "bar"); nj.set("mykey", "bar");
@@ -83,7 +94,6 @@ public class TransactionCommandsTest extends JedisCommandTestBase {
assertEquals("OK", status); assertEquals("OK", status);
Transaction t = jedis.multi(); Transaction t = jedis.multi();
Jedis nj = new Jedis("localhost");
nj.connect(); nj.connect();
nj.auth("foobared"); nj.auth("foobared");
nj.set("mykey", "bar"); nj.set("mykey", "bar");