JBoss Cache和Ehcache的性能
我正在考虑使用JBoss Cache或Ehcache来实现缓存。 在查看了这两个API后,我直觉认为JBoss可能比Ehcache更高效,因为它可以将原始对象放入缓存,而Ehcache需要将数据包装在Element
对象中。
我设置了一个快速的工作台,在缓存中重复插入密钥,值元组。 键和值类非常简单:
键:
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 + "]"; } }
值:
public class Value implements Serializable{ /** * serialVersionUID */ private static final long serialVersionUID = -499278480347842883L; }
当在内存中插入100000个对象的结果时,Ehcache使用13396个字节来存储对象,而JBoss使用5712个字节进行相同的操作(这是好的,因为使用ConcurrentHashMap
的同一测试使用了5680个字节)。
然而,当我查看执行时间时,我有一个非常糟糕的惊喜:Ehcache花了300毫秒来执行我的测试,而JBossCache花了44秒才做同样的事情。 我很确定我的JBoss配置中有一些东西在解释这种差异。
Ehcache以编程方式初始化如下:
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缓存是使用Spring创建的,具有以下bean配置:
/META-INF/jbossCacheSimpleConf.xml
以及以下jbossCacheConf.xml
文件:
为了完整起见,Ehcache测试是:
for (int i = 0; i < ITEM_COUNT; i++) { this.cache.put(new Element(new Key(i), new Value())); }
而JBoss是:
for (int i = 0; i < ITEM_COUNT; i++) { this.processNode.put(new Key(i), new Value()); }
我的设置/基准测试有什么问题吗?
我切换到infinispan,然后我没有任何奇怪的性能问题。
注意默认的JBossCache配置。 默认情况下,JBossCache可能会尝试在从属节点上查找和复制数据。