您的位置:首页 > 数据库 > Memcache

windows下redis和memcached的性能对比测试

2011-03-29 22:09 477 查看
在windows下测试了redis和memcached的性能,如下:

jedis get 20000次的执行时间:1265毫秒

jedis set 20000次的执行时间:1313毫秒

jedis 同时启动60个线程的执行时间:21609毫秒

xmemcached get执行20000次的时间:3219毫秒

xmemcached set 20000次的执行时间:3000毫秒

xmemcached 同时启动60个线程的执行时间:75562毫秒

可以看出redis的性能要好于memcached!

redis代码:

public class Demo {

public static void main(String[] args) throws Exception {
Demo demo = new Demo();
demo.test();
}

public void test() throws Exception {
Jedis jedis = new Jedis("localhost");
jedis.set("key", "mykey");

long begin = System.currentTimeMillis();
for(int i=0; i<20000; i++)
jedis.get("key");
long end = System.currentTimeMillis();
System.out.println("jedis get20000次的执行时间:" + (end - begin) + "毫秒");

begin = System.currentTimeMillis();
for(int i=0; i<20000; i++)
jedis.set(String.valueOf(i), String.valueOf(i));
end = System.currentTimeMillis();
System.out.println("jedis set 20000次的执行时间:" + (end - begin) + "毫秒");

begin = System.currentTimeMillis();
Thread t[] = new Thread[60];
for(int j=0; j<t.length; j++) {
t[j] = new TestThread();
t[j].start();
}

for(int j=0; j<t.length; j++) {
t[j].join();
}
end = System.currentTimeMillis();
System.out.println("jedis 启动"+ t.length +"个线程的执行时间:" + (end - begin) + "毫秒");
}

class TestThread extends Thread {

@Override
public void run() {
Jedis jedis = new Jedis("localhost");
for(int i=0; i<20000; i++)
jedis.get(String.valueOf(i));
jedis.disconnect();
}

}
}

测试redis的代码时,发现同时启动的线程只能到达63,超过的线程都会失败。

排查很久,发现配置文件中有一句话:it's up to the number of file descriptors the Redis process is able to open.

估计在windows xp下只能到达64,那么还有一个呢?无法得知,查了好久也没有找到和linux下文件描述符想匹配的东西。

xmemcache代码:

public class Demo {

public static void main(String[] args) throws Exception {
Demo demo = new Demo();
demo.test();
}

public void test() throws Exception  {
MemcachedClient mc = new XMemcachedClient("localhost", 11211);
mc.set("key", 2000, "mykey");

long begin = System.currentTimeMillis();
for(int i=0; i<20000; i++)
mc.get("key", 1000);
long end = System.currentTimeMillis();

System.out.println("memcached get执行20000次的时间:" + (end - begin) + "毫秒");

begin = System.currentTimeMillis();
for(int i=0; i<20000; i++)
mc.set(String.valueOf(i), 2000 ,String.valueOf(i));
end = System.currentTimeMillis();
System.out.println("memcached set 20000次的执行时间:" + (end - begin) + "毫秒");

mc.shutdown();

begin = System.currentTimeMillis();
Thread t[] = new Thread[60];
for(int j=0; j<t.length; j++) {
t[j] = new TestThread();
t[j].start();
}

for(int j=0; j<t.length; j++) {
t[j].join();
}
end = System.currentTimeMillis();
System.out.println("memcached 启动"+ t.length +"个线程的执行时间:" + (end - begin) + "毫秒");
}

class TestThread extends Thread {

@Override
public void run() {
try {
MemcachedClient mc = new XMemcachedClient("localhost", 11211);
for(int i=0; i<20000; i++)
mc.get(String.valueOf(i), 1000);
mc.shutdown();
}
catch (Exception e) {

}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: