more reasonable PoolBenchmark
This commit is contained in:
@@ -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,30 +61,35 @@ 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() {
|
||||||
try {
|
for(int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS; ) {
|
||||||
Jedis j = pool.getResource();
|
try {
|
||||||
j.auth("foobared");
|
Jedis j = pool.getResource();
|
||||||
j.set(key, key);
|
final String key = "foo" + i;
|
||||||
j.get(key);
|
j.set(key, key);
|
||||||
pool.returnResource(j);
|
j.get(key);
|
||||||
} catch (Exception e) {
|
pool.returnResource(j);
|
||||||
e.printStackTrace();
|
} catch (Exception e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
tds.add(hj);
|
tds.add(hj);
|
||||||
hj.start();
|
hj.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(Thread t : tds)
|
||||||
|
t.join();
|
||||||
|
|
||||||
pool.destroy();
|
pool.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user