1   /***************************************************************************************
2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package test.performance;
9   
10  import junit.framework.TestCase;
11  
12  /***
13   * A so far VERY limited bench. <p/>Only tests the overhead of one around advice and one introduced
14   * method.
15   *
16   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17   * @TODO: extends this test case to be more interesting or replace with a real bench
18   * @TODO: should add some more around advice, since JIT really shines when we have advice chains
19   */
20  public class PerformanceTest extends TestCase {
21      private boolean m_printInfo = true;
22  
23      private int m_numberOfInvocations = 100000000;
24  
25      public PerformanceTest(String name) {
26          super(name);
27      }
28  
29      public void testNonAdvisedMethodPerformance() {
30          long startTime = System.currentTimeMillis();
31          for (int i = 0; i < m_numberOfInvocations; i++) {
32              nonAdvisedMethod();
33          }
34          long time = System.currentTimeMillis() - startTime;
35          double timePerInvocationNormalMethod = time / (double) m_numberOfInvocations;
36          if (m_printInfo) {
37              System.out.println("\nNon advised method: " + timePerInvocationNormalMethod);
38          }
39      }
40  
41      public void testAroundAdvicePerJVMPerformance() {
42          methodAdvisedMethodPerJVM();
43          long startTime = System.currentTimeMillis();
44          for (int i = 0; i < m_numberOfInvocations; i++) {
45              nonAdvisedMethod();
46          }
47          long time = System.currentTimeMillis() - startTime;
48          double timePerInvocationNormalMethod = time / (double) m_numberOfInvocations;
49          startTime = System.currentTimeMillis();
50          for (int i = 0; i < m_numberOfInvocations; i++) {
51              methodAdvisedMethodPerJVM();
52          }
53          time = System.currentTimeMillis() - startTime;
54          double timePerInvocation = time / (double) m_numberOfInvocations;
55          double overhead = timePerInvocation - timePerInvocationNormalMethod;
56          if (m_printInfo) {
57              System.out.println("\nPER_JVM advice: " + overhead);
58          }
59      }
60  
61      public void testAroundAdvicePerClassPerformance() {
62          methodAdvisedMethodPerClass();
63          long startTime = System.currentTimeMillis();
64          for (int i = 0; i < m_numberOfInvocations; i++) {
65              nonAdvisedMethod();
66          }
67          long time = System.currentTimeMillis() - startTime;
68          double timePerInvocationNormalMethod = time / (double) m_numberOfInvocations;
69          startTime = System.currentTimeMillis();
70          for (int i = 0; i < m_numberOfInvocations; i++) {
71              methodAdvisedMethodPerClass();
72          }
73          time = System.currentTimeMillis() - startTime;
74          double timePerInvocation = time / (double) m_numberOfInvocations;
75          double overhead = timePerInvocation - timePerInvocationNormalMethod;
76          if (m_printInfo) {
77              System.out.println("\nPER_CLASS advice: " + overhead);
78          }
79      }
80  
81      public void testAroundAdvicePerInstancePerformance() {
82          methodAdvisedMethodPerInstance();
83          long startTime = System.currentTimeMillis();
84          for (int i = 0; i < m_numberOfInvocations; i++) {
85              nonAdvisedMethod();
86          }
87          long time = System.currentTimeMillis() - startTime;
88          double timePerInvocationNormalMethod = time / (double) m_numberOfInvocations;
89          startTime = System.currentTimeMillis();
90          for (int i = 0; i < m_numberOfInvocations; i++) {
91              methodAdvisedMethodPerInstance();
92          }
93          time = System.currentTimeMillis() - startTime;
94          double timePerInvocation = time / (double) m_numberOfInvocations;
95          double overhead = timePerInvocation - timePerInvocationNormalMethod;
96          if (m_printInfo) {
97              System.out.println("\nPER_INSTANCE advice: " + overhead);
98          }
99      }
100 
101     public void testAroundAdvicePerThreadPerformance() {
102         methodAdvisedMethodPerThread();
103         long startTime = System.currentTimeMillis();
104         for (int i = 0; i < m_numberOfInvocations; i++) {
105             nonAdvisedMethod();
106         }
107         long time = System.currentTimeMillis() - startTime;
108         double timePerInvocationNormalMethod = time / (double) m_numberOfInvocations;
109         startTime = System.currentTimeMillis();
110         for (int i = 0; i < m_numberOfInvocations; i++) {
111             methodAdvisedMethodPerThread();
112         }
113         time = System.currentTimeMillis() - startTime;
114         double timePerInvocation = time / (double) m_numberOfInvocations;
115         double overhead = timePerInvocation - timePerInvocationNormalMethod;
116         if (m_printInfo) {
117             System.out.println("\nPER_THREAD advice: " + overhead);
118         }
119     }
120 
121     public void testIntroductionPerJVMPerformance() {
122         long startTime = System.currentTimeMillis();
123         PerJVM perJVM = (PerJVM) this;
124         for (int i = 0; i < m_numberOfInvocations; i++) {
125             perJVM.runPerJVM();
126         }
127         long time = System.currentTimeMillis() - startTime;
128         double timePerInvocation = time / (double) m_numberOfInvocations;
129         if (m_printInfo) {
130             System.out.println("\nPER_JVM introduction: " + timePerInvocation);
131         }
132     }
133 
134     public void testIntroductionPerClassPerformance() {
135         long startTime = System.currentTimeMillis();
136         PerClass perClass = (PerClass) this;
137         for (int i = 0; i < m_numberOfInvocations; i++) {
138             perClass.runPerClass();
139         }
140         long time = System.currentTimeMillis() - startTime;
141         double timePerInvocation = time / (double) m_numberOfInvocations;
142         if (m_printInfo) {
143             System.out.println("\nPER_CLASS introduction: " + timePerInvocation);
144         }
145     }
146 
147     public void testIntroductionPerInstancePerformance() {
148         long startTime = System.currentTimeMillis();
149         PerInstance perInstance = (PerInstance) this;
150         for (int i = 0; i < m_numberOfInvocations; i++) {
151             perInstance.runPerInstance();
152         }
153         long time = System.currentTimeMillis() - startTime;
154         double timePerInvocation = time / (double) m_numberOfInvocations;
155         if (m_printInfo) {
156             System.out.println("\nPER_INSTANCE introduction: " + timePerInvocation);
157         }
158     }
159 
160     public void testIntroductionPerThreadPerformance() {
161         long startTime = System.currentTimeMillis();
162         PerThread perThread = (PerThread) this;
163         for (int i = 0; i < m_numberOfInvocations; i++) {
164             perThread.runPerThread();
165         }
166         long time = System.currentTimeMillis() - startTime;
167         double timePerInvocation = time / (double) m_numberOfInvocations;
168         if (m_printInfo) {
169             System.out.println("\nPER_THREAD introduction: " + timePerInvocation);
170         }
171     }
172 
173     public static void main(String[] args) {
174         junit.textui.TestRunner.run(suite());
175     }
176 
177     public static junit.framework.Test suite() {
178         return new junit.framework.TestSuite(PerformanceTest.class);
179     }
180 
181     // ==== methods to test ====
182     public void nonAdvisedMethod() {
183     }
184 
185     public void preAdvisedMethodPerJVM() {
186     }
187 
188     public void preAdvisedMethodPerClass() {
189     }
190 
191     public void preAdvisedMethodPerInstance() {
192     }
193 
194     public void preAdvisedMethodPerThread() {
195     }
196 
197     public void methodAdvisedMethodPerJVM() {
198     }
199 
200     public void methodAdvisedMethodPerClass() {
201     }
202 
203     public void methodAdvisedMethodPerInstance() {
204     }
205 
206     public void methodAdvisedMethodPerThread() {
207     }
208 
209     public void methodAdvisedMethodNoAdvice() {
210     }
211 }