利用Proxy来测试DAO中方法的执行时间
提供一个记录开始时间和结束时间的工具类
建一代理类
在test类中使用
查看结果得出结论
提供一个记录开始时间和结束时间的工具类
/**
* 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));
}
}
* 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;
}
}
* 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());
}
}
* 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());
}
}
查看结果得出结论