redis分布锁Redisson性能测试

java 同时被 2 个专栏收录
42 篇文章 0 订阅

#综述
  redisson是一个用于连接redis的java客户端工作,相对于jedis,是一个采用异步模型,大量使用netty promise编程的客户端框架,需要测试性能。
#代码

import java.util.concurrent.CountDownLatch;

import org.redisson.Config;
import org.redisson.Redisson;
import org.redisson.RedissonClient;
import org.redisson.core.RAtomicLong;
import org.redisson.core.RLock;

/**
 * redis的分布式锁测试
 * @author zhaoyq
 */
public class RedissonPerformanceTest {
	private static int TOTAL_PRE_THREADS = 10;
	private static long SECONDS = 10;
	protected static boolean isSkipLock = false;

	public static void main(String[] args) {
		Config config = new Config();
		// for single server
		config.useSingleServer()
		.setAddress("10.10.208.32:7003")
		.setConnectionPoolSize(10);
		final RedissonClient redissonCli = Redisson.create(config);

		final RLock lock = redissonCli.getLock("testLock");
		final CountDownLatch startLatch = new CountDownLatch(1);
		final CountDownLatch endLatch = new CountDownLatch(TOTAL_PRE_THREADS);
		Thread[]  threads = new Thread[TOTAL_PRE_THREADS];
		setZero(redissonCli);
		
		for (int i = 0; i < TOTAL_PRE_THREADS; i++) {
			PerformanceThread runningThread = new PerformanceThread(startLatch,endLatch,lock,redissonCli);
			runningThread.start();
			threads[i]=runningThread;
		}

		//开始执行
		startLatch.countDown();
		try {
			Thread.sleep(SECONDS*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		for (int i = 0; i < threads.length; i++) {
			PerformanceThread runningThread =(PerformanceThread)threads[i];
			runningThread.stopThrad();
		}
		
		try {
			endLatch.await();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		
		printCount(redissonCli);
		redissonCli.shutdown();
	}
	
	
	public static class  PerformanceThread extends Thread{
		private volatile boolean isRunning = true;
		private CountDownLatch startLatch ;
		private CountDownLatch endLatch;
		private RLock lock;
		private RedissonClient redissonCli;
		private PerformanceThread(CountDownLatch startLatch ,CountDownLatch endLatch,RLock lock ,RedissonClient redissonCli){
			this.startLatch = startLatch;
			this.endLatch = endLatch;
			this.lock = lock;
			this.redissonCli =redissonCli;
		}

		@Override
		public void run() {
			try {
				startLatch.await();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			
			while (isRunning) {
				try {
					if (!isSkipLock) {
						lock.lock();
					}
					testUpdate(redissonCli);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (!isSkipLock) {
							lock.unlock();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			
			endLatch.countDown();
		}
		
		public void stopThrad(){
			isRunning = false;
		}
	}

	/**
	 * 更新对应的记录
	 */
	public static void testUpdate(RedissonClient redissonCli) {
		RAtomicLong addLong = redissonCli.getAtomicLong("test_performance");
		long totalUsed = addLong.addAndGet(1);
	}
	
	/**
	 * 最后获得总数
	 * @param redissonCli
	 */
	public static void printCount(RedissonClient redissonCli){
		RAtomicLong addLong = redissonCli.getAtomicLong("test_performance");
		System.out.println("count tps is:"+(addLong.get()/SECONDS));
	}
	
	/**
	 * 初始化
	 * @param redissonCli
	 */
	public static void setZero(RedissonClient redissonCli){
		RAtomicLong addLong = redissonCli.getAtomicLong("test_performance");
		addLong.set(0);
	}
}

#总结分析

  1. isSkipLock为false运行开启分布式锁,压力测试tps在300笔左右
  2. isSkipLock为true关闭分布式锁,tps在1万笔左右
  3. redisson在各种异常的情况下很强壮,比如非正常关闭等,但是性能确实有比较大的损耗,后续比较下其他实现方案或者看看代码看有什么需要优化的
  4. 后续采用异步回调的方式看看分布式锁的性能是否有提高
  • 1
    点赞
  • 3
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值