I'm considering to use to implement a cache either JBoss Cache or Ehcache. After looking at both APIs I has the intuition that JBoss is probably a little bit more memory efficient than Ehcache since it can put raw objects into the cache while Ehcache needs to wrap the data in a Element
object.
I set up a quick bench inserting repeatedly key, value tuples in the cache. The key and values classes are very simple:
Key:
public class Key implements Serializable {
private static final long serialVersionUID = -2124973847139523943L;
private final int key;
public Key(int pValue) {
this.key = pValue;
}
public int getValue() {
return this.key;
}
@Override
public String toString() {
return "Key [key=" + this.key + "]";
}
}
Value:
public class Value implements Serializable{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -499278480347842883L;
}
When inserting 100000 objects the result on memory where quite what I expected, Ehcache used 13396 bytes to store the objects while JBoss used 5712 bytes for the same operation (which is good since the same test using a ConcurrentHashMap
used 5680 bytes).
However when I looked at the execution times, I had a very bad surprise: it took Ehcache 300 milliseconds to perform my test while it took 44 seconds for JBossCache to do the same. I'm pretty sure there's something rotten in my JBoss configuration explaining this difference.
Ehcache is initialized programmatically like this:
CacheConfiguration cacheConfiguration = new CacheConfiguration("MyCache", 0).diskPersistent(false).eternal(true)
.diskExpiryThreadIntervalSeconds(100000).transactionalMode(TransactionalMode.OFF);
final Configuration config = new Configuration();
config.setDefaultCacheConfiguration(cacheConfiguration);
this.cacheManager = new CacheManager(config);
cacheConfiguration.name("primaryCache");
this.cache = new net.sf.ehcache.Cache(cacheConfiguration);
this.cacheManager.addCache(this.cache);
JBoss cache is created using Spring with the following bean configuration:
<bean id="cache" class="org.jboss.cache.Cache" factory-bean="cacheFactory" factory-method="createCache">
<constructor-arg>
<value type="java.io.InputStream">/META-INF/jbossCacheSimpleConf.xml</value>
</constructor-arg>
</bean>
and the following jbossCacheConf.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<jbosscache xmlns="urn:jboss:jbosscache-core:config:3.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:jboss:jbosscache-core:config:3.2 http://www.jboss.org/schema/jbosscache/jbosscache-config-3.2.xsd">
</jbosscache>
For the sake of completeness the Ehcache test is:
for (int i = 0; i < ITEM_COUNT; i++) {
this.cache.put(new Element(new Key(i), new Value()));
}
While the JBoss one is:
for (int i = 0; i < ITEM_COUNT; i++) {
this.processNode.put(new Key(i), new Value());
}
Anything wrong in my setup/benchmark?
I switched to infinispan and I don't have any strange performance issues then.