Skip to content

Instantly share code, notes, and snippets.

@addozhang
Created March 16, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addozhang/cd8551af4ca008c1f86896b9232c7a3b to your computer and use it in GitHub Desktop.
Save addozhang/cd8551af4ca008c1f86896b9232c7a3b to your computer and use it in GitHub Desktop.
Key长度对Redis性能的影响
package com.atbug.test;
import redis.clients.jedis.Jedis;
import java.util.Random;
/**
* Created by addo on 2017/3/16.
*/
public class RedisTest {
private static String[] keys = new String[1000];
private static String randomString(int length) {
Random random = new Random();
char[] chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
StringBuilder key = new StringBuilder(length);
int i = length;
while (i > 0) {
key.append(chars[random.nextInt(chars.length)]);
i--;
}
return key.toString();
}
private static void write(Jedis jedis, int length) {
long start = System.currentTimeMillis();
String key = null;
String value = null;
for (int i = 0; i < 1000; i++) {
key = randomString(length);
keys[i] = key;
value = randomString(1000);
jedis.set(key, value);
}
System.out.println(String.format("put key length %d with value length 1000 in 1000 tims costs: %d ms", length, System.currentTimeMillis() - start));
}
private static void read(Jedis jedis, int length) {
long start = System.currentTimeMillis();
for (int i = 0; i < keys.length; i++) {
jedis.get(keys[i]);
}
System.out.println(String.format("get key length %d with value length 1000 in 1000 tims costs: %d ms", length, System.currentTimeMillis() - start));
}
public static void main(String[] args) throws InterruptedException {
Jedis jedis = new Jedis("localhost", 6379);
int[] lengths = {10, 100, 500, 1000, 2500, 5000, 7500, 10000, 20000};
for (int i = 0; i < lengths.length; i++) {
write(jedis, lengths[i]);
read(jedis, lengths[i]);
keys = new String[1000];
jedis.flushAll();
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment