Skip to content

Instantly share code, notes, and snippets.

@ka-ka-xyz ka-ka-xyz/Perftest.java
Last active Dec 4, 2017

Embed
What would you like to do?
Redis perftest to get max score.
package example;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.ZParams.Aggregate;
public class Perftest {
private JedisPool pool;
public static void main(String[] args) {
System.out.println("start");
Perftest perftest = new Perftest(Integer.valueOf(args[0]));
int scores =10;
perftest.zscoreThenZadd("case1", scores);
perftest.zscoreThenZaddInScript("case2", scores);
perftest.saddAllPreProcessedData("case3_pre", scores);
perftest.smembersAllPreProcessedDataThenAggregateAndZadd("case3_pre", "case3");
perftest.zunionstore("case4", scores);
perftest.zunionstore2("case5", scores);
perftest.zunionstore2WithTransaction("case6", scores);
System.out.println("end");
}
public Perftest(int size) {
this.pool = new JedisPool(new JedisPoolConfig(), "localhost");
this.data = initData(size);
}
private Map<String, Double> data;
private Map<String, Double> initData(int size) {
Map<String, Double> data = new HashMap<String, Double>();
System.out.println("------------ Generated value - score pair. ------------");
for (int i = 0; i < size; i ++) {
UUID uuid = UUID.randomUUID();
System.out.println(uuid + ", " + Math.abs(Double.valueOf(uuid.getMostSignificantBits())));
data.put(uuid.toString(), Math.abs(Double.valueOf(uuid.getMostSignificantBits())));
}
System.out.println("------------------------");
return data;
}
private void del(String key) {
try (Jedis jedis = this.pool.getResource()){
jedis.del(key);
}
}
private void zscoreThenZadd(String key, int loop) {
del(key);
long start = System.currentTimeMillis();
for (String value : data.keySet()) {
for (int i = 1; i <= loop; i++) {
try (Jedis jedis = this.pool.getResource()){
Double currentScore = data.get(value) * i;
Double oldScore = jedis.zscore(key, value);
if (oldScore == null || currentScore.compareTo(oldScore) > 0) {
jedis.zadd(key, currentScore, value);
}
}
}
}
long total = System.currentTimeMillis() - start;
System.out.println(key + ":" + total);
}
private final String scriptTemplate =
"local new_score = tonumber(KEYS[1])\n" +
"local data = KEYS[2]\n" +
"local bulk_old_score = redis.call('zscore', '%s', data)\n" +
"local old_score = -1\n" +
"if bulk_old_score then old_score = tonumber(bulk_old_score) end\n" +
"if old_score < new_score then\n" +
" return redis.call('zadd', '%s', new_score, data)\n" +
"else\n" +
" return nil\n" +
"end";
private void zscoreThenZaddInScript(String key, int loop) {
del(key);
String sha = null;
try (Jedis jedis = this.pool.getResource()){
String script = String.format(scriptTemplate, key, key);
sha = jedis.scriptLoad(script);
}
long start = System.currentTimeMillis();
for (String value : data.keySet()) {
for (int i = 1; i <= loop; i++) {
try (Jedis jedis = this.pool.getResource()){
Double currentScore = data.get(value) * i;
String[] args = {currentScore.toString(), value};
jedis.evalsha(sha, args.length, args);
}
}
}
long total = System.currentTimeMillis() - start;
System.out.println(key + ":" + total);
}
private static final String KV_SPLITTER = "::";
private void saddAllPreProcessedData(String preProcessedStoreKey, int loop) {
del(preProcessedStoreKey);
long start = System.currentTimeMillis();
for (String value : data.keySet()) {
for (int i = 1; i <= loop; i++) {
try (Jedis jedis = this.pool.getResource()){
String score = String.valueOf(data.get(value) * i);
jedis.sadd(preProcessedStoreKey, value + KV_SPLITTER + score);
}
}
}
long total = System.currentTimeMillis() - start;
System.out.println(preProcessedStoreKey + ":" + total);
}
private void smembersAllPreProcessedDataThenAggregateAndZadd(String preProcessedStoreKey, String key) {
del(key);
try (Jedis jedis = this.pool.getResource()){
long start = System.currentTimeMillis();
Set<String> preProcessed = jedis.smembers(preProcessedStoreKey);
jedis.del(preProcessedStoreKey);
Map<String, Double> map = new HashMap<String, Double>();
for (String kv : preProcessed) {
String[] kvAry = kv.split(KV_SPLITTER);
String value = kvAry[0];
Double score = Double.valueOf(kvAry[1]);
if (map.putIfAbsent(value, score) != null) {
map.computeIfPresent(value, (val, oldScore) -> {
return oldScore < score? score: oldScore;
});
}
}
jedis.zadd(key,map);
long total = System.currentTimeMillis() - start;
System.out.println(key + ":" + total);
}
}
private void zunionstore(String key, int loop) {
del(key + "::*");
long start = System.currentTimeMillis();
for (String value : data.keySet()) {
for (int i = 1; i <= loop; i++) {
try (Jedis jedis = this.pool.getResource()){
Double currentScore = data.get(value) * i;
jedis.zadd(key + "::" + value, currentScore, value);
}
}
}
try (Jedis jedis = this.pool.getResource()) {
Set<String> keys = jedis.keys(key + "::*");
double[] weights = new double[keys.size()];
Arrays.fill(weights, 1d);
ZParams params = new ZParams();
params.weightsByDouble(weights);
params.aggregate(Aggregate.MAX);
jedis.zunionstore(key, params, keys.toArray(new String[keys.size()]));
del(key + "::*");
}
long total = System.currentTimeMillis() - start;
System.out.println(key + ":" + total);
}
private void zunionstore2(String key, int loop) {
long start = System.currentTimeMillis();
for (String value : data.keySet()) {
for (int i = 1; i <= loop; i++) {
try (Jedis jedis = this.pool.getResource()){
Double currentScore = data.get(value) * i;
String tempStoreKey = key + "::" + UUID.randomUUID().toString();
jedis.zadd(tempStoreKey, currentScore, value);
double[] weights = new double[2];
Arrays.fill(weights, 1d);
ZParams params = new ZParams();
params.weightsByDouble(weights);
params.aggregate(Aggregate.MAX);
jedis.zunionstore(key, params, tempStoreKey, key);
jedis.del(tempStoreKey);
}
}
}
long total = System.currentTimeMillis() - start;
System.out.println(key + ":" + total);
}
private void zunionstore2WithTransaction(String key, int loop) {
long start = System.currentTimeMillis();
for (String value : data.keySet()) {
for (int i = 1; i <= loop; i++) {
try (Jedis jedis = this.pool.getResource()){
jedis.watch(key);
Transaction t = jedis.multi();
Double currentScore = data.get(value) * i;
String tempStoreKey = key + "::" + UUID.randomUUID().toString();
t.zadd(tempStoreKey, currentScore, value);
double[] weights = new double[2];
Arrays.fill(weights, 1d);
ZParams params = new ZParams();
params.weightsByDouble(weights);
params.aggregate(Aggregate.MAX);
t.zunionstore(key, params, tempStoreKey, key);
t.del(tempStoreKey);
t.exec();
}
}
}
long total = System.currentTimeMillis() - start;
System.out.println(key + ":" + total);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jp.gr.java.conf.ka_ka_xyz</groupId>
<artifactId>redis_perftest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis_perftest</name>
<dependencies>
<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.7.0</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.