redis和ssdb讀取效能對比

smark發表於2014-08-13

      最近關注了一下ssdb,它的特點是基於檔案儲存系統所以它支撐量大的資料而不因為記憶體的限制受取約束.從官網的測試報告來看其效能也非常出色和redis相當,因此可以使用它來代替redis來進行k-v資料業務的處理.想法總是美好的,不過現實中就可能帶點骨感.

    幸好ssdb是相容redis的部份協議,所以直接用redis client庫就可以進行一個壓力測試.以於針對Redis和ssdb的幾個讀操進行一個簡單的效能測試對比,這個測試不是直接在本機呼叫Redis和ssdb. 而是通過一個程式在別的伺服器上呼叫.測試指令(get,hget,lregion)以下是測試結果截圖

測試程式碼

 class Test
    {
        long mCount = 0;

        long mIndex = 0;

        private bool mRuning = true;

        public long Count
        {
            get
            {
                return mCount;
            }
        }

        public void Execute()
        {
            Console.WriteLine("* -----------------------------------------------");
            Console.WriteLine("* redis get");
            
            ConsoleWait.Start();
            string result = OnTest(Config.RedisClient,GetHandler);
            ConsoleWait.End();
            Console.WriteLine(result);

            Console.WriteLine("* ssdb get");
            ConsoleWait.Start();
            result = OnTest(Config.SSDBClient, GetHandler);
            ConsoleWait.End();
            Console.WriteLine(result);
            Console.WriteLine("* -----------------------------------------------");

            //
            Console.WriteLine("* redis lregion[1-2]");
            ConsoleWait.Start();
            result = OnTest(Config.RedisClient, LRegionHandler1TO2);
            ConsoleWait.End();
            Console.WriteLine(result);

            Console.WriteLine("* ssdb lregion[1-2]");
            ConsoleWait.Start();
            result = OnTest(Config.SSDBClient, LRegionHandler1TO2);
            ConsoleWait.End();
            Console.WriteLine(result);
            Console.WriteLine("* -----------------------------------------------");

            //
            Console.WriteLine("* redis lregion[50-60]");
            ConsoleWait.Start();
            result = OnTest(Config.RedisClient, LRegionHandler1TO2);
            ConsoleWait.End();
            Console.WriteLine(result);

            Console.WriteLine("* ssdb lregion[50-60]");
            ConsoleWait.Start();
            result = OnTest(Config.SSDBClient, LRegionHandler1TO2);
            ConsoleWait.End();
            Console.WriteLine(result);
            Console.WriteLine("* -----------------------------------------------");

            //
            Console.WriteLine("* redis lregion[100-110]");
            ConsoleWait.Start();
            result = OnTest(Config.RedisClient, LRegionHandler1TO2);
            ConsoleWait.End();
            Console.WriteLine(result);


            Console.WriteLine("* ssdb lregion[100-110]");
            ConsoleWait.Start();
            result = OnTest(Config.SSDBClient, LRegionHandler1TO2);
            ConsoleWait.End();
            Console.WriteLine(result);
            Console.WriteLine("* -----------------------------------------------");

            //
            Console.WriteLine("* redis hget");
            ConsoleWait.Start();
            result = OnTest(Config.RedisClient, HGetHandler);
            ConsoleWait.End();
            Console.WriteLine(result);

            Console.WriteLine("* ssdb hget");
            ConsoleWait.Start();
            result = OnTest(Config.SSDBClient, HGetHandler);
            ConsoleWait.End();
            Console.WriteLine(result);
            Console.WriteLine("* -----------------------------------------------");
        }

        private void HGetHandler(RedisClient e)
        {
            while (mRuning)
            {
                long index = System.Threading.Interlocked.Increment(ref mIndex);
                ProtobufKey key = "user_" + Data.Import.Users[(int)(index % Data.Import.Users.Count)].Name;
                key.Get<Model.Order, Model.Employee, Model.Customer>(e);
                System.Threading.Interlocked.Increment(ref mCount);
            }
        }

        private void LRegionHandler1TO2(RedisClient e)
        {
            while (mRuning)
            {
                ProtobufList<Model.Order> list = "Orders";
                list.Range(1, 2, e);
                System.Threading.Interlocked.Increment(ref mCount);
            }
        }

        private void LRegionHandler50TO60(RedisClient e)
        {
            while (mRuning)
            {
                ProtobufList<Model.Order> list = "Orders";
                list.Range(50, 60, e);
                System.Threading.Interlocked.Increment(ref mCount);
            }
        }
        private void LRegionHandler100TO110(RedisClient e)
        {
            while (mRuning)
            {
                ProtobufList<Model.Order> list = "Orders";
                list.Range(100, 110, e);
                System.Threading.Interlocked.Increment(ref mCount);
            }
        }

        private  void GetHandler(RedisClient e)
        {
            while (mRuning)
            {
                long index = System.Threading.Interlocked.Increment(ref mIndex);
                ProtobufKey key = "user_" + Data.Import.Users[(int)(index % Data.Import.Users.Count)].Name;
                key.Get<Model.User>(e);
                System.Threading.Interlocked.Increment(ref mCount);
            }
        }

        private string OnTest(RedisClient client,Action<RedisClient> handler)
        {
            mCount = 0;
            mRuning = true;
            for (int i = 0; i < 20; i++)
            {
                System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                {

                    GetHandler((RedisClient)o);
                }, client);
            }
            int s = 20;
            while (s > 0)
            {
                System.Threading.Thread.Sleep(1000);
                s--;
            }
            mRuning = false;
            System.Threading.Thread.Sleep(1000);
            return string.Format("* [seconds:{1}/total:{0}]", mCount, mCount / 20);
        }
    }

    從測試結果看來差距還是非常明顯,並不象官網那樣說得這麼理想.雖然SSDB效率上不如REDIS,但其基於磁碟儲存有著其最大的優勢,畢竟很多業務資料遠超過伺服器記憶體的容量.

     SSDB的測試結果不理想也許是硬體環境受限,如果有個SSD硬碟的測試環境估計也得到一個更好的結果,但在測試過程中發現一個問題就是SSDB佔用的CPU資源也是非常大的,在以上測試過程SSDB的併發效率比不上REDIS,同時CPU損耗上基本要比REDIS高出一倍的樣子.

    以上測試結果緊緊是是一些情況下的效能測試對比,不能完全表述出兩者在應用的差距的結果,如果需要用到這些產品的同學不防在實施前進行一些測試為實施選擇提供一個更可靠的結果.

相關文章