在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代码:
01 |
public class Demo { |
02 |
03 |
public static void main(String[] args) throws Exception { |
04 |
Demo demo = new Demo(); |
05 |
demo.test(); |
06 |
} |
07 |
|
08 |
public void test() throws Exception { |
09 |
Jedis jedis = new Jedis( "localhost" ); |
10 |
jedis.set( "key" , "mykey" ); |
11 |
|
12 |
long begin = System.currentTimeMillis(); |
13 |
for ( int i= 0 ; i< 20000 ; i++) |
14 |
jedis.get( "key" ); |
15 |
long end = System.currentTimeMillis(); |
16 |
System.out.println( "jedis get20000次的执行时间:" + (end - begin) + "毫秒" ); |
17 |
|
18 |
begin = System.currentTimeMillis(); |
19 |
for ( int i= 0 ; i< 20000 ; i++) |
20 |
jedis.set(String.valueOf(i), String.valueOf(i)); |
21 |
end = System.currentTimeMillis(); |
22 |
System.out.println( "jedis set 20000次的执行时间:" + (end - begin) + "毫秒" ); |
23 |
|
24 |
begin = System.currentTimeMillis(); |
25 |
Thread t[] = new Thread[ 60 ]; |
26 |
for ( int j= 0 ; j<t.length; j++) { |
27 |
t[j] = new TestThread(); |
28 |
t[j].start(); |
29 |
} |
30 |
|
31 |
for ( int j= 0 ; j<t.length; j++) { |
32 |
t[j].join(); |
33 |
} |
34 |
end = System.currentTimeMillis(); |
35 |
System.out.println( "jedis 启动" + t.length + "个线程的执行时间:" + (end - begin) + "毫秒" ); |
36 |
} |
37 |
38 |
class TestThread extends Thread { |
39 |
40 |
@Override |
41 |
public void run() { |
42 |
Jedis jedis = new Jedis( "localhost" ); |
43 |
for ( int i= 0 ; i< 20000 ; i++) |
44 |
jedis.get(String.valueOf(i)); |
45 |
jedis.disconnect(); |
46 |
} |
47 |
|
48 |
} |
49 |
} |
测试redis的代码时,发现同时启动的线程只能到达63,超过的线程都会失败。
排查很久,发现配置文件中有一句话:it's up to the number of file descriptors the Redis process is able to open.
估计在windows xp下只能到达64,那么还有一个呢?无法得知,查了好久也没有找到和linux下文件描述符想匹配的东西。
xmemcache代码:
01 |
public class Demo { |
02 |
03 |
public static void main(String[] args) throws Exception { |
04 |
Demo demo = new Demo(); |
05 |
demo.test(); |
06 |
} |
07 |
|
08 |
public void test() throws Exception { |
09 |
MemcachedClient mc = new XMemcachedClient( "localhost" , 11211 ); |
10 |
mc.set( "key" , 2000 , "mykey" ); |
11 |
|
12 |
long begin = System.currentTimeMillis(); |
13 |
for ( int i= 0 ; i< 20000 ; i++) |
14 |
mc.get( "key" , 1000 ); |
15 |
long end = System.currentTimeMillis(); |
16 |
|
17 |
System.out.println( "memcached get执行20000次的时间:" + (end - begin) + "毫秒" ); |
18 |
|
19 |
begin = System.currentTimeMillis(); |
20 |
for ( int i= 0 ; i< 20000 ; i++) |
21 |
mc.set(String.valueOf(i), 2000 ,String.valueOf(i)); |
22 |
end = System.currentTimeMillis(); |
23 |
System.out.println( "memcached set 20000次的执行时间:" + (end - begin) + "毫秒" ); |
24 |
|
25 |
mc.shutdown(); |
26 |
|
27 |
begin = System.currentTimeMillis(); |
28 |
Thread t[] = new Thread[ 60 ]; |
29 |
for ( int j= 0 ; j<t.length; j++) { |
30 |
t[j] = new TestThread(); |
31 |
t[j].start(); |
32 |
} |
33 |
|
34 |
for ( int j= 0 ; j<t.length; j++) { |
35 |
t[j].join(); |
36 |
} |
37 |
end = System.currentTimeMillis(); |
38 |
System.out.println( "memcached 启动" + t.length + "个线程的执行时间:" + (end - begin) + "毫秒" ); |
39 |
} |
40 |
41 |
class TestThread extends Thread { |
42 |
43 |
@Override |
44 |
public void run() { |
45 |
try { |
46 |
MemcachedClient mc = new XMemcachedClient( "localhost" , 11211 ); |
47 |
for ( int i= 0 ; i< 20000 ; i++) |
48 |
mc.get(String.valueOf(i), 1000 ); |
49 |
mc.shutdown(); |
50 |
} |
51 |
catch (Exception e) { |
52 |
|
53 |
} |
54 |
} |
55 |
|
56 |
} |
57 |
} |