• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package javassist;
2 
3 import junit.framework.*;
4 import javassist.expr.*;
5 import javassist.compiler.*;
6 
7 public class Bench extends JvstTestRoot {
Bench(String name)8     public Bench(String name) {
9         super(name);
10     }
11 
testProceed()12     public void testProceed() throws Exception {
13         CtClass cc = sloader.get("test.BenchProceed");
14         CtMethod m1 = cc.getDeclaredMethod("p");
15         m1.instrument(new ExprEditor() {
16             public void edit(MethodCall m) throws CannotCompileException {
17                 if (m.getMethodName().equals("calc"))
18                     m.replace("{ before($args); $_ = $proceed($$); }");
19             }
20         });
21 
22         CtMethod m2 = cc.getDeclaredMethod("q");
23         m2.instrument(new ExprEditor() {
24             public void edit(MethodCall m) throws CannotCompileException {
25                 if (m.getMethodName().equals("calc"))
26                     m.replace("{ $_ = ($r)replace($args); }");
27             }
28         });
29 
30         CtMethod m3 = cc.getDeclaredMethod("s");
31         m3.instrument(new ExprEditor() {
32             public void edit(MethodCall m) throws CannotCompileException {
33                 if (m.getMethodName().equals("calc2"))
34                     m.replace(
35                         "{ long start = System.currentTimeMillis();"
36                       + "$_ = $proceed($$);"
37                       + "long elapsed = System.currentTimeMillis() - start;"
38                       + "System.out.println(elapsed); }");
39             }
40         });
41 
42         CtMethod m4 = cc.getDeclaredMethod("t");
43         m4.instrument(new ExprEditor() {
44             public void edit(MethodCall m) throws CannotCompileException {
45                 if (m.getMethodName().equals("calc2"))
46                     m.replace(
47                         "{ long start = System.currentTimeMillis();"
48                       + "$_ = $proceed($$);"
49                       + "System.out.println(System.currentTimeMillis() - start);"
50                       + "}");
51             }
52         });
53 
54         cc.writeFile();
55         Object obj = make(cc.getName());
56         int ptime = invoke(obj, "p");
57         int qtime = invoke(obj, "q");
58         System.out.println("time: (p) " + ptime + ", (q) " + qtime);
59         System.out.println("s:");
60         invoke(obj, "s");
61         System.out.println("t:");
62         invoke(obj, "t");
63         assertTrue(ptime < qtime);
64     }
65 
66     public void testProceedNew() throws Exception {
67         CtClass cc = sloader.get("test.BenchProceedNew");
68         CtMethod m1 = cc.getDeclaredMethod("jvst0");
69         m1.instrument(new ExprEditor() {
70             public void edit(NewExpr m) throws CannotCompileException {
71                 m.replace("{ $_ = $proceed($$); }");
72             }
73         });
74 
75         CtMethod m2 = cc.getDeclaredMethod("jvst2");
76         m2.instrument(new ExprEditor() {
77             public void edit(NewExpr m) throws CannotCompileException {
78                 m.replace("{ $_ = $proceed($$); }");
79             }
80         });
81 
82         cc.writeFile();
83         Object obj = make(cc.getName());
84         int qtime = invoke(obj, "jvst0");
85         int ptime = invoke(obj, "org0");
86         System.out.println("time: (org0) " + ptime + ", (jvst0) " + qtime);
87         qtime = invoke(obj, "jvst2");
88         ptime = invoke(obj, "org2");
89         System.out.println("time: (org2) " + ptime + ", (jvst2) " + qtime);
90     }
91 
92     public void testStaticMethod() throws Exception {
93         CtClass cc = sloader.get("test.BenchStaticMethod");
94         CtMethod m1 = cc.getDeclaredMethod("test");
95         m1.instrument(new ExprEditor() {
96             public void edit(MethodCall m) throws CannotCompileException {
97                 if (m.getMethodName().equals("foo"))
98                     m.replace("{ num += $1; $_ = $proceed($$); }");
99             }
100         });
101 
102         cc.writeFile();
103         Object obj = make(cc.getName());
104         int qtime = invoke(obj, "test");
105         int ptime = invoke(obj, "orgTest");
106         System.out.println(
107             "BenchStaticMethod time: (org) " + ptime + ", (jvst) " + qtime);
108     }
109 
110     public void testStaticField() throws Exception {
111         System.out.println(sloader);
112         Javac jc = new Javac(sloader.get("test.StaticField"));
113         long t0 = System.currentTimeMillis();
114         for (int i = 0; i < 100; i++)
115             jc.compileStmnt("{ int counter = 0; counter++; }");
116 
117         t0 = System.currentTimeMillis() - t0;
118         System.out.println("local variable: " + (t0 * 10) + " usec");
119 
120         long t = System.currentTimeMillis();
121         for (int i = 0; i < 100; i++)
122             jc.compileStmnt("{ test.StaticField.counter++; }");
123 
124         t = System.currentTimeMillis() - t;
125         System.out.println("StaticField: " + (t * 10) + " usec");
126 
127         long t2 = System.currentTimeMillis();
128         for (int i = 0; i < 100; i++)
129             jc.compileStmnt("{ test.StaticField#counter++; }");
130 
131         t2 = System.currentTimeMillis() - t2;
132         System.out.println("StaticField with #: " + (t2 * 10) + " usec");
133 
134         long t3 = System.currentTimeMillis();
135         for (int i = 0; i < 100; i++)
136             jc.compileStmnt("{ StaticField.counter2++; }");
137 
138         t3 = System.currentTimeMillis() - t3;
139         System.out.println("StaticField without package: " + (t3 * 10) + " usec");
140 
141         long t4 = System.currentTimeMillis();
142         for (int i = 0; i < 100; i++)
143             jc.compileStmnt("{ test.StaticField.counter++; }");
144 
145         t4 = System.currentTimeMillis() - t4;
146         System.out.println("StaticField: " + (t4 * 10) + " usec");
147 
148         long t5 = System.currentTimeMillis();
149         for (int i = 0; i < 100; i++)
150             jc.compileStmnt("{ System.out.println(); }");
151 
152         t5 = System.currentTimeMillis() - t5;
153         System.out.println("println: " + (t5 * 10) + " usec");
154     }
155 
156     public static Test suite() {
157         TestSuite suite = new TestSuite("Benchmark Tests");
158         suite.addTestSuite(Bench.class);
159         suite.addTestSuite(testproxy.ProxyFactoryPerformanceTest.class);
160         return suite;
161     }
162 }
163