Why My JedisPool is not so fast
See original GitHub issueWhen I use a multi-threaded simple java jar to test the performance of the JedisPool, the avg response time can reach 0.068 per request. Code is as following:
public class TestJedisPool { private static JedisPool pool; static { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(250); config.setMaxIdle(1000 * 60 * 60 * 6); config.setMaxWait(1000); config.setTestOnBorrow(true);
pool = new JedisPool(config, "anotherhost", 6379);
}
private int total = 20000;
public void setTotal(int total) {
this.total = total;
}
private int c = 1;
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < TestJedisPool.this.total; i++){
Jedis jedis = TestJedisPool.pool.getResource();
jedis.get(String.valueOf(i));
TestJedisPool.pool.returnResource(jedis);
}
}
}
private void setConcurrent(int c) {
this.c = c;
}
public void test() {
initInsert();
testThread();
}
private void initInsert() {
System.out.println("INIT Insert");
Jedis jedis = pool.getResource();
for (int i = 0; i < (TestJedisPool.this.total); i++)
jedis.set(String.valueOf(i), String.valueOf(i));
pool.returnResource(jedis);
}
private void testThread() {
long begin = System.currentTimeMillis();
System.out.println("BEGIN Test");
Thread thread[] = new Thread[this.c];
for (int i = 0; i < thread.length; i++) {
thread[i] = new MyThread();
thread[i].start();
}
for (int i = 0; i < thread.length; i++) {
try {
thread[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long cost = System.currentTimeMillis() - begin;
System.out.println("COST:" + cost + " ms");
System.out.println("AVG:" + ((double)cost / (total*c)));
}
/**
* @param args
*/
public static void main(String[] args) {
TestJedisPool test = new TestJedisPool();
test.setTotal(Integer.parseInt(args[0]));
test.setConcurrent(Integer.parseInt(args[1]));
test.test();
}
}
But, When I use loadrunner to test calling a servlet(/testjedis) which uses the jedisPool running in a resin, I found the performance become very low about 20ms per request. The usage of JedisPool is the same as above, and also the JedisPool’s config is the same. The servlet is like this: public class TestRedis extends HttpServlet { private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestRedis() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
jedis = jedisPool.getResource();
jedis.set(key, value);
jedisPool.returnResource(jedis);
}
}
When I recorded the time in the code, I found the most time costs is jedisPool.getResource(), What makes getResource becomes slow? Why is there so much different between a resin and a common jar?
Issue Analytics
- State:
- Created 12 years ago
- Comments:10
@ThomasLau Redis worker thread is single but it uses event driven so we can concurrent within round-trip.
I have this configuration.
jedisPoolConfig = new JedisPoolConfig()j jedisPoolConfig.setMaxTotal(100); jedispool = new JedisPool(jedisPoolConfig,REDIS_HOST,REDIS_PORT)); jedis = jedispool.getResource();
Performance wise it is not working good ,Can somebody help how do i improve the performance