Added a small benchmark test with a small performance improvement
This commit is contained in:
@@ -22,11 +22,9 @@ public class Protocol {
|
|||||||
public static final byte COLON_BYTE = COLON.getBytes()[0];
|
public static final byte COLON_BYTE = COLON.getBytes()[0];
|
||||||
|
|
||||||
public void sendCommand(OutputStream os, String name, String... args) {
|
public void sendCommand(OutputStream os, String name, String... args) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder(ASTERISK + (args.length + 1)
|
||||||
builder.append(ASTERISK).append(args.length + 1).append(
|
+ COMMAND_DELIMITER + DOLLAR + name.length()
|
||||||
COMMAND_DELIMITER);
|
+ COMMAND_DELIMITER + name + COMMAND_DELIMITER);
|
||||||
builder.append(DOLLAR).append(name.length()).append(COMMAND_DELIMITER);
|
|
||||||
builder.append(name).append(COMMAND_DELIMITER);
|
|
||||||
for (String arg : args) {
|
for (String arg : args) {
|
||||||
builder.append(DOLLAR).append(arg.length()).append(
|
builder.append(DOLLAR).append(arg.length()).append(
|
||||||
COMMAND_DELIMITER).append(arg).append(COMMAND_DELIMITER);
|
COMMAND_DELIMITER).append(arg).append(COMMAND_DELIMITER);
|
||||||
@@ -45,17 +43,21 @@ public class Protocol {
|
|||||||
|
|
||||||
private String readLine(InputStream is) {
|
private String readLine(InputStream is) {
|
||||||
byte b;
|
byte b;
|
||||||
|
byte c;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while ((b = (byte) is.read()) != -1) {
|
while ((b = (byte) is.read()) != -1) {
|
||||||
if (b == '\r') {
|
if (b == '\r') {
|
||||||
b = (byte) is.read();
|
c = (byte) is.read();
|
||||||
if (b == '\n') {
|
if (c == '\n') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
sb.append((char) b);
|
||||||
|
sb.append((char) c);
|
||||||
|
} else {
|
||||||
|
sb.append((char) b);
|
||||||
}
|
}
|
||||||
sb.append((char) b);
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Dont know what to do here!
|
// TODO Dont know what to do here!
|
||||||
@@ -106,7 +108,6 @@ public class Protocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object processBulkReply(InputStream is) throws IOException {
|
private Object processBulkReply(InputStream is) throws IOException {
|
||||||
String ret = null;
|
|
||||||
int len = Integer.parseInt(readLine(is));
|
int len = Integer.parseInt(readLine(is));
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
return null;
|
return null;
|
||||||
@@ -117,8 +118,7 @@ public class Protocol {
|
|||||||
is.read();
|
is.read();
|
||||||
is.read();
|
is.read();
|
||||||
|
|
||||||
ret = new String(read);
|
return new String(read);
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object processInteger(InputStream is) {
|
private Object processInteger(InputStream is) {
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package redis.clients.jedis.tests.benchmark;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisException;
|
||||||
|
|
||||||
|
public class GetSetBenchmark {
|
||||||
|
private static final int TOTAL_OPERATIONS = 100000;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws UnknownHostException,
|
||||||
|
IOException, JedisException {
|
||||||
|
Jedis jedis = new Jedis("localhost");
|
||||||
|
jedis.connect();
|
||||||
|
jedis.auth("foobared");
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user