Created
December 3, 2015 01:24
-
-
Save xiezefan/4bd5e0d0c264aadaf061 to your computer and use it in GitHub Desktop.
Redis Cluster 写入/读取 测试
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment