DAOは時間に応じてパフォーマンスを表示

3879 ワード

Proxyを使用してDAOのメソッドの実行時間をテストし、開始時間と終了時間を記録するツールクラスを提供します.
/**

 * TimeTestUtil.java

 *

 * Copyright 2008. All Rights Reserved.

 */

package com.easou.yybar.utils;



/** *//**

 * TODO util class TimeTestUtil

 * 

 * Revision History

 *

 * 2008-7-4,Cosmo,created it

 */

public class TimeTestUtil {



    // : 

    private static Long timeStart;

    

    // : 

    private static Long timeEnd;

    

    // 

    public static void getBeginTime() { 

        timeStart = System.currentTimeMillis();

        System.out.println(" :" + timeStart);

    }

    

    // 

    public static void getEndTime() { 

        timeEnd = System.currentTimeMillis();

        System.out.println(" :" + timeEnd);

    }

    

    // 

    public static void getTimeDispersion() { 

        System.out.println(" :" + (timeEnd - timeStart));

    }

}


エージェントクラスを作成
/**

 * TimeTestProxy.java

 *

 * Copyright 2008. All Rights Reserved.

 */

package com.easou.yybar;



import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

import java.util.List;



import com.easou.yybar.model.dao.ICatalogsDAO;

import com.easou.yybar.model.dao.impl.CatalogsDAO;

import com.easou.yybar.model.entity.Catalogs;

import com.easou.yybar.utils.TimeTestUtil;



/** *//**

 * TODO proxy class TimeTestProxy

 * 

 * Revision History

 * 

 * 2008-7-4,Cosmo,created it

 */

public class TimeTestProxy implements InvocationHandler {

    //  

    private Object proxyObj;



    public TimeTestProxy(Object obj) {

        this.proxyObj = obj;

    }



    //  

    @SuppressWarnings("unchecked")

    public static Object factory(Object obj) {

        //  

        Class cls = obj.getClass();

        //  invoke 

        return Proxy.newProxyInstance(cls.getClassLoader(),

                cls.getInterfaces(), new TimeTestProxy(obj));

    }



    public Object invoke(Object proxy, Method method, Object[] args)

            throws Throwable {

        //  , 

        TimeTestUtil.getBeginTime();

        //  

        Object o = method.invoke(proxyObj, args);

        //  , 

        TimeTestUtil.getEndTime();

        //  , 

        TimeTestUtil.getTimeDispersion();

        return o;

    }



}


testクラスでの使用
/**

 * CatalogsDAOTest.java

 *

 * Copyright 2008. All Rights Reserved.

 */

package com.easou.yybar.catalogs;



import com.easou.yybar.BaseTestCase;

import com.easou.yybar.TimeTestProxy;

import com.easou.yybar.model.dao.ICatalogsDAO;



/** *//**

 * TODO test CatalogsDAOTest.class

 * 

 * Revision History

 * 

 * 2008-6-17,Cosmo,created it

 */

public class CatalogsDAOTest extends BaseTestCase {



    ICatalogsDAO catalogsDao = null;



    @Override

    protected void onSetUp() throws Exception {

        catalogsDao = (ICatalogsDAO) this.getApplicationContext().getBean(

                "catalogsDAO");

        super.onSetUp();

    }



    @Override

    protected void onTearDown() throws Exception {

        catalogsDao = null;

        super.onTearDown();

    }



    @SuppressWarnings("unchecked")

    public void testCatalogsDAO() {

        catalogsDao = (ICatalogsDAO)TimeTestProxy.factory(catalogsDao);

        final int TYPE_F = 1;

        final int TYPE_Y = 2;

        int page = 1;

        int rowsPerPage = 4;

        assertEquals(rowsPerPage, catalogsDao.findCatalogsByType(TYPE_F, page,

                rowsPerPage).size());

        assertEquals(rowsPerPage, catalogsDao.findCatalogsByType(TYPE_Y, page,

                rowsPerPage).size());

        assertEquals(8, catalogsDao.findCatalogsByType(TYPE_F, 0, -1).size());

        assertEquals(8, catalogsDao.findCatalogsByType(TYPE_Y, 1, -1).size());

    }

}


結果を見て結論を出す