我们如何测试 Apache 公共池逐出功能

How we can test the Apache Common pool evict functionality

我正在尝试使用 Apache 公共池库来为在我的应用程序中创建昂贵的对象实施对象池。对于资源池,我使用库的 GenericObjectPool class 来使用 API 为对象池提供的默认实现。为了确保我们最终不会在内存中有多个空闲对象,我将 minEvictableIdleTimeMillis 和 timeBetweenEvictionRunsMillis 属性设置为 30 分钟。

正如我从其他问题、博客和 API 文档中了解到的那样,这些属性会触发一个单独的线程,以便从池中驱逐空闲对象。

如果这对应用程序性能有任何不利影响,以及是否有任何方法可以测试该线程是否实际执行,有人可以帮助我吗?

  1. 启用驱逐器时库附带性能免责声明

Eviction runs contend with client threads for access to objects in the pool, so if they run too frequently performance issues may result.

参考:https://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html

但是,我们有一个高 TPS 系统 运行 每 1 秒驱逐一次,我们没有看到太多的性能瓶颈。

  1. 至于逐出线程运行,您可以在 GenericObjectPool 的实现中覆盖 evict() 方法并添加日志行。

    @Override
    public void evict() throws Exception {
       //log
       super.evict();
    }