redis分布锁Redisson性能测试

综述

  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. 后续采用异步回调的方式看看分布式锁的性能是否有提高
    原文作者:java锁
    原文地址: https://blog.csdn.net/everlasting_188/article/details/51073505
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