对比spymemcached和Memcached-Java-Client性能
在http://code.google.com/p/memcached/wiki/Clients 上主要有两个Java版本的Memcached客户端,原文如下:
spymemcached
- http://code.google.com/p/spymemcached
- An improved Java API maintained by Dustin Sallings.
- Aggressively optimised, ability to run async, supports binary protocol, etc. See site for details.
Java memcached client
- http://www.whalin.com/memcached
- A Java API is maintained by Greg Whalin from Meetup.com.
在个人电脑上写了一段程序对比两个客户端的效率(单一memcached服务器),
用100个线程,分别插入后查询1000笔数据,spymemcached耗时约8s
对spymemcached测试如下:
import java.io.IOException;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;
public class TestSpymemcached extends Thread{
/**
* @param args
*/
private int count;
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
System.out.println("begin:"+System.currentTimeMillis());
for(int i=0 ; i<100;i++){
TestJavaClient test = new TestJavaClient(i);
test.start();
}
System.out.println("end:"+System.currentTimeMillis());
}
public TestJavaClient(int i){
count = i;
}
public void run(){
System.out.println(count+"start:"+System.currentTimeMillis());
MemcachedClient c= MemCachedManager.getInstance();
for(int i=0 ; i<1000;i++){
// Store a value (async) for one hour
c.set(count+"000"+i, 3600, "Hello World "+count+"000"+i+"!");
// Retrieve a value (synchronously).
Object myObject=c.get("liusong"+count);
}
System.out.println(count+"end:"+System.currentTimeMillis());
}
}
class MemCachedManager{
private static MemcachedClient c;
public static synchronized MemcachedClient getInstance(){
if(c==null){
try {
c=new MemcachedClient(
AddrUtil.getAddresses("10.148.11.112:11211"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return c;
}
}
Java memcached client同样用100个线程,分别插入后查询1000笔数据,耗时约36s
Java memcached client代码如下:
public class TestJavaClient extends Thread{
private int count;
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
System.out.println("begin:"+System.currentTimeMillis());
for(int i=0 ; i<100;i++){
TestDangaClient test = new TestDangaClient(i);
test.start();
}
System.out.println("end:"+System.currentTimeMillis());
}
public TestDangaClient(int i){
count = i;
}
public void run(){
System.out.println(count+"start:"+System.currentTimeMillis());
MemCachedManage cache = MemCachedManage.getInstance();
for(int i=0 ; i<1000;i++){
// Store a value (async) for one hour
cache.add(count+"000"+i, "Hello World "+count+"000"+i+"!");
// Retrieve a value (synchronously).
Object myObject=cache.get("liusong"+count);
}
System.out.println(count+"end:"+System.currentTimeMillis());
}
}
import java.util.Date;
import com.meetup.memcached.MemcachedClient;
import com.meetup.memcached.SockIOPool;
public class MemCachedManage {
private static MemcachedClient mcc = new MemcachedClient();
private static MemCachedManage memCachedManager = new MemCachedManage();
static {
String[] servers = { "10.148.71.215:11211" };
Integer[] weights = { 3 };
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(servers);
pool.setWeights(weights);
pool.setInitConn(100);
pool.setMinConn(100);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);
pool.setMaintSleep(30);
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
pool.initialize();
mcc.setCompressEnable(true);
mcc.setCompressThreshold(64 * 1024);
}
protected MemCachedManage() {
}
public static MemCachedManage getInstance() {
return memCachedManager;
}
public boolean add(String key, Object value) {
return mcc.add(key, value);
}
public boolean add(String key, Object value, Date expiry) {
return mcc.add(key, value, expiry);
}
public boolean replace(String key, Object value) {
return mcc.replace(key, value);
}
public boolean replace(String key, Object value, Date expiry) {
return mcc.replace(key, value, expiry);
}
public Object get(String key) {
return mcc.get(key);
}
public static void main(String[] args) {
MemCachedManage cache = MemCachedManage.getInstance();
cache.add("hello", 234);
System.out.print("get value : " + cache.get("hello"));
}
}
结论,spymemcached更有效率。