Yazan : Ersin Aksoy
Bilgisayar Yuksek Muhendisi

AMAÇ

Bu çalışmada Memcached, HazelCast, Redis cache mekanizmaları 100 K lik PNG veriler kullanılarak okuma hızlarına göre karşılaştırılmıştır.

YÖNTEM

Önbelleğin (Cache) kullanıldığı ortam Java Servlet olarak belirlenmiştir. 10000 adet key-value kullanılmıştır. Tüm PNG dosyaları Cache lendikten sonra okuma işlemine başlanmıştır. Servlet kodları ekte sunulmuştur.

ÖLÇÜMLER

1- HazelCast PNG OKU

 

 

2- Jedis PNG OKU


3- Memcached PNG OKU

 

SONUÇ

HazelCast ve Memcached okuma hızlarına göre çok yakın sonuçlar vermiş (200 T için 90 ms) tir. Redis benzer koşullarda çok kötü değerler (200 T için 7093 ms) üretmiştir.

 

KAYNAK KODLAR

EK 1 – HAZEL CAST

@WebServlet(name = "Hazel", urlPatterns = {"/Hazel"})
public class Hazel extends HttpServlet {
    private static IMap cache = null;
    private static HazelcastInstance client = null;
    private static Object _synchObject =new Object() ;

    protected void initHazelClient() {
        if (cache == null) {
            synchronized (_synchObject) {
                if (cache == null) {
                    ClientConfig clientConfig = new ClientConfig();
                    clientConfig.getGroupConfig().setName("dev").setPassword("dev-pass");
                    clientConfig.addAddress("localhost");
                    client = HazelcastClient.newHazelcastClient(clientConfig);
                    cache = client.getMap("pngs");
                }

            }

        }

    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        initHazelClient();
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        try {

            String path = request.getQueryString();
            byte[] bytes = cache.get(path);
            if (bytes == null) {
                //-------------------
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                FileInputStream fis = new FileInputStream(new File("c:/temp/tile/" + path));
                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    baos.write(buf, 0, len);
                }
                fis.close();
                baos.close();

                bytes = baos.toByteArray();
                cache.put(path, bytes);
                //--------
            }

            out.write(bytes);
            out.flush();
        } finally {
            out.close();
        }}

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

 
}

EK 2- MEMCACHED

package cache;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.spy.memcached.MemcachedClient;

@WebServlet(name = "Memcached", urlPatterns = {"/Memcached"})
    public class Memcached extends HttpServlet {
 
    private static  MemcachedClient    cache = null;
    private static Object _synchObject=new Object(); 
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
         if (cache == null) {
            synchronized (_synchObject) {
                if (cache == null) {
                       cache = new MemcachedClient( new InetSocketAddress("10.100.1.251", 11211));
                }
            }
        }
         
         response.setContentType("image/png");
        OutputStream out = response.getOutputStream();

        try {
            String path = request.getQueryString();
            byte[] bytes = (byte[]) cache.get(path);
            if (bytes == null) {
                //-------------------
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                FileInputStream fis = new FileInputStream(new File("c:/temp/tile/" + path));
                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    baos.write(buf, 0, len);
                }
                fis.close();
                baos.close();

                bytes = baos.toByteArray();
                cache.set(path, 0, bytes);
                //--------
            }
            out.write(bytes);
            out.flush();
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

}

EK 3- REDIS

package cache;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@WebServlet(name = "Redis", urlPatterns = {"/Redis"})
public class Redis extends HttpServlet {

   private static final JedisPool jedisPool =  new JedisPool("localhost", 6379);
    private static final Jedis cache = jedisPool.getResource(); 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
               
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();

        try {
            String path = request.getQueryString();
             byte[] bytes = cache.get(path.getBytes());         
     
            if (bytes == null) {
                //-------------------
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                FileInputStream fis = new FileInputStream(new File("c:/temp/tile/" + path));
                byte[] buf = new byte[1024];
                int len;
                while ((len = fis.read(buf)) > 0) {
                    baos.write(buf, 0, len);
                }
                fis.close();
                baos.close();

                bytes = baos.toByteArray();
              
                
                cache.set(path.getBytes(), bytes);
                //--------
            }

            out.write(bytes);
            out.flush();
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

}

Yorumlar

  1. Anonim

    Konu hakkında yapılan daha geniş bir benchmark testinde (daha fazla ürün test ediliyordu) belli büyüklükten sonra redis’in durumunun değiştiğini gösteriyordu. Ek bilgi olarak eklenmeli.

Anonim için bir cevap yazın Cevabı iptal et

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir