more reasonable PoolBenchmark

This commit is contained in:
Alex Tkachman
2010-09-14 11:59:25 +02:00
parent 032fe7e134
commit 20dac7e9b4

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 java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
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;
@@ -24,7 +25,7 @@ public class PoolBenchmark {
// withoutPool(); // withoutPool();
withPool(); withPool();
long elapsed = System.currentTimeMillis() - t; long elapsed = System.currentTimeMillis() - t;
System.out.println(((1000 * 3 * TOTAL_OPERATIONS) / elapsed) + " ops"); System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
} }
private static void withoutPool() throws InterruptedException { private static void withoutPool() throws InterruptedException {
@@ -60,19 +61,19 @@ 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("localhost");
pool.setResourcesNumber(1000); pool.setResourcesNumber(50);
pool.setDefaultPoolWait(20); pool.setDefaultPoolWait(1000000);
pool.init(); pool.init();
List<Thread> tds = new ArrayList<Thread>(); List<Thread> tds = new ArrayList<Thread>();
for (int i = 0; i < TOTAL_OPERATIONS; i++) { final AtomicInteger ind = new AtomicInteger();
final String key = "foo" + i; for (int i = 0; i < 50; i++) {
Thread hj = new Thread(new Runnable() { Thread hj = new Thread(new Runnable() {
@Override
public void run() { public void run() {
for(int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS; ) {
try { try {
Jedis j = pool.getResource(); Jedis j = pool.getResource();
j.auth("foobared"); final String key = "foo" + i;
j.set(key, key); j.set(key, key);
j.get(key); j.get(key);
pool.returnResource(j); pool.returnResource(j);
@@ -80,10 +81,15 @@ public class PoolBenchmark {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
}); });
tds.add(hj); tds.add(hj);
hj.start(); hj.start();
} }
for(Thread t : tds)
t.join();
pool.destroy(); pool.destroy();
} }
} }