我是靠谱客的博主 风中发带,最近开发中收集的这篇文章主要介绍Jedis 读写效率测试 hgetAll hmset,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

测试结果:

1.redis使用三种方式

1)单 Jedis 读写1条命令,读写玩即close()

2) 单Jedis读写多条命令,此种情况尝试不释放Jedis连接,由于Jedis本质是tcp长连接,需要做异常判断 

3)Pipeline方式读写,此种方式效率最高,但是由于将多条命令缓存与Outpustream并在syn()方法时一次性flush(),若本次出现异常,会影响全部命令执行。

2.测试代码

package com.nari.server.Redis.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.nari.server.Redis.RedisFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

import java.lang.Class;
public class Main {

	public static Logger loger  = Logger.getLogger(Main.class); 
	public static JedisPool jedisPool =null;
	public static   void Maintest() {
		// TODO Auto-generated constructor stub
		try(Jedis jedis = jedisPool.getResource()) {
//		try {
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("addr","1" );
			jedis.hmset("test", map);
			System.out.println("---");

		} catch (Exception e) {

			System.out.println("jedis set error:" );
			e.printStackTrace();
			
		}
	
	}
	
    private static  void  hmset(final String key, final Map<String,String> hash) {        
   try( Jedis jedis =RedisFactory.getJedisPool().getResource() ) {
       jedis.hmset(key, hash);
  }catch(Exception e) {
  	e.printStackTrace();
  }
 }

    public static  void main(String[] args) {
    	RedisFactory.initRedis(true);
    	Map<String,String> hash =new HashMap<String,String>();
   
    	Jedis jedis =RedisFactory.getJedisPool().getResource();
    	long time1 = System.currentTimeMillis();
    	for(int i=0;i<100000;i++) {
        	hash.clear();
        	hash.put("k_1" + i, "v_" );
    		hmset("key_1" + i, hash);
    	}
    	long time2 = System.currentTimeMillis();
    	long res1 = time2-time1;
    	loger.info("Jedis直接hmset写单键值MAP(100000条)用时"+res1);
    	
        //使用pipeline hmset
        Pipeline p = jedis.pipelined();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
        	hash.clear();
        	hash.put("k_1" + i, "v_" );
            p.hmset("key_1" + i, hash);
        }
        p.sync();
        long end = System.currentTimeMillis();
    	long res2 = end-start;
    	loger.info("pipeline方式hmset写单键值MAP(100000条)用时:"+res2);
    	
    	long time3 =System.currentTimeMillis();
        //HMGET
    	
    	Set<String> keys0 = jedis.keys("*");
    	Set<String> keys00 = new HashSet<String>();;
    	for(String key : keys0) {
    		if(key.contains("key_1")) {
    			keys00.add(key);
    		}
    		
    	}
    	
        Map<String,Map<String,String>> result = new HashMap<String,Map<String,String>>();
        Set<String> keys = RedisFactory.getJedisPool().getResource().keys("*");
        Map<String,Response<Map<String,String>>> responses = new HashMap<String,Response<Map<String,String>>>(keys.size());
        for(String key : keys00) {
        	responses.put(key, p.hgetAll(key));
        }
        p.sync();
    	long time4 =System.currentTimeMillis();
    	long res3 = time4-time3;
    	loger.info("pipeline 方式 hgetAll 读取read("+responses.size()+")条用时:"+res3);
    	
 //-------------------------------------------------


    	Set<String> keys01 = jedis.keys("*");
    	Set<String> keys001 = new HashSet<String>();;
    	for(String key : keys01) {
    		if(key.contains("key_1")) {
    			keys001.add(key);
    		}
    		
    	}
    	//直接使用Jedis hgetall
    	long startr = System.currentTimeMillis();
    	Map<String,Map<String,String>> result01 = new HashMap<String,Map<String,String>>();
    	for(String key : keys001) {
    	result01.put(key, jedis.hgetAll(key));
    	}
    	long endr = System.currentTimeMillis();
    	long timer = endr-startr;
    	loger.info("hgetAll without pipeline used"+"读取read("+result01.size()+")条用时:" + timer);
      	
    	
    	
