JBoss Cache和Ehcache的性能

发布于 2024-11-15 12:53:06 字数 2682 浏览 1 评论 0原文

我正在考虑使用 JBoss Cache 或 Ehcache 来实现缓存。在查看了这两个 API 后,我的直觉是 JBoss 可能比 Ehcache 的内存效率更高一些,因为它可以将原始对象放入缓存中,而 Ehcache 需要将数据包装在 Element 中对象。

我设置了一个快速工作台,在缓存中重复插入键、值元组。键和值类非常简单:

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;
}

当插入 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 配置:

<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>

和以下 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>

为了完整起见,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());
}

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路弥 2024-11-22 12:53:06

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

I switched to infinispan and I don't have any strange performance issues then.

羁拥 2024-11-22 12:53:06

请注意默认的 JBossCache 配置。
默认情况下,JBossCache 可能会尝试在从属节点上查找并复制数据。

Be careful about the default JBossCache configuration.
It could be possible that by default JBossCache try to find and replicate data on a slave node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文