title: Performance of JBoss Cache and Ehcache
body: <p>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 <em>raw</em> objects into the cache while Ehcache needs to wrap the data in a <code>Element</code> object.</p>
  
  <p>I set up a quick bench inserting repeatedly key, value tuples in the cache. The key and values classes are very simple:</p>
  
  <p>Key:</p>
  
  <pre><code>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 + "]";
      }
  }
  </code></pre>
  
  <p>Value:</p>
  
  <pre><code>public class Value implements Serializable{
  
      /**
       * serialVersionUID
       */
      private static final long serialVersionUID = -499278480347842883L;
  }
  </code></pre>
  
  <p>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 <code>ConcurrentHashMap</code> used 5680 bytes).</p>
  
  <p>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.</p>
  
  <p>Ehcache is initialized programmatically like this:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>JBoss cache is created using Spring with the following bean configuration:</p>
  
  <pre><code>&lt;bean id="cache" class="org.jboss.cache.Cache" factory-bean="cacheFactory" factory-method="createCache"&gt;
      &lt;constructor-arg&gt;
          &lt;value type="java.io.InputStream"&gt;/META-INF/jbossCacheSimpleConf.xml&lt;/value&gt;
      &lt;/constructor-arg&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>and the following <code>jbossCacheConf.xml</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  &lt;/jbosscache&gt;
  </code></pre>
  
  <p>For the sake of completeness the Ehcache test is:</p>
  
  <pre><code>for (int i = 0; i &lt; ITEM_COUNT; i++) {
      this.cache.put(new Element(new Key(i), new Value()));
  }
  </code></pre>
  
  <p>While the JBoss one is:</p>
  
  <pre><code>for (int i = 0; i &lt; ITEM_COUNT; i++) {
      this.processNode.put(new Key(i), new Value());
  }
  </code></pre>
  
  <p>Anything wrong in my setup/benchmark?</p>