aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/javassist/Bench.java
blob: b7b8b27e0fb1133ac32d5402d5443182e48226e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package javassist;

import junit.framework.*;
import javassist.expr.*;
import javassist.compiler.*;

public class Bench extends JvstTestRoot {
    public Bench(String name) {
        super(name);
    }

    public void testProceed() throws Exception {
        CtClass cc = sloader.get("test.BenchProceed");
        CtMethod m1 = cc.getDeclaredMethod("p");
        m1.instrument(new ExprEditor() {
            public void edit(MethodCall m) throws CannotCompileException {
                if (m.getMethodName().equals("calc"))
                    m.replace("{ before($args); $_ = $proceed($$); }");
            }
        });

        CtMethod m2 = cc.getDeclaredMethod("q");
        m2.instrument(new ExprEditor() {
            public void edit(MethodCall m) throws CannotCompileException {
                if (m.getMethodName().equals("calc"))
                    m.replace("{ $_ = ($r)replace($args); }");
            }
        });

        CtMethod m3 = cc.getDeclaredMethod("s");
        m3.instrument(new ExprEditor() {
            public void edit(MethodCall m) throws CannotCompileException {
                if (m.getMethodName().equals("calc2"))
                    m.replace(
                        "{ long start = System.currentTimeMillis();"
                      + "$_ = $proceed($$);"
                      + "long elapsed = System.currentTimeMillis() - start;"
                      + "System.out.println(elapsed); }");
            }
        });

        CtMethod m4 = cc.getDeclaredMethod("t");
        m4.instrument(new ExprEditor() {
            public void edit(MethodCall m) throws CannotCompileException {
                if (m.getMethodName().equals("calc2"))
                    m.replace(
                        "{ long start = System.currentTimeMillis();"
                      + "$_ = $proceed($$);"
                      + "System.out.println(System.currentTimeMillis() - start);"
                      + "}");
            }
        });

        cc.writeFile();
        Object obj = make(cc.getName());
        int ptime = invoke(obj, "p");
        int qtime = invoke(obj, "q");
        System.out.println("time: (p) " + ptime + ", (q) " + qtime);
        System.out.println("s:");
        invoke(obj, "s");
        System.out.println("t:");
        invoke(obj, "t");
        assertTrue(ptime < qtime);
    }

    public void testProceedNew() throws Exception {
        CtClass cc = sloader.get("test.BenchProceedNew");
        CtMethod m1 = cc.getDeclaredMethod("jvst0");
        m1.instrument(new ExprEditor() {
            public void edit(NewExpr m) throws CannotCompileException {
                m.replace("{ $_ = $proceed($$); }");
            }
        });

        CtMethod m2 = cc.getDeclaredMethod("jvst2");
        m2.instrument(new ExprEditor() {
            public void edit(NewExpr m) throws CannotCompileException {
                m.replace("{ $_ = $proceed($$); }");
            }
        });

        cc.writeFile();
        Object obj = make(cc.getName());
        int qtime = invoke(obj, "jvst0");
        int ptime = invoke(obj, "org0");
        System.out.println("time: (org0) " + ptime + ", (jvst0) " + qtime);
        qtime = invoke(obj, "jvst2");
        ptime = invoke(obj, "org2");
        System.out.println("time: (org2) " + ptime + ", (jvst2) " + qtime);
    }

    public void testStaticMethod() throws Exception {
        CtClass cc = sloader.get("test.BenchStaticMethod");
        CtMethod m1 = cc.getDeclaredMethod("test");
        m1.instrument(new ExprEditor() {
            public void edit(MethodCall m) throws CannotCompileException {
                if (m.getMethodName().equals("foo"))
                    m.replace("{ num += $1; $_ = $proceed($$); }");
            }
        });

        cc.writeFile();
        Object obj = make(cc.getName());
        int qtime = invoke(obj, "test");
        int ptime = invoke(obj, "orgTest");
        System.out.println(
            "BenchStaticMethod time: (org) " + ptime + ", (jvst) " + qtime);
    }

    public void testStaticField() throws Exception {
        System.out.println(sloader);
        Javac jc = new Javac(sloader.get("test.StaticField"));
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++)
            jc.compileStmnt("{ int counter = 0; counter++; }");

        t0 = System.currentTimeMillis() - t0;
        System.out.println("local variable: " + (t0 * 10) + " usec");

        long t = System.currentTimeMillis();
        for (int i = 0; i < 100; i++)
            jc.compileStmnt("{ test.StaticField.counter++; }");

        t = System.currentTimeMillis() - t;
        System.out.println("StaticField: " + (t * 10) + " usec");

        long t2 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++)
            jc.compileStmnt("{ test.StaticField#counter++; }");

        t2 = System.currentTimeMillis() - t2;
        System.out.println("StaticField with #: " + (t2 * 10) + " usec");

        long t3 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++)
            jc.compileStmnt("{ StaticField.counter2++; }");

        t3 = System.currentTimeMillis() - t3;
        System.out.println("StaticField without package: " + (t3 * 10) + " usec");

        long t4 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++)
            jc.compileStmnt("{ test.StaticField.counter++; }");

        t4 = System.currentTimeMillis() - t4;
        System.out.println("StaticField: " + (t4 * 10) + " usec");

        long t5 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++)
            jc.compileStmnt("{ System.out.println(); }");

        t5 = System.currentTimeMillis() - t5;
        System.out.println("println: " + (t5 * 10) + " usec");
    }

    public static Test suite() {
        TestSuite suite = new TestSuite("Benchmark Tests");
        suite.addTestSuite(Bench.class);
        suite.addTestSuite(testproxy.ProxyFactoryPerformanceTest.class);
        return suite;
    }
}