001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.performance;
008:
009: import junit.framework.TestCase;
010:
011: /**
012: * A so far VERY limited bench. <p/>Only tests the overhead of one around advice and one introduced
013: * method.
014: *
015: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
016: * @TODO: extends this test case to be more interesting or replace with a real bench
017: * @TODO: should add some more around advice, since JIT really shines when we have advice chains
018: */
019: public class PerformanceTest extends TestCase {
020: private boolean m_printInfo = true;
021:
022: private int m_numberOfInvocations = 100000000;
023:
024: public PerformanceTest(String name) {
025: super (name);
026: }
027:
028: public void testNonAdvisedMethodPerformance() {
029: long startTime = System.currentTimeMillis();
030: for (int i = 0; i < m_numberOfInvocations; i++) {
031: nonAdvisedMethod();
032: }
033: long time = System.currentTimeMillis() - startTime;
034: double timePerInvocationNormalMethod = time
035: / (double) m_numberOfInvocations;
036: if (m_printInfo) {
037: System.out.println("\nNon advised method: "
038: + timePerInvocationNormalMethod);
039: }
040: }
041:
042: public void testAroundAdvicePerJVMPerformance() {
043: methodAdvisedMethodPerJVM();
044: long startTime = System.currentTimeMillis();
045: for (int i = 0; i < m_numberOfInvocations; i++) {
046: nonAdvisedMethod();
047: }
048: long time = System.currentTimeMillis() - startTime;
049: double timePerInvocationNormalMethod = time
050: / (double) m_numberOfInvocations;
051: startTime = System.currentTimeMillis();
052: for (int i = 0; i < m_numberOfInvocations; i++) {
053: methodAdvisedMethodPerJVM();
054: }
055: time = System.currentTimeMillis() - startTime;
056: double timePerInvocation = time
057: / (double) m_numberOfInvocations;
058: double overhead = timePerInvocation
059: - timePerInvocationNormalMethod;
060: if (m_printInfo) {
061: System.out.println("\nPER_JVM advice: " + overhead);
062: }
063: }
064:
065: public void testAroundAdvicePerClassPerformance() {
066: methodAdvisedMethodPerClass();
067: long startTime = System.currentTimeMillis();
068: for (int i = 0; i < m_numberOfInvocations; i++) {
069: nonAdvisedMethod();
070: }
071: long time = System.currentTimeMillis() - startTime;
072: double timePerInvocationNormalMethod = time
073: / (double) m_numberOfInvocations;
074: startTime = System.currentTimeMillis();
075: for (int i = 0; i < m_numberOfInvocations; i++) {
076: methodAdvisedMethodPerClass();
077: }
078: time = System.currentTimeMillis() - startTime;
079: double timePerInvocation = time
080: / (double) m_numberOfInvocations;
081: double overhead = timePerInvocation
082: - timePerInvocationNormalMethod;
083: if (m_printInfo) {
084: System.out.println("\nPER_CLASS advice: " + overhead);
085: }
086: }
087:
088: public void testAroundAdvicePerInstancePerformance() {
089: methodAdvisedMethodPerInstance();
090: long startTime = System.currentTimeMillis();
091: for (int i = 0; i < m_numberOfInvocations; i++) {
092: nonAdvisedMethod();
093: }
094: long time = System.currentTimeMillis() - startTime;
095: double timePerInvocationNormalMethod = time
096: / (double) m_numberOfInvocations;
097: startTime = System.currentTimeMillis();
098: for (int i = 0; i < m_numberOfInvocations; i++) {
099: methodAdvisedMethodPerInstance();
100: }
101: time = System.currentTimeMillis() - startTime;
102: double timePerInvocation = time
103: / (double) m_numberOfInvocations;
104: double overhead = timePerInvocation
105: - timePerInvocationNormalMethod;
106: if (m_printInfo) {
107: System.out.println("\nPER_INSTANCE advice: " + overhead);
108: }
109: }
110:
111: public void testAroundAdvicePerThreadPerformance() {
112: methodAdvisedMethodPerThread();
113: long startTime = System.currentTimeMillis();
114: for (int i = 0; i < m_numberOfInvocations; i++) {
115: nonAdvisedMethod();
116: }
117: long time = System.currentTimeMillis() - startTime;
118: double timePerInvocationNormalMethod = time
119: / (double) m_numberOfInvocations;
120: startTime = System.currentTimeMillis();
121: for (int i = 0; i < m_numberOfInvocations; i++) {
122: methodAdvisedMethodPerThread();
123: }
124: time = System.currentTimeMillis() - startTime;
125: double timePerInvocation = time
126: / (double) m_numberOfInvocations;
127: double overhead = timePerInvocation
128: - timePerInvocationNormalMethod;
129: if (m_printInfo) {
130: System.out.println("\nPER_THREAD advice: " + overhead);
131: }
132: }
133:
134: public void testIntroductionPerJVMPerformance() {
135: long startTime = System.currentTimeMillis();
136: PerJVM perJVM = (PerJVM) this ;
137: for (int i = 0; i < m_numberOfInvocations; i++) {
138: perJVM.runPerJVM();
139: }
140: long time = System.currentTimeMillis() - startTime;
141: double timePerInvocation = time
142: / (double) m_numberOfInvocations;
143: if (m_printInfo) {
144: System.out.println("\nPER_JVM introduction: "
145: + timePerInvocation);
146: }
147: }
148:
149: public void testIntroductionPerClassPerformance() {
150: long startTime = System.currentTimeMillis();
151: PerClass perClass = (PerClass) this ;
152: for (int i = 0; i < m_numberOfInvocations; i++) {
153: perClass.runPerClass();
154: }
155: long time = System.currentTimeMillis() - startTime;
156: double timePerInvocation = time
157: / (double) m_numberOfInvocations;
158: if (m_printInfo) {
159: System.out.println("\nPER_CLASS introduction: "
160: + timePerInvocation);
161: }
162: }
163:
164: public void testIntroductionPerInstancePerformance() {
165: long startTime = System.currentTimeMillis();
166: PerInstance perInstance = (PerInstance) this ;
167: for (int i = 0; i < m_numberOfInvocations; i++) {
168: perInstance.runPerInstance();
169: }
170: long time = System.currentTimeMillis() - startTime;
171: double timePerInvocation = time
172: / (double) m_numberOfInvocations;
173: if (m_printInfo) {
174: System.out.println("\nPER_INSTANCE introduction: "
175: + timePerInvocation);
176: }
177: }
178:
179: public void testIntroductionPerThreadPerformance() {
180: long startTime = System.currentTimeMillis();
181: PerThread perThread = (PerThread) this ;
182: for (int i = 0; i < m_numberOfInvocations; i++) {
183: perThread.runPerThread();
184: }
185: long time = System.currentTimeMillis() - startTime;
186: double timePerInvocation = time
187: / (double) m_numberOfInvocations;
188: if (m_printInfo) {
189: System.out.println("\nPER_THREAD introduction: "
190: + timePerInvocation);
191: }
192: }
193:
194: public static void main(String[] args) {
195: junit.textui.TestRunner.run(suite());
196: }
197:
198: public static junit.framework.Test suite() {
199: return new junit.framework.TestSuite(PerformanceTest.class);
200: }
201:
202: // ==== methods to test ====
203: public void nonAdvisedMethod() {
204: }
205:
206: public void preAdvisedMethodPerJVM() {
207: }
208:
209: public void preAdvisedMethodPerClass() {
210: }
211:
212: public void preAdvisedMethodPerInstance() {
213: }
214:
215: public void preAdvisedMethodPerThread() {
216: }
217:
218: public void methodAdvisedMethodPerJVM() {
219: }
220:
221: public void methodAdvisedMethodPerClass() {
222: }
223:
224: public void methodAdvisedMethodPerInstance() {
225: }
226:
227: public void methodAdvisedMethodPerThread() {
228: }
229:
230: public void methodAdvisedMethodNoAdvice() {
231: }
232: }
|