Added sharding benchmark and removed some imports

This commit is contained in:
Jonathan Leibiusky
2010-09-30 21:04:21 -03:00
parent 39618506e4
commit 6a1e141064
10 changed files with 2997 additions and 2945 deletions

View File

@@ -11,7 +11,6 @@ import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import redis.clients.jedis.Jedis;

View File

@@ -10,7 +10,6 @@ import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
import redis.clients.util.RedisOutputStream;
import redis.clients.util.ShardInfo;
public class JedisTest extends JedisCommandTestBase {
@Test
@@ -36,7 +35,8 @@ public class JedisTest extends JedisCommandTestBase {
@Test
public void connectWithShardInfo() {
JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT);
JedisShardInfo shardInfo = new JedisShardInfo("localhost",
Protocol.DEFAULT_PORT);
shardInfo.setPassword("foobared");
Jedis jedis = new Jedis(shardInfo);
jedis.get("foo");

View File

@@ -11,21 +11,20 @@ import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPipeline;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PipeliningTest extends Assert {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private Jedis jedis;
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@Before
public void setUp() throws Exception {
jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
}
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
public void pipeline() throws UnknownHostException, IOException {

View File

@@ -2,7 +2,6 @@ package redis.clients.jedis.tests;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
@@ -50,13 +49,13 @@ public class ProtocolTest extends Assert {
@Test
public void fragmentedBulkReply() {
FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream("$30\r\n012345678901234567890123456789\r\n".getBytes());
Protocol protocol = new Protocol();
String response = (String) protocol.read(new RedisInputStream(fis));
assertEquals("012345678901234567890123456789", response);
FragmentedByteArrayInputStream fis = new FragmentedByteArrayInputStream(
"$30\r\n012345678901234567890123456789\r\n".getBytes());
Protocol protocol = new Protocol();
String response = (String) protocol.read(new RedisInputStream(fis));
assertEquals("012345678901234567890123456789", response);
}
@Test
public void nullBulkReply() {
InputStream is = new ByteArrayInputStream("$-1\r\n".getBytes());

View File

@@ -12,21 +12,21 @@ import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
import redis.clients.util.Hashing;
import redis.clients.util.ShardInfo;
import redis.clients.util.Sharded;
public class ShardedJedisTest extends Assert {
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers().get(0);
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers().get(1);
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers()
.get(0);
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers()
.get(1);
@Test
public void checkSharding() throws IOException {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port));
shards.add(new JedisShardInfo(redis2.host, redis2.port));
ShardedJedis jedis = new ShardedJedis(shards);
ShardInfo s1 = jedis.getShardInfo("a");
ShardInfo s2 = jedis.getShardInfo("b");
JedisShardInfo s1 = jedis.getShardInfo("a");
JedisShardInfo s2 = jedis.getShardInfo("b");
assertNotSame(s1, s2);
}
@@ -83,38 +83,39 @@ public class ShardedJedisTest extends Assert {
assertEquals("bar1", j.get("b"));
j.disconnect();
}
/** @author muriloq@gmail.com */
@Test
public void checkKeyTags(){
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port));
shards.add(new JedisShardInfo(redis2.host, redis2.port));
ShardedJedis jedis = new ShardedJedis(shards, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
assertEquals(jedis.getKeyTag("foo"),"foo");
assertEquals(jedis.getKeyTag("foo{bar}"),"bar");
assertEquals(jedis.getKeyTag("foo{bar}}"),"bar"); // default pattern is non greedy
assertEquals(jedis.getKeyTag("{bar}foo"),"bar"); // Key tag may appear anywhere
assertEquals(jedis.getKeyTag("f{bar}oo"),"bar"); // Key tag may appear anywhere
ShardInfo s1 = jedis.getShardInfo("abc{bar}");
ShardInfo s2 = jedis.getShardInfo("foo{bar}");
assertSame(s1, s2);
ShardInfo s3 = jedis.getShardInfo("a");
ShardInfo s4 = jedis.getShardInfo("b");
assertNotSame(s3, s4);
ShardedJedis jedis2 = new ShardedJedis(shards);
assertEquals(jedis2.getKeyTag("foo"),"foo");
assertNotSame(jedis2.getKeyTag("foo{bar}"),"bar");
ShardInfo s5 = jedis2.getShardInfo("foo{bar}");
ShardInfo s6 = jedis2.getShardInfo("abc{bar}");
assertNotSame(s5, s6);
}
@Test
public void checkKeyTags() {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port));
shards.add(new JedisShardInfo(redis2.host, redis2.port));
ShardedJedis jedis = new ShardedJedis(shards,
ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
assertEquals(jedis.getKeyTag("foo"), "foo");
assertEquals(jedis.getKeyTag("foo{bar}"), "bar");
assertEquals(jedis.getKeyTag("foo{bar}}"), "bar"); // default pattern is
// non greedy
assertEquals(jedis.getKeyTag("{bar}foo"), "bar"); // Key tag may appear
// anywhere
assertEquals(jedis.getKeyTag("f{bar}oo"), "bar"); // Key tag may appear
// anywhere
JedisShardInfo s1 = jedis.getShardInfo("abc{bar}");
JedisShardInfo s2 = jedis.getShardInfo("foo{bar}");
assertSame(s1, s2);
JedisShardInfo s3 = jedis.getShardInfo("a");
JedisShardInfo s4 = jedis.getShardInfo("b");
assertNotSame(s3, s4);
ShardedJedis jedis2 = new ShardedJedis(shards);
assertEquals(jedis2.getKeyTag("foo"), "foo");
assertNotSame(jedis2.getKeyTag("foo{bar}"), "bar");
JedisShardInfo s5 = jedis2.getShardInfo("foo{bar}");
JedisShardInfo s6 = jedis2.getShardInfo("abc{bar}");
assertNotSame(s5, s6);
}
}

View File

@@ -0,0 +1,49 @@
package redis.clients.jedis.tests.benchmark;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class ShardedBenchmark {
private static HostAndPort hnp1 = HostAndPortUtil.getRedisServers().get(0);
private static HostAndPort hnp2 = HostAndPortUtil.getRedisServers().get(1);
private static final int TOTAL_OPERATIONS = 100000;
public static void main(String[] args) throws UnknownHostException,
IOException {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo shard = new JedisShardInfo(hnp1.host, hnp1.port);
shard.setPassword("foobared");
shards.add(shard);
shard = new JedisShardInfo(hnp2.host, hnp2.port);
shard.setPassword("foobared");
shards.add(shard);
ShardedJedis jedis = new ShardedJedis(shards);
Collection<JedisShardInfo> allShards = jedis.getAllShards();
for (JedisShardInfo jedisShardInfo : allShards) {
jedisShardInfo.getResource().flushAll();
}
long begin = Calendar.getInstance().getTimeInMillis();
for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
String key = "foo" + n;
jedis.set(key, "bar" + n);
jedis.get(key);
}
long elapsed = Calendar.getInstance().getTimeInMillis() - begin;
jedis.disconnect();
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
}