• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.bcel;
19 
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.util.concurrent.TimeUnit;
23 import java.util.jar.JarEntry;
24 import java.util.jar.JarFile;
25 
26 import org.apache.bcel.classfile.ClassParser;
27 import org.apache.bcel.classfile.JavaClass;
28 import org.apache.bcel.classfile.Method;
29 import org.apache.bcel.generic.ClassGen;
30 import org.apache.bcel.generic.InstructionList;
31 import org.apache.bcel.generic.MethodGen;
32 import org.apache.commons.collections4.Predicate;
33 import org.apache.commons.collections4.iterators.EnumerationIterator;
34 import org.apache.commons.collections4.iterators.FilterIterator;
35 import org.apache.commons.collections4.iterators.IteratorIterable;
36 import org.apache.commons.io.IOUtils;
37 import org.openjdk.jmh.annotations.Benchmark;
38 import org.openjdk.jmh.annotations.BenchmarkMode;
39 import org.openjdk.jmh.annotations.Fork;
40 import org.openjdk.jmh.annotations.Measurement;
41 import org.openjdk.jmh.annotations.Mode;
42 import org.openjdk.jmh.annotations.OutputTimeUnit;
43 import org.openjdk.jmh.annotations.Threads;
44 import org.openjdk.jmh.annotations.Warmup;
45 import org.openjdk.jmh.infra.Blackhole;
46 
47 @BenchmarkMode(Mode.AverageTime)
48 @Fork(value = 1, jvmArgs = "-server")
49 @Threads(1)
50 @Warmup(iterations = 10)
51 @Measurement(iterations = 20)
52 @OutputTimeUnit(TimeUnit.MILLISECONDS)
53 public class BCELBenchmark {
54 
getJarFile()55     private JarFile getJarFile() throws IOException {
56         String javaHome = System.getProperty("java.home");
57         return new JarFile(javaHome + "/lib/rt.jar");
58     }
59 
getClasses(JarFile jar)60     private Iterable<JarEntry> getClasses(JarFile jar) {
61         return new IteratorIterable<>(new FilterIterator<>(new EnumerationIterator<>(jar.entries()), new Predicate<JarEntry>() {
62             @Override
63             public boolean evaluate(JarEntry entry) {
64                 return entry.getName().endsWith(".class");
65             }
66         }));
67     }
68 
69     /**
70      * Baseline benchmark. Read the classes but don't parse them.
71      */
72     @Benchmark
73     public void baseline(Blackhole bh) throws IOException {
74         JarFile jar = getJarFile();
75 
76         for (JarEntry entry : getClasses(jar)) {
77             byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));
78             bh.consume(bytes);
79         }
80 
81         jar.close();
82     }
83 
84     @Benchmark
85     public void parser(Blackhole bh) throws IOException {
86         JarFile jar = getJarFile();
87 
88         for (JarEntry entry : getClasses(jar)) {
89             byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));
90 
91             JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse();
92             bh.consume(clazz);
93         }
94 
95         jar.close();
96     }
97 
98     @Benchmark
99     public void generator(Blackhole bh) throws IOException {
100         JarFile jar = getJarFile();
101 
102         for (JarEntry entry : getClasses(jar)) {
103             byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));
104 
105             JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse();
106 
107             ClassGen cg = new ClassGen(clazz);
108 
109             for (Method m : cg.getMethods()) {
110                 MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool());
111                 InstructionList il = mg.getInstructionList();
112 
113                 if (il != null) {
114                     mg.getInstructionList().setPositions();
115                     mg.setMaxLocals();
116                     mg.setMaxStack();
117                 }
118                 cg.replaceMethod(m, mg.getMethod());
119             }
120 
121             bh.consume(cg.getJavaClass().getBytes());
122         }
123 
124         jar.close();
125     }
126 }
127