JBoss Cache和Ehcache的性能

gab*_*uzo 9 java performance ehcache jboss-cache

我正在考虑使用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 + "]";
    }
}
Run Code Online (Sandbox Code Playgroud)

值:

public class Value implements Serializable{

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -499278480347842883L;
}
Run Code Online (Sandbox Code Playgroud)

当将100000个对象的结果插入到内存中时,Ehcache使用13396个字节来存储对象,而JBoss使用5712个字节进行相同的操作(由于使用了ConcurrentHashMap5680字节的相同测试,因此很好).

然而,当我查看执行时间时,我有一个非常糟糕的惊喜: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);
Run Code Online (Sandbox Code Playgroud)

JBoss缓存是使用Spring创建的,具有以下bean配置:

<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>
Run Code Online (Sandbox Code Playgroud)

和以下jbossCacheConf.xml文件:

<?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>
Run Code Online (Sandbox Code Playgroud)

为了完整起见,Ehcache测试是:

for (int i = 0; i < ITEM_COUNT; i++) {
    this.cache.put(new Element(new Key(i), new Value()));
}
Run Code Online (Sandbox Code Playgroud)

而JBoss是:

for (int i = 0; i < ITEM_COUNT; i++) {
    this.processNode.put(new Key(i), new Value());
}
Run Code Online (Sandbox Code Playgroud)

我的设置/基准测试有什么问题吗?

gab*_*uzo 4

我切换到infinispan,然后就没有任何奇怪的性能问题了。