記一次Redis性能測試

記一次對Redis單機版的性能測性

在某書本上,有描述,redis 支持十幾萬次/s的讀、81000萬次的寫。讀寫性能可能有多方面因素,最近剛買了一臺雲主機,今天我也來測試下吧。代碼如下:


import redis.clients.jedis.Jedis;

public class QpsRedisTest {

    public static void main(String[] args) {
        //如果使用網絡,則無法測試性能
        //Jedis jedis = new Jedis("真實IP", 6379);
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.auth("123456");
        int i = 0 ;
        try{

            long l = System.currentTimeMillis();
            while(true){
                long end = System.currentTimeMillis();
                if(end - l>=1000){
                    break;
                }
                i++;
                jedis.set("test:" + i,i+"");
                //下面這行打印代碼,在Linux本地環境測試影響很大,性能差了1倍多
                //System.out.println("current=" + i);
            }

            System.out.println("write qps :" + i + "/s");
            
			i = 0;
            long l2 = System.currentTimeMillis();
            while(true){
                long end = System.currentTimeMillis();
                if(end - l2>=1000){
                    break;
                }
                i++;
                jedis.get("test:1");
                //下面這行打印代碼,在Linux本地環境測試影響很大,性能差了1倍多
                //System.out.println("current=" + i);
            }
            System.out.println("read qps :" + i + "次/s");

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }

    }
}

環境

  • redis-6.0.5
  • jdk-8u121-linux-x64
  • 雲服務器 1核 2GB 1Mbps(3.10.0-862.el7.x86_64 #1 x86_64 x86_64 x86_64 GNU/Linux)

測試

將代碼打成jar包,拷貝到服務器本機,進行測試:

[root@VM_0_13_centos SpringForRedis_jar]# java -jar SpringForRedis.jar 
write qps :27772/s
read qps :34811/s

在我的電腦上測試(網絡因素,僅供參考):

write qps :31/s
read qps :31/s

redis自帶的性能測試:

[root@VM_0_13_centos ~]# redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 10000 -q
ERROR: NOAUTH Authentication required.
ERROR: failed to fetch CONFIG from 127.0.0.1:6379
WARN: could not fetch server CONFIG
SET: 64516.13 requests per second
LPUSH: 63694.27 requests per second

結論

整體來說,雲服務的主機,使用Java的API,讀寫性能並沒有達到書上所說的,而且讀和寫的性能差別不大;但是使用redis的性能測試指令,性能比較高;性能測試需要考慮的點較多,也需要大量的測試比較,今天也只是簡單驗證了下set的讀寫性能,記錄一下,希望後續能夠更深入的學習。

以後有遇到相關的知識,再來掃盲啦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章