PerformanceProxy

Posted by Bruce Tsai

說明

透過 AOP 記錄呼叫方法時,執行花費的時間。需在 spring 設定中設定 AOP 相關的設定,配合 PerformanceAdvice 使用。

範例

  • spring 設定檔
<aop:config proxy-target-class="true">

    <aop:pointcut
      id="performanceProxy"
      expression="execution(* com.fet.crm.eservice..*.*(..))
      and (@within(foundation.core.generic.annotation.aop.PerformanceProxy)
      or @annotation(foundation.core.generic.annotation.aop.PerformanceProxy))" />

    <aop:advisor
      advice-ref="performanceAdvice" 
      pointcut-ref="performanceProxy" />

</aop:config>

<bean id="performanceAdvice" 
      class="foundation.core.generic.aop.PerformanceAdvice">
    <property name="enabled" value="true" />
</bean>
  • 程式用例
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Executor {

    @PerformanceProxy
    public void execute() {
        try {
            Thread.sleep((long) (Math.random() * 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

public class Program

    public static void main(String[] args) {
        Context context = Contexts.Load();
        Executor executor = context.getBean(Executor.class);
        executor.execute();
    }
}

輸出結果

開始進行執行效能計算(public void Executor.execute())
執行Executor.execute() 共花費272.2 ms

results matching ""

    No results matching ""