From 8207c2415f573e11cbdb01201d604dbddbceab48 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Wed, 8 Sep 2010 17:39:25 -0300 Subject: [PATCH] Added a benchmark to the jedis pool --- .../jedis/tests/benchmark/PoolBenchmark.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java diff --git a/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java b/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java new file mode 100644 index 0000000..cc665b2 --- /dev/null +++ b/src/test/java/redis/clients/jedis/tests/benchmark/PoolBenchmark.java @@ -0,0 +1,89 @@ +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 redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +public class PoolBenchmark { + private static final int TOTAL_OPERATIONS = 100000; + + public static void main(String[] args) throws UnknownHostException, + IOException, TimeoutException, InterruptedException { + Jedis j = new Jedis("localhost"); + 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 * 3 * TOTAL_OPERATIONS) / elapsed) + " ops"); + } + + private static void withoutPool() throws InterruptedException { + List tds = new ArrayList(); + + 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("localhost"); + 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("localhost"); + pool.setResourcesNumber(1000); + pool.setDefaultPoolWait(20); + pool.init(); + List tds = new ArrayList(); + + for (int i = 0; i < TOTAL_OPERATIONS; i++) { + final String key = "foo" + i; + Thread hj = new Thread(new Runnable() { + @Override + public void run() { + try { + Jedis j = pool.getResource(); + j.auth("foobared"); + j.set(key, key); + j.get(key); + pool.returnResource(j); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + tds.add(hj); + hj.start(); + } + pool.destroy(); + } +} \ No newline at end of file