    	  MsgBean msg =null;
          List<Device> devicesList;
          List<Data> dataList;
          Device device;
          Data data;
          for(int i = 0; i < 1; i++) { //消息数目
              msg = new MsgBean();
              devicesList = new ArrayList<>();
              for(int j=0; j < 1; j++){ //设备数目
                  device = new Device();
                  device.setDeviceId("12");
                  device.setServiceId("analog");
                  dataList = new ArrayList<>();
                  for(int d =0; d <1; d++){ //设备中data数目
                      data = new Data();
                      data.setPhVAr_phsA1(220);
                      data.setPhV_phsB(1.3);
                      data.setPhV_phsC(1.1);
                      data.setPhV_neut(0.001);
                      data.setA_phsA(0.01);
                      data.setA_phsB(0.01);
                      data.setA_phsC(0.01);
                      data.setHz(50);
                      data.setTotW(1.4);
                      data.setTotVAr(0);
                      data.setTotPF(1);
                      data.setPhW_phsA(0.1);
                      data.setPhW_phsB(0.1);
                      data.setPhW_phsC(0.1);
                      data.setPhVAr_phsA(0.01);
                      data.setPhVAr_phsB(0.01);
                      data.setPhVAr_phsC(0.01);
                      dataList.add(data);
                  }
                  device.setData(dataList);
                  device.setEventTime();
                  devicesList.add(device);
              }
              msg.setDevices(devicesList);
          }

          {
        	 
          	long time5 = System.currentTimeMillis();
          	for(int i=0;i<100000;i++) {
          		hmset("key_1" + i, RedisFactory.BeanToMap(msg));
          	}
          	long time6 = System.currentTimeMillis();
          	long res6 = time6-time5;
          	System.out.println("简单Jedis hmset写物模型实例(100000条)用时:"+res6);
          	
              //使用pipeline hmset
              Pipeline p7 = jedis.pipelined();
              long start7 = System.currentTimeMillis();
              for (int i = 0; i < 100000; i++) {
                  p7.hmset("key_1" + i,RedisFactory.BeanToMap(msg));
              }
              p7.sync();
              long end7 = System.currentTimeMillis();
          	long res7 = end7-start7;
          	loger.info("pipeline方式hmset写物模型实例(100000条)用时:"+res7);
          }
    }
}

RedisFactory本质制作了JedisPool的初始化,可以简化此类,去除无用内容

package com.nari.server.Redis;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import java.util.HashMap;

public class RedisFactory {
	private static Logger loger = Logger.getLogger(RedisFactory.class);
	
	private static JedisPool jedisPool = null;
	private static Jedis _Publisher = null;
	private static Jedis _Realinker = null;
	private static Jedis _PublisherAlarm = null;
	private static Jedis _Listener = null;
	private static final Gson _Jsoner = new Gson();

	public static final void initRedis( Boolean sub) {
		if (jedisPool == null) {
			try {
				JedisPoolConfig config = new JedisPoolConfig();
				// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
				config.setBlockWhenExhausted(true);
				// 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
				config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
				// 是否启用pool的jmx管理功能, 默认true
				config.setJmxEnabled(true);
				// MBean ObjectName = new
				// ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name="
				// + "pool" + i); 默认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
				config.setJmxNamePrefix("pool");
				// 是否启用后进先出, 默认true
				config.setLifo(true);
				// 最大空闲连接数, 默认8个
				config.setMaxIdle(8);
				// 最大连接数, 默认8个
				config.setMaxTotal(8);
				// 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常,
				// 小于零:阻塞不确定的时间,
				// 默认-1
				config.setMinIdle(8);
				config.setMaxWaitMillis(-1);
				// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
				config.setMinEvictableIdleTimeMillis(1800000);
				// 最小空闲连接数, 默认0
				config.setMinIdle(0);
				// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
				config.setNumTestsPerEvictionRun(3);
				// 对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数
				// 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
				config.setSoftMinEvictableIdleTimeMillis(1800000);
				//检验返回有效性
//				config.setTestOnReturn(true);
				// 在获取连接的时候检查有效性, 默认false
				config.setTestOnBorrow(false);
				// 在空闲时检查有效性, 默认false
				config.setTestWhileIdle(true);
				// 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
				config.setTimeBetweenEvictionRunsMillis(-1);
				jedisPool = new JedisPool(config, "localhost", 6379);
				
				loger.info("Redis database initialized");
				
				if(sub) {
					doSubscribeNew("listener", new Listener());
				}else {//重启redis之后 subscribe需要重新设置
					Listener listerner = new Listener();
					 doSubscribeNew("listener", listerner);
				}
			
				
			} catch (Exception e) {
				loger.error(e);
			}
//			jedisPool = new JedisPool("127.0.0.1");
		}
	}
	
	private static final void doSubscribeNew(String channel, JedisPubSub sub) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					try(Jedis jedis = RedisFactory.jedisPool.getResource()){
						jedis.subscribe(sub, channel);	
					}catch(Exception e) {
						e.printStackTrace();
					}	
				}
			}
			
		}).start();

	}
	public  static final Jedis getInstance() {
		return jedisPool.getResource();
	}
		
	public static synchronized final Jedis getPublisher() {
		if(jedisPool.isClosed()) {
			return null;
		}
		if(_Publisher==null) {
			_Publisher = getInstance();
		}		
		return _Publisher;
	}

	protected static synchronized final Jedis getListener() {
		if(jedisPool.isClosed()) {
			return null;
		}
		if(_Listener==null) {
			_Listener = getInstance();
		}		
		return _Listener;
	}
	
	protected static synchronized final Jedis getPublisherAlarm() {
		if(jedisPool.isClosed()) {
			return null;
		}
		if(_PublisherAlarm==null) {
			_PublisherAlarm = getInstance();
		}		
		return _PublisherAlarm;
	}
	public Jedis getJedis() {
		return jedisPool.getResource();
	}
	
	protected static synchronized final Jedis getRealinker() {
		if(jedisPool.isClosed()) {
			return null;
		}
		if(_Realinker==null) {
			_Realinker =  getInstance();
		}		
		
		return _Realinker;
	}
	
	
	public static JedisPool getJedisPool() {
		return jedisPool;
	}


	public static void setJedisPool(JedisPool jedisPool) {
		RedisFactory.jedisPool = jedisPool;
	}


	public static void releaseJedisSource() {
		if(_Listener!=null) {
			_Listener.close();
		}
		
		if (null != jedisPool) {
				 jedisPool.close();
				 jedisPool=null;
		        }
	}
	public static Map<String, String> BeanToMap(Object  bean) {
		Map<String, String> beanMap = new HashMap<String, String>();
		try {
			PropertyDescriptor[] PDS = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
			for (PropertyDescriptor pd : PDS) {
				String propertyName = pd.getName();
				if (propertyName != null && !propertyName.equals("class")) {
					Method getter = pd.getReadMethod();
					if (getter != null) {
						//可用注解标识优化处理
						//getter.getAnnotation(null); 
						beanMap.put(propertyName,_Jsoner.toJson(getter.invoke(bean)));
					}
				}
			}
		} catch (Exception e) {
			loger.error("BeanToMap error "+e);
			if(loger.isInfoEnabled())
				e.printStackTrace();			
		}
		return beanMap;
	}
	
	protected static <T> T MapToBean( Map<String, String> beanMap, Class<T> clazz) {
		T bean = null;
		try {
			bean = clazz.newInstance();
			PropertyDescriptor[] PDS = Introspector.getBeanInfo(clazz).getPropertyDescriptors();
			for (PropertyDescriptor pd : PDS) {
				String propertyName = pd.getName();
				if (propertyName != null && !propertyName.equals("class")) {
					Method setter = pd.getWriteMethod();
					String value = beanMap.get(propertyName);
					//String type = pd.getPropertyType().getName();
					Class<?> type = pd.getPropertyType();
					if (setter != null && value != null	&& !value.equalsIgnoreCase("null")) {
						//Object obj = value(value, type);
						Object obj = _Jsoner.fromJson(value, type);
						if (obj != null) {
							setter.invoke(bean, obj);
						}
					}
				}
			}
		} catch (Exception e) {
			loger.error(e);
			if(loger.isInfoEnabled())
				e.printStackTrace();
		}
		return bean;
	}

	/**
	 * 单个集中器的总量统计 
	 * 单个集中器单个指令的统计 
	 * 全部数量的统计
	 * */
	 public static void numInc(long objLong,short NUM_ID_SHORT) {
		
		}
	 /**
	  * 获得上行的报文时,写redis中对应的总数和每个分量**/
	 public static void numIncRcv(long objLong,short NUM_ID_SHORT) {
		
		}
	    /**
	     *增加总报文数量统计
	     * **/
	 public static void numIncALL(short NUM_ID_SHORT) {

	 }
	 
	 /**
	     *增加返回总报文数量统计
	     * **/
	 public static void numIncALLRcv(short NUM_ID_SHORT) {

	 }
	
		 
}

完整项目见:https://download.csdn.net/download/mychinasty/12055377 后续补上链接,mavean项目包含全部Jar包,只需要执行Main类中的main方法即可完美运行

3.测试结果

最后

以上就是风中发带为你收集整理的Jedis 读写效率测试 hgetAll hmset的全部内容,希望文章能够帮你解决Jedis 读写效率测试 hgetAll hmset所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部