aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/bcel/PerformanceTest.java
blob: 404b69941a1ac5a0ef3d6d238ca73eeb4697fa45 (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.bcel;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.MethodGen;
import org.junit.Assert;

import junit.framework.TestCase;

public final class PerformanceTest extends TestCase {

    private static final boolean REPORT = Boolean.parseBoolean(System.getProperty("PerformanceTest.report", "true"));;

    private static byte[] read(final InputStream is) throws IOException {
        if (is == null) {
            throw new IOException("Class not found");
        }
        byte[] b = new byte[is.available()];
        int len = 0;
        while (true) {
            final int n = is.read(b, len, b.length - len);
            if (n == -1) {
                if (len < b.length) {
                    final byte[] c = new byte[len];
                    System.arraycopy(b, 0, c, 0, len);
                    b = c;
                }
                return b;
            }
            len += n;
            if (len == b.length) {
                final byte[] c = new byte[b.length + 1000];
                System.arraycopy(b, 0, c, 0, len);
                b = c;
            }
        }
    }

    private static void test(final File lib) throws IOException {
        final NanoTimer total = new NanoTimer();
        final NanoTimer parseTime = new NanoTimer();
        final NanoTimer cgenTime = new NanoTimer();
        final NanoTimer mgenTime = new NanoTimer();
        final NanoTimer mserTime = new NanoTimer();
        final NanoTimer serTime = new NanoTimer();

        System.out.println("parsing " + lib);

        total.start();
        try (JarFile jar = new JarFile(lib)) {
            final Enumeration<?> en = jar.entries();

            while (en.hasMoreElements()) {
                final JarEntry e = (JarEntry) en.nextElement();
                if (e.getName().endsWith(".class")) {
                    byte[] bytes;
                    try (InputStream in = jar.getInputStream(e)) {
                        bytes = read(in);
                    }

                    parseTime.start();
                    final JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), e.getName()).parse();
                    parseTime.stop();

                    cgenTime.start();
                    final ClassGen cg = new ClassGen(clazz);
                    cgenTime.stop();

                    final Method[] methods = cg.getMethods();
                    for (final Method m : methods) {
                        mgenTime.start();
                        final MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool());
                        final InstructionList il = mg.getInstructionList();
                        mgenTime.stop();

                        mserTime.start();
                        if (il != null) {
                            mg.getInstructionList().setPositions();
                            mg.setMaxLocals();
                            mg.setMaxStack();
                        }
                        cg.replaceMethod(m, mg.getMethod());
                        mserTime.stop();
                    }

                    serTime.start();
                    cg.getJavaClass().getBytes();
                    serTime.stop();
                }
            }
        }
        total.stop();
        if (REPORT) {
            System.out.println("ClassParser.parse: " + parseTime);
            System.out.println("ClassGen.init: " + cgenTime);
            System.out.println("MethodGen.init: " + mgenTime);
            System.out.println("MethodGen.getMethod: " + mserTime);
            System.out.println("ClassGen.getJavaClass.getBytes: " + serTime);
            System.out.println("Total: " + total);
            System.out.println();
        }
    }

    public void testPerformance() {
        final File javaLib = new File(System.getProperty("java.home"), "lib");
        javaLib.listFiles(new FileFilter() {

            @Override
            public boolean accept(final File file) {
                if(file.getName().endsWith(".jar")) {
                    try {
                        test(file);
                    } catch (final IOException e) {
                        Assert.fail(e.getMessage());
                    }
                }
                return false;
            }
        });
    }

}