Last active
July 10, 2025 13:15
-
-
Save raphaeldelio/e5bcfc5cc501ea9699bb3615d0a8e963 to your computer and use it in GitHub Desktop.
pipelining-sample
This file contains hidden or 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.*; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class RedisStringBenchmarkTest { | |
| static final int NUM_OBJECTS = 100_000; | |
| static final int OBJECT_SIZE = 50000; // bytes | |
| static final int BATCH_SIZE = 10_000; // control how many commands per pipeline execution | |
| // Generates a fixed-size string of OBJECT_SIZE characters | |
| public static String generateFixedSizeString(int length) { | |
| return "X".repeat(length); | |
| } | |
| public static void main(String[] args) { | |
| JedisPoolConfig poolConfig = new JedisPoolConfig(); | |
| poolConfig.setMaxTotal(128); | |
| try (JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379, 30000)) { | |
| try (Jedis jedis = jedisPool.getResource()) { | |
| jedis.flushDB(); // Clear Redis before benchmark | |
| } | |
| // INSERT BENCHMARK (PIPELINED) | |
| long insertStart = System.nanoTime(); | |
| try (Jedis jedis = jedisPool.getResource()) { | |
| try (Pipeline pipeline = jedis.pipelined()) { | |
| for (int i = 0; i < NUM_OBJECTS; i++) { | |
| String key = "obj:" + i; | |
| String value = generateFixedSizeString(OBJECT_SIZE); | |
| pipeline.set(key, value); | |
| // Optionally sync in batches to avoid OOM | |
| if (i % BATCH_SIZE == 0) { | |
| pipeline.sync(); | |
| } | |
| } | |
| pipeline.sync(); // Final sync to flush remaining | |
| } | |
| long insertEnd = System.nanoTime(); | |
| double insertTimeMs = (insertEnd - insertStart) / 1_000_000.0; | |
| System.out.printf("Pipelined insertion of %d Redis string objects of %d bytes each took %.2f ms%n", NUM_OBJECTS, OBJECT_SIZE, insertTimeMs); | |
| double insertOpsPerSec = NUM_OBJECTS / (insertTimeMs / 1000.0); | |
| double insertOpsPerMs = NUM_OBJECTS / insertTimeMs; | |
| System.out.printf("Insert throughput: %.2f ops/sec, %.2f ops/ms%n", insertOpsPerSec, insertOpsPerMs); | |
| } | |
| // READ BENCHMARK | |
| long readStart = System.nanoTime(); | |
| long dummySum = 0; | |
| try (Jedis jedis = jedisPool.getResource()) { | |
| try (Pipeline pipeline = jedis.pipelined()) { | |
| List<Response<String>> responses = new ArrayList<>(NUM_OBJECTS); | |
| for (int i = 0; i < NUM_OBJECTS; i++) { | |
| String key = "obj:" + i; | |
| responses.add(pipeline.get(key)); | |
| if (i % BATCH_SIZE == 0) { | |
| pipeline.sync(); | |
| } | |
| } | |
| pipeline.sync(); // Final sync | |
| for (Response<String> response : responses) { | |
| String value = response.get(); | |
| if (value != null) { | |
| dummySum += value.charAt(0); // Prevent dead code elimination | |
| } | |
| } | |
| } | |
| long readEnd = System.nanoTime(); | |
| double readTimeMs = (readEnd - readStart) / 1_000_000.0; | |
| System.out.printf( | |
| "Pipelined reading of %d Redis string objects took %.2f ms (dummySum=%d)%n", | |
| NUM_OBJECTS, readTimeMs, dummySum | |
| ); | |
| double readOpsPerSec = NUM_OBJECTS / (readTimeMs / 1000.0); | |
| double readOpsPerMs = NUM_OBJECTS / readTimeMs; | |
| System.out.printf("Read throughput: %.2f ops/sec, %.2f ops/ms%n", readOpsPerSec, readOpsPerMs); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment