Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7584246 建立时间:2006年5月29日 |

| |
[Java Open Source]oscache使用和研究 软件技术
lhwork 发表于 2006/7/13 10:18:16 |
Oscache的使用非常方便,特别是jsp cache用的非常广泛。Oscache的文档中也对jsp cache tag的配置有详细说明,但对如普通pojo对象的cache讲的较少,也许是比较简单的缘故。今天做了个测试方案,写测试案例进行了比较和研究。
测试方案1:在本机上直接测试,循环从metabase库中的process_info表取得数据(表中只有2条记录)比较使用cache和不使用cache的性能(为平均值)。
代码如下:
1500)this.width=500'>500)this.width=500'>public class DatabaseCacheTest extends TestCase 500)this.width=500'>{ 2500)this.width=500'> GeneralCacheAdministrator admin = null; 3500)this.width=500'> 4500)this.width=500'> protected ApplicationContext ctx; 5500)this.width=500'> 6500)this.width=500'> protected ProcessInfoDAO processInfoDAO; 7500)this.width=500'> 8500)this.width=500'>500)this.width=500'> protected void setUp() throws Exception 500)this.width=500'>{ 9500)this.width=500'>500)this.width=500'> String[] paths = 500)this.width=500'>{ "/spring/dataAccessContext.xml",10500)this.width=500'> "/spring/spring-biz-db.xml" };11500)this.width=500'> ctx = new ClassPathXmlApplicationContext(paths);12500)this.width=500'>13500)this.width=500'> processInfoDAO = (ProcessInfoDAO) ctx.getBean("processInfoDAO");14500)this.width=500'> admin = new GeneralCacheAdministrator();15500)this.width=500'> }16500)this.width=500'>17500)this.width=500'>500)this.width=500'> protected void tearDown() throws Exception 500)this.width=500'>{18500)this.width=500'> admin.destroy();19500)this.width=500'> }20500)this.width=500'>21500)this.width=500'>500)this.width=500'> public void testGetFromCache() 500)this.width=500'>{22500)this.width=500'> long t1 = System.currentTimeMillis();23500)this.width=500'>24500)this.width=500'>500)this.width=500'> for (int i = 0; i < 10000; i++) 500)this.width=500'>{25500)this.width=500'> ProcessInfoDO pdo = getProcess("65");26500)this.width=500'> assertEquals(pdo.getProcessName(), "TestProcess");27500)this.width=500'> }28500)this.width=500'> System.out.println(System.currentTimeMillis() - t1);29500)this.width=500'>30500)this.width=500'> }31500)this.width=500'>32500)this.width=500'>500)this.width=500'> private ProcessInfoDO getProcessByCache(String id) 500)this.width=500'>{33500)this.width=500'> ProcessInfoDO pdo;34500)this.width=500'>500)this.width=500'> try 500)this.width=500'>{35500)this.width=500'> pdo = (ProcessInfoDO) admin.getFromCache("65");36500)this.width=500'> return pdo;37500)this.width=500'>500)this.width=500'> } catch (NeedsRefreshException e) 500)this.width=500'>{38500)this.width=500'> pdo = processInfoDAO.selectById(65);39500)this.width=500'> admin.putInCache("65", pdo);40500)this.width=500'> return pdo;41500)this.width=500'> }42500)this.width=500'> }43500)this.width=500'>44500)this.width=500'>500)this.width=500'> private ProcessInfoDO getProcess(String id) 500)this.width=500'>{45500)this.width=500'> return processInfoDAO.selectById(65);46500)this.width=500'> }47500)this.width=500'>}48500)this.width=500'>
Ø 循环100次,使用cache用时578ms,直接从数据库取用时2015ms。
Ø 循环1000次,使用cache用时719ms,直接从数据库取用时13984ms。
Ø 循环10000次,使用cache用时2016ms,直接从数据库取用时131188ms。
使用图例比较,系列1表示循环的次数,系列2为使用cache的用时,系列3为不使用cache的用时。可以看出,随着循环次数的增多,使用cache方案的性能优势更加明显。500)this.width=500'>
结论:使用cache,随着循环的增多,用时增长较缓慢,而不使用cache基本是等比例增长。在循环次数较多时,使用cache cpu利用率显著提高,能达到90%以上。不使用cache则只能上到50%左右,更多是在等待数据库返回结果。所以使用cache能大大减轻数据库的压力,提高应用服务器的利用率,符合我们对应用服务器进行水平扩展的要求。 |
|
|