Add alternative pipeline usage

This commit is contained in:
Jonathan Leibiusky
2010-11-27 15:44:43 -03:00
parent f8636ec16b
commit 8043f12e20
10 changed files with 1122 additions and 104 deletions

View File

@@ -5,12 +5,12 @@ import java.net.UnknownHostException;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPipeline;
import redis.clients.jedis.PipelineBlock;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
@@ -21,23 +21,37 @@ public class PipeliningTest extends Assert {
@Before
public void setUp() throws Exception {
jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
}
@Test
public void pipeline() throws UnknownHostException, IOException {
List<Object> results = jedis.pipelined(new JedisPipeline() {
public void execute() {
client.set("foo", "bar");
client.get("foo");
}
});
List<Object> results = jedis.pipelined(new PipelineBlock() {
public void execute() {
set("foo", "bar");
get("foo");
}
});
assertEquals(2, results.size());
assertArrayEquals("OK".getBytes(Protocol.CHARSET), (byte[]) results
.get(0));
assertArrayEquals("bar".getBytes(Protocol.CHARSET), (byte[]) results
.get(1));
Pipeline p = jedis.pipelined();
p.set("foo", "bar");
p.get("foo");
results = p.execute();
assertEquals(2, results.size());
assertArrayEquals("OK".getBytes(Protocol.CHARSET), (byte[]) results
.get(0));
assertArrayEquals("bar".getBytes(Protocol.CHARSET), (byte[]) results
.get(1));
assertEquals(2, results.size());
assertArrayEquals("OK".getBytes(Protocol.CHARSET), (byte[])results.get(0));
assertArrayEquals("bar".getBytes(Protocol.CHARSET), (byte[])results.get(1));
}
}

View File

@@ -5,37 +5,37 @@ import java.net.UnknownHostException;
import java.util.Calendar;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPipeline;
import redis.clients.jedis.PipelineBlock;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class PipelinedGetSetBenchmark {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
private static final int TOTAL_OPERATIONS = 200000;
public static void main(String[] args) throws UnknownHostException,
IOException {
Jedis jedis = new Jedis(hnp.host, hnp.port);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
IOException {
Jedis jedis = new Jedis(hnp.host, hnp.port);
jedis.connect();
jedis.auth("foobared");
jedis.flushAll();
long begin = Calendar.getInstance().getTimeInMillis();
long begin = Calendar.getInstance().getTimeInMillis();
jedis.pipelined(new JedisPipeline() {
public void execute() {
for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
String key = "foo" + n;
client.set(key, "bar" + n);
client.get(key);
}
}
});
jedis.pipelined(new PipelineBlock() {
public void execute() {
for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
String key = "foo" + n;
set(key, "bar" + n);
get(key);
}
}
});
long elapsed = Calendar.getInstance().getTimeInMillis() - begin;
long elapsed = Calendar.getInstance().getTimeInMillis() - begin;
jedis.disconnect();
jedis.disconnect();
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
}