|
Package com.jd.redis.client;
import Java.util.concurrent.CountDownLatch;
import Redis.clients.jedis.Jedis;
import Redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
Publicclass jedispooltest {
privatestatic Jedispoolconfigconfig;//jedis Client Pool Configuration
privatestatic Jedispoolpool;//jedis Client Pool
Static {
Config =new jedispoolconfig ();
Config.setmaxactive (60000);
Config.setmaxidle (1000);
Config.setmaxwait (10000);
Config.settestonborrow (true);
Pool =new jedispool (config, "192.168.157.128", 6380);
}
/**
* Single pen Test (without pool)
* @param Count
*/
publicstaticvoid testnopool (int count) {
for (int i=0;i<count;i++) {
Jedis JR = null;
Try {
JR = new Jedis ("10.10.224.44", 6379);
Testonce (JR);
} catch (Exception e) {
E.printstacktrace ();
}
finally {
if (jr!=null) Jr.disconnect ();
}
}
}
/**
* Single pen Test (with pool)
* @param Count
*/
publicstaticvoid testwithpool (int count) {
for (int i=0;i<count;i++) {
Jedis JR = null;
Try {
JR = Pool.getresource ();
Testonce (JR);
} catch (Exception e) {
E.printstacktrace ();
}
finally {
if (jr!=null) Pool.returnresource (JR);
}
}
}
/**
* Concurrent Testing (no pool)
* @param paiallel concurrency
* @param count each concurrent loop count
*/
publicstaticvoid paialleltestnopool (int paiallel, int count) {
thread[] ts = new Thread[paiallel];
Use this object to ensure that all threads complete the main line friend exit
Countdownlatch cd = new countdownlatch (Paiallel);
long start = System.currenttimemillis ();
for (int i=0; i < paiallel; i++) {
Ts[i] = new Thread (new workernopool (CD, Count));
Ts[i].start ();
}
Try {
Cd.await ();//wait for all child threads to complete
} catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("Nopool usetime:" + (System.currenttimemillis ()-start));
}
/**
* Concurrent testing (with pool)
* @param paiallel concurrency
* @param count each concurrent loop count
*/
publicstaticvoid paialleltestwithpool (int paiallel, int count) {
Use this object to ensure that all threads complete the main line friend exit
Countdownlatch cd = new countdownlatch (Paiallel);
long start = System.currenttimemillis ();
thread[] ts = new Thread[paiallel];
for (int i=0; i < paiallel; i++) {
Ts[i] = new Thread (new workerwithpool (CD, Count));
Ts[i].start ();
}
Try {
Cd.await ();//wait for all child threads to complete
} catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("Pool usetime:" + (System.currenttimemillis ()-start));
Pool.destroy ();
}
privatestaticvoid Testonce (Jedis Jr) {
System.out.println (JR.INCR ("incrtest"));
}
Publicstaticclass Workernopoolimplements runnable{
Private COUNTDOWNLATCHCD;
PrivateintCount;
Public Workernopool (countdownlatch CD,int count) {
this. cd = CD;
this. Count = Count;
}
publicvoid Run () {
Try {
Testnopool (this. count);
} catch (Exception e) {
|