Java 使用reflect , 做Method 測試,計算花費時間

摘要:Java 使用reflect , 做Method 測試,計算花費時間

我寫了一個method,專門應付某一個物件,他的method所花費的時間長短,

只針對method沒有任何參數與回傳值

 

以下是我寫的method,方便做測試使用,並將資料print出來


    public static void call_method(TestObject object,String method_name)
    {
        try
        {
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
             Date start_date = new Date();
             long start_time = start_date.getTime();
             Method method = TestObject .class.getMethod(method_name);
             method.invoke(object);
             Date end_date = new Date();
             long end_time = end_date.getTime();
             System.out.println("method_name = "+String.format("%28s",method_name)+ " " + sdf.format(start_date) + "~" + sdf.format(end_date) + " spend time = " + (end_time - start_time));
        }catch (Exception ex)
        {
             ex.printStackTrace();
        }
    }

呼叫時使用


    public static void main(String[] args) throws Exception{
        TestObject object= new TestObject ();
        String[] methods = new String[]{"method1","method2"};

        for (String method:methods)
            call_method(object ,method);
    }

之後我又寫了一個專門測試的Object如下


package test;


import org.apache.log4j.Logger;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;

public class TestObject {
    private static Logger log = Logger.getLogger("Test");
    private HashSet reject_method_map = new HashSet();

    public TestObject()
    {
        reject_method_map.add("wait");
        reject_method_map.add("equals");
        reject_method_map.add("toString");
        reject_method_map.add("hashCode");
        reject_method_map.add("getClass");
        reject_method_map.add("notify");
        reject_method_map.add("notifyAll");
        reject_method_map.add("call_method");
        reject_method_map.add("test_all");
    }

    public void call_method(String method_name)
    {
        try
        {
             if(reject_method_map.contains(method_name))
                 return;

             SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
             Date start_date = new Date();
             long start_time = start_date.getTime();
             Method method = GatewayObject.class.getMethod(method_name);

             if(method.getParameterTypes().length>0)
                 return;

             method.invoke(this);
             Date end_date = new Date();
             long end_time = end_date.getTime();
             log.info("method ="+String.format("%-28s",method_name)+ " " + sdf.format(start_date) + "~" + sdf.format(end_date) + " spend time = " + (end_time - start_time));
        }catch (Exception ex)
        {
             log.info(method_name);
             //ex.printStackTrace();
        }
    }

    public void test_all()
    {
        Method[] methods = this.getClass().getMethods();
        for(Method method:methods)
        {
             call_method(method.getName());
        }
    }
}

只要繼承這個Object之後,

使用物件呼叫test_all

就可以找每一個method所花費的時間