Files
jlibredis/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java
Yaourt 4a1d9928b5 Update the way the Redis host(s) can be configured for the tests.
There is now a single property : redis-hosts.
This property must contain at least 2 host definitions of the form "host:port" (comma separated).
Sharding tests need 2 hosts ...

If this is not the case, the default value used is "localhost:6379,localhost:6380".
Tests that required one host are using the first definition.
2010-09-15 10:49:36 +02:00

99 lines
2.5 KiB
Java

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 redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
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 {
Jedis j = new Jedis(hnp.host, hnp.port);
j.connect();
j.auth("foobared");
j.flushAll();
j.quit();
j.disconnect();
long t = System.currentTimeMillis();
// withoutPool();
withPool();
long elapsed = System.currentTimeMillis() - t;
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
private static void withoutPool() throws InterruptedException {
List<Thread> tds = new ArrayList<Thread>();
for (int i = 0; i < TOTAL_OPERATIONS; i++) {
final String key = "foo" + i;
Thread hj = new Thread(new Runnable() {
@Override
public void run() {
Jedis j = new Jedis(hnp.host, hnp.port);
try {
j.connect();
j.auth("foobared");
j.set(key, key);
j.get(key);
j.quit();
j.disconnect();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
tds.add(hj);
hj.start();
}
for (Thread thread : tds) {
thread.join();
}
}
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();
List<Thread> tds = new ArrayList<Thread>();
final AtomicInteger ind = new AtomicInteger();
for (int i = 0; i < 50; i++) {
Thread hj = new Thread(new Runnable() {
public void run() {
for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) {
try {
Jedis j = pool.getResource();
final String key = "foo" + i;
j.set(key, key);
j.get(key);
pool.returnResource(j);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
tds.add(hj);
hj.start();
}
for (Thread t : tds)
t.join();
pool.destroy();
}
}