Package measurements.suites

Source Code of measurements.suites.JoinPointCostsMeasurement$TestHook

//$Id: JoinPointCostsMeasurement.java,v 1.3 2008/11/18 10:41:01 anicoara Exp $
//=====================================================================

package measurements.suites;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import junit.framework.Test;
import ch.ethz.inf.util.junit.PerformanceTest;
import ch.ethz.inf.util.junit.PerformanceTestSuite;
import ch.ethz.jvmai.*;

/**
* Measure the join point costs.
*
* @version $Revision: 1.3 $
* @author   Angela Nicoara
* @author   Johann Gyger
*/
public class JoinPointCostsMeasurement extends PerformanceTest {

  public static final boolean CHECK_ASSERT = false;

  public static final boolean USE_PROSE = false;

  public static int fieldAccessCount;
  public static int fieldModificationCount;
  public static int methodEntryCount;
  public static int methodExitCount;
  public static int exceptionThrowCount;
  public static int exceptionCatchCount;

  public static Test suite() {
    return new PerformanceTestSuite(JoinPointCostsMeasurement.class);
  }

  protected TestHook hook;

  protected JVMAspectInterface aspectInterface;

  protected MyTestClass testObject = new MyTestClass();

  protected Method methodEntry;

  protected Method methodExit;

  protected Field field;

  public JoinPointCostsMeasurement(String name) {
    super(name);
    RANGE = new int[] { 10000000 };
  }

  protected void setUp() throws Exception {
    if (USE_PROSE) {
      String providerName = System.getProperty("ch.ethz.prose.JVMAIProvider");
      Class providerClass = Class.forName(providerName);
      Provider provider = (Provider) providerClass.newInstance();

      aspectInterface = provider.getAspectInterface();
      aspectInterface.startup(null, true);

      hook = new TestHook();
      aspectInterface.setJoinPointHook(hook);

      methodEntry = MyTestClass.class.getDeclaredMethod("myMethodEntry", new Class[] {});
      methodExit = MyTestClass.class.getDeclaredMethod("myMethodExit", new Class[] {});
      field = MyTestClass.class.getDeclaredField("myField");
    }

    fieldAccessCount = 0;
    fieldModificationCount = 0;
    methodEntryCount = 0;
    methodExitCount = 0;
    exceptionThrowCount = 0;
    exceptionCatchCount = 0;
  }

  protected void tearDown() {
    if (USE_PROSE)
      aspectInterface.teardown();
  }

  public void testMethod0() {
    testObject.myMethod(RUNS);
  }

  public void testMethodEntry() {
    if (USE_PROSE) {
      aspectInterface.setMethodEntryWatch(methodEntry, new Object());
      aspectInterface.resumeNotification(Thread.currentThread());
    }

    testObject.myMethodEntry(RUNS);

    if (CHECK_ASSERT)
      assertEquals("Hook notifications", RUNS, methodEntryCount);
  }

  public void testMethodExit() {
    if (USE_PROSE) {
      aspectInterface.setMethodExitWatch(methodExit, new Object());
      aspectInterface.resumeNotification(Thread.currentThread());
    }

    testObject.myMethodExit(RUNS);

    if (CHECK_ASSERT)
      assertEquals("Hook notifications", RUNS, methodExitCount);
  }

  public void testFieldAccess0() {
    testObject.myFieldAccess(RUNS);
  }

  public void testFieldAccess() {
    if (USE_PROSE) {
      aspectInterface.setFieldAccessWatch(field, new Object());
      aspectInterface.resumeNotification(Thread.currentThread());
    }

    testObject.myFieldAccess(RUNS);

    if (CHECK_ASSERT)
      assertEquals("Hook notifications", RUNS, fieldAccessCount);
  }

  public void testFieldModification0() {
    testObject.myFieldModification(RUNS);
  }

  public void testFieldModification() {
    if (USE_PROSE) {
      aspectInterface.setFieldModificationWatch(field, new Object());
      aspectInterface.resumeNotification(Thread.currentThread());
    }

    testObject.myFieldModification(RUNS);

    if (CHECK_ASSERT)
      assertEquals("Hook notifications", RUNS, fieldModificationCount);
  }

  public void testException0() {
    RUNS = 1000000;
    testObject.myException(RUNS);
  }

  public void testExceptionThrow() {
    if (USE_PROSE) {
      aspectInterface.setExceptionThrowWatch(MyException.class, new Object());
      aspectInterface.resumeNotification(Thread.currentThread());
    }

    RUNS = 1000000;
    testObject.myException(RUNS);

    if (CHECK_ASSERT && USE_PROSE)
      assertEquals("Hook notifications", RUNS, exceptionThrowCount);
  }

  public void testExceptionCatch() {
    if (USE_PROSE) {
      aspectInterface.setExceptionCatchWatch(MyException.class, new Object());
      aspectInterface.resumeNotification(Thread.currentThread());
    }

    RUNS = 1000000;
    testObject.myException(RUNS);

    if (CHECK_ASSERT)
      assertEquals("Hook notifications", RUNS, exceptionCatchCount);
  }

  public static class MyTestClass {

    public static MyException myException = new MyException();

    public int myField = 42;

    public void myMethod() {}

    public void myMethodEntry() {}

    public void myMethodExit() {}

    public void myMethod(int runs) {
      startChronometer();
      for (int i = 0; i < runs; i++)
        myMethod();
      stopChronometer();
    }

    public void myMethodEntry(int runs) {
      startChronometer();
      for (int i = 0; i < runs; i++)
        myMethodEntry();
      stopChronometer();
    }

    public void myMethodExit(int runs) {
      startChronometer();
      for (int i = 0; i < runs; i++)
        myMethodExit();
      stopChronometer();
    }

    public void myFieldAccess(int runs) {
      int n = 0;

      startChronometer();
      for (int i = 0; i < runs; i++)
        n = myField;
      stopChronometer();
    }

    public void myFieldModification(int runs) {
      startChronometer();
      for (int i = 0; i < runs; i++)
        myField = i;
      stopChronometer();
    }

    public void myExceptionThrow() throws MyException {
      throw myException;
    }

    public void myExceptionCatch() {
      try {
        myExceptionThrow();
      } catch (MyException e) {}
    }

    public void myException(int runs) {
      MyException e = new MyException();

      startChronometer();
      for (int i = 0; i < runs; i++)
        myExceptionCatch();
      stopChronometer();
    }
  }

  public static class MyException extends Exception {};

  public static class TestHook extends JoinPointHook {

    public void onFieldAccess(FieldAccessJoinPoint joinPoint) {
      fieldAccessCount++;
    }

    public void onFieldModification(FieldModificationJoinPoint joinPoint) {
      fieldModificationCount++;
    }

    public void onMethodEntry(MethodEntryJoinPoint joinPoint) {
      methodEntryCount++;
    }

    public void onMethodExit(MethodExitJoinPoint joinPoint) {
      methodExitCount++;
    }

    public void onExceptionThrow(ExceptionJoinPoint joinPoint) {
      exceptionThrowCount++;
    }

    public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) {
      exceptionCatchCount++;
    }

    public void onClassLoad(Class cls) {}

    public void onConstructor(ConstructorJoinPoint joinpoint) {}

  }

}
TOP

Related Classes of measurements.suites.JoinPointCostsMeasurement$TestHook

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.