Last active
June 7, 2022 10:40
-
-
Save DanielYWoo/33b8f2e635b3723b28f4bf4b2f9e19b0 to your computer and use it in GitHub Desktop.
Redis Benchmark
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
import redis.clients.jedis.Jedis; | |
public class RedisBenchmark { | |
public static void main(String[] args) { | |
Jedis jedis = new Jedis("127.0.0.1", 6379); | |
//warm up | |
for (int i = 0; i < 1000; i++) | |
jedis.set("a", "1"); | |
System.out.println("value size, TPS (write), TPS (read)"); | |
extracted(jedis, 10, 100000); | |
extracted(jedis, 100, 100000); | |
extracted(jedis, 1024, 100000); | |
extracted(jedis, 1024 * 10, 100000); | |
extracted(jedis, 1024 * 100, 100000); | |
extracted(jedis, 1024 * 1024, 50000); | |
extracted(jedis, 1024 * 1024 * 10, 50000); | |
extracted(jedis, 1024 * 1024 * 100, 1000); | |
} | |
private static void extracted(Jedis jedis, long len, int loop) { | |
String value = buildString(len); | |
int err = 0; | |
long t1 = System.nanoTime(); | |
for (int i = 0; i < loop; i++) { | |
try { | |
jedis.set(String.format("%010d", i), value); | |
} catch (Exception e) { | |
err++; | |
jedis = new Jedis("127.0.0.1", 6379); | |
} | |
} | |
long t2 = System.nanoTime(); | |
double ms = (t2 - t1) / 1000000.0; | |
// System.out.println("set " + len); | |
// System.out.println("err=" + err); | |
// System.out.println("time (ms) =" + ms); | |
// System.out.println("throughput(mbps)=" + (len * loop * 8 / 1024.0 / 1024.0 / ms * 1000)); | |
// System.out.println("throughput(TPS)=" + (loop / ms * 1000)); | |
System.out.print(len); | |
System.out.print(","); | |
System.out.print(loop / ms * 1000); | |
err = 0; | |
t1 = System.nanoTime(); | |
for (int i = 0; i < loop; i++) { | |
try { | |
jedis.get(String.format("%010d", i)); | |
} catch (Exception ex) { | |
err++; | |
jedis = new Jedis("127.0.0.1", 6379); | |
} | |
} | |
t2 = System.nanoTime(); | |
ms = (t2 - t1) / 1000000.0; | |
System.out.print(","); | |
System.out.println(loop / ms * 1000); | |
// System.out.println("get " + len); | |
// System.out.println("err=" + err); | |
// System.out.println("time (ms) =" + ms); | |
// System.out.println("throughput(mbps)=" + (len * loop * 8 / 1024.0 / 1024.0 / ms * 1000)); | |
// System.out.println("throughput(TPS)=" + (loop / ms * 1000)); | |
} | |
private static String buildString(long len) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < len; i++) { | |
sb.append('0'); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment