redis分布锁Redisson性能测试

综述

  redisson是一个用于连接RedisJava客户端工作,相对于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
  • 2
  • 3
  • 4
  • 5
  • 6

总结分析

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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱分享的淘金达人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值