java Redis Cluster写入/读取测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Redis Cluster写入/读取测试相关的知识,希望对你有一定的参考价值。

package cn.jpush.sms.test;

import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * Redis Cluster Test
 * Created by xiezefan on 15/11/15.
 */
public class RedisClusterTest {

    private static JedisCluster redisCluster;

    private static void init() {

        Set<HostAndPort> redisClusterNodes = new HashSet<>();
        redisClusterNodes.add(new HostAndPort("10.211.55.4", 7000));
        redisClusterNodes.add(new HostAndPort("10.211.55.4", 7001));
        redisClusterNodes.add(new HostAndPort("10.211.55.4", 7002));
        redisClusterNodes.add(new HostAndPort("10.211.55.4", 7003));
        redisClusterNodes.add(new HostAndPort("10.211.55.4", 7004));
        redisClusterNodes.add(new HostAndPort("10.211.55.4", 7005));
        redisCluster = new JedisCluster(redisClusterNodes);
    }

    public static void testSet() {
        init();
        int failTimes = 0;
        long before = System.currentTimeMillis();
        for (long i=1000000000; i<1000100000L; i++) {
            long b = System.currentTimeMillis();
            try {
                redisCluster.set(i + ":" + i, "1") ;
                long a = System.currentTimeMillis();
            } catch (Exception e) {
                long a = System.currentTimeMillis();
                System.out.println(String.format("第%s次插入失败, 耗时:%s, Error:%s", i+1, a-b, e.getMessage()));
                failTimes++;
            }

        }

        long after = System.currentTimeMillis();
        System.out.println("Cost time " + (after - before));
        System.out.println("Fail times " + failTimes);
    }

    public static void testGet() {
        init();
        int found = 0;
        long before = System.currentTimeMillis();
        for (long i=1000000000; i<1000100000L; i++) {
            String key = i + ":" + i;
            try {
                String value = redisCluster.get(key);
                if (StringUtils.isEmpty(value)) {
                    System.out.println(key + " not found");
                } else {
                    found++;
                }
            } catch (Exception e) {
                System.out.println(key + " not found");
            }

        }
        long after = System.currentTimeMillis();
        System.out.println("Cost time " + (after - before));
        System.out.println("Total founded " + found);
    }

    public static void main(String[] args) {
        testSet();
//        testGet();
    }
}

以上是关于java Redis Cluster写入/读取测试的主要内容,如果未能解决你的问题,请参考以下文章

Docker实战之Redis-Cluster集群

Redis Cluster 集群

Java使用jedis连接NoSQL数据库Redis写入数据并读取出来

Java使用jedis连接NoSQL数据库Redis写入数据并读取出来

使用Python脚本实现RedisCluster集群写入

一 java 中使用redis 测试Redis的写入性能