DAO根據時間查看效能

來源:互聯網
上載者:User

利用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;    }    // 靜態Factory 方法    @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());    }}

查看結果得出結論

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.