Hi,
I have written a standalone program PublishBenchmark.java (please see below)
using Jedis to publish a 2K message to a Redis Channel 100,000 times.
I repeat the same process using Camel 2.11.1 Camel spring redis component
(Please see RedisPublisherBenchmark.java below).
Below is timing for both.
Jedis : Time taken (in sec) to publish 100000 msgs=1.805
Camel spring redis : Time taken (in sec) to publish 100000 msgs=16.017
Is my configuration for Camel spring redis optimal ?
Is it expected that Camel spring redis about 8 times slower than a standalone
Jedis program ?
Thanks in advance for any assistance !
Shing
public class RedisPublisherBenchmark {
public static void main(String[] args) throws Exception {
JedisShardInfo hostInfo = new JedisShardInfo("localhost", 6379);
hostInfo.setPassword("abc123");
final JedisConnectionFactory CONNECTION_FACTORY = new
JedisConnectionFactory(
hostInfo);
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String,
String>();
redisTemplate.setConnectionFactory(CONNECTION_FACTORY);
// redisTemplate.afterPropertiesSet();
SimpleRegistry registry = new SimpleRegistry();
registry.put("redisTemplate", redisTemplate);
// To get rid of Hex padding
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
// create CamelContext
CamelContext context = new DefaultCamelContext(registry);
Component direct = new DirectComponent();
context.addComponent("start", direct);
final String directEndPoint = "direct:start";
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("spring-redis://localhost:6379?redisTemplate=#redisTemplate");
}
});
context.start();
ProducerTemplate template = context.createProducerTemplate();
final String payload = getPayload();
int NO_OF_MSGS= 100000;
final Processor processor = new RedisProcessor(payload);
long start = System.currentTimeMillis();
for (int i = 0; i <NO_OF_MSGS ; ++i) {
// Publish "Hello world" to a redis channel
Exchange exchange = template.send(directEndPoint, processor);
}
long end = System.currentTimeMillis();
double timeTaken = (end - start)/1000.0;
System.out.println(String.format("Time taken (in sec) to publish %s
msgs=%s", NO_OF_MSGS,timeTaken));
System.out.println("Press any key to shutdown.");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
br.readLine();
// stop the CamelContext
context.stop();
}
private static String getPayload() {
String template = "name11:foo11";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 170; ++i) {
sb.append(template);
}
return sb.toString();
}
private static class RedisProcessor implements Processor {
public String message;
public RedisProcessor(String message) {
this.message = message;
}
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
in.setHeader(RedisConstants.COMMAND, "PUBLISH");
in.setHeader(RedisConstants.CHANNEL, "msjscollector");
in.setHeader(RedisConstants.MESSAGE, message);
}
}
}
============================
public class PublishBenchmark {
private static final int NO_OF_MSGS = 100000;
public static void main(String[] args) throws UnknownHostException,
IOException {
Jedis jedis = new Jedis("localhost", 6379);
jedis.connect();
jedis.auth("abc123");
jedis.flushAll();
long begin = System.currentTimeMillis();
String payload = getPayload();
for (int n = 0; n <= NO_OF_MSGS; ++n) {
jedis.publish("msjscollector", payload);
}
double timeTaken = (System.currentTimeMillis() - begin)/1000.0;
jedis.disconnect();
System.out.println(String.format("Time taken (in sec) to publish %s
msgs=%s", NO_OF_MSGS,timeTaken));
}
private static String getPayload(){
String template ="name11:foo11";
StringBuilder sb = new StringBuilder();
for (int i=0; i < 170; ++i){
sb.append(template);
}
return sb.toString();
}
}