Last active
August 29, 2015 14:25
-
-
Save agrison/d4ea24b3687084bc85f9 to your computer and use it in GitHub Desktop.
Simple spring boot app that inserts 1M random key into redis using one connection and pipelining
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 me.grison.redis.foo; | |
import org.springframework.boot.*; | |
import org.springframework.boot.autoconfigure.*; | |
import org.springframework.context.annotation.*; | |
import redis.clients.jedis.*; | |
import java.util.UUID; | |
@ComponentScan | |
@Configuration | |
@SpringBootApplication | |
@EnableAutoConfiguration | |
public class RedisMassImport { | |
@Bean | |
JedisPool jedisPool() { | |
JedisPoolConfig poolConfig = new JedisPoolConfig(); | |
poolConfig.setTestOnBorrow(true); | |
poolConfig.setTestWhileIdle(true); | |
return new JedisPool(poolConfig, "localhost", 6379, 30); | |
} | |
@Bean | |
CommandLineRunner init(JedisPool pool) { | |
return args -> { | |
int numKeys = 1_000_000; | |
try (Jedis jedis = pool.getResource()) { | |
// prepare UUIDs | |
String[] uuids = new String[numKeys]; | |
for (int i = 0; i < numKeys; ++i) | |
uuids[i] = UUID.randomUUID().toString(); | |
long time = System.nanoTime(); | |
Pipeline p = jedis.pipelined(); | |
for (int i = 0; i < numKeys; ++i) | |
p.set(uuids[i], uuids[i]); | |
p.sync(); | |
long endTime = System.nanoTime(); | |
System.out.println("\tTook " + (endTime - time) + "ns to sync the pipeline."); | |
} | |
}; | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(RedisMassImport.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment