You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2014/12/19 00:48:27 UTC

svn commit: r1646584 - in /commons/proper/bcel/trunk: pom.xml src/test/java/org/apache/bcel/BCELBenchmark.java src/test/java/org/apache/bcel/PerformanceTest.java

Author: ebourg
Date: Thu Dec 18 23:48:27 2014
New Revision: 1646584

URL: http://svn.apache.org/r1646584
Log:
Added a JMH benchmark for the class parser and the class generator (run with: mvn test -Pbenchmark)

Added:
    commons/proper/bcel/trunk/src/test/java/org/apache/bcel/BCELBenchmark.java   (with props)
Modified:
    commons/proper/bcel/trunk/pom.xml
    commons/proper/bcel/trunk/src/test/java/org/apache/bcel/PerformanceTest.java

Modified: commons/proper/bcel/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/pom.xml?rev=1646584&r1=1646583&r2=1646584&view=diff
==============================================================================
--- commons/proper/bcel/trunk/pom.xml (original)
+++ commons/proper/bcel/trunk/pom.xml Thu Dec 18 23:48:27 2014
@@ -190,6 +190,14 @@
         </executions>
       </plugin>
       <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <testExcludes>
+            <testExclude>**/*Benchmark*</testExclude>
+          </testExcludes>
+        </configuration>
+      </plugin>
+      <plugin>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
           <includes>
@@ -341,6 +349,89 @@
         <findbugs.plugin.version>3.0.0</findbugs.plugin.version>
       </properties>
     </profile>
+
+    <!-- Profile to build and run the benchmarks. Use 'mvn test -Pbenchmark', and add '-Dbenchmark=foo' to run only the foo benchmark -->
+    <profile>
+      <id>benchmark</id>
+      
+      <dependencies>
+        <dependency>
+          <groupId>org.openjdk.jmh</groupId>
+          <artifactId>jmh-core</artifactId>
+          <version>1.3.4</version>
+          <scope>test</scope>
+        </dependency>
+    
+        <dependency>
+          <groupId>org.openjdk.jmh</groupId>
+          <artifactId>jmh-generator-annprocess</artifactId>
+          <version>1.3.4</version>
+          <scope>test</scope>
+        </dependency>
+        
+        <dependency>
+          <groupId>commons-io</groupId>
+          <artifactId>commons-io</artifactId>
+          <version>2.4</version>
+          <scope>test</scope>
+        </dependency>
+        
+        <dependency>
+          <groupId>org.apache.commons</groupId>
+          <artifactId>commons-collections4</artifactId>
+          <version>4.0</version>
+          <scope>test</scope>
+        </dependency>
+      </dependencies>
+      
+      <properties>
+        <skipTests>true</skipTests>
+        <benchmark>org.apache</benchmark>
+      </properties>
+      
+      <build>
+        <plugins>
+          <!-- Enable the compilation of the benchmarks -->
+          <plugin>
+            <artifactId>maven-compiler-plugin</artifactId>
+            <configuration combine.self="override">
+              <testIncludes>
+                <testInclude>**/*</testInclude>
+              </testIncludes>
+            </configuration>
+          </plugin>
+          
+          <!-- Hook the benchmarks to the test phase -->
+          <plugin>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>exec-maven-plugin</artifactId>
+            <executions>
+              <execution>
+                <id>benchmark</id>
+                <phase>test</phase>
+                <goals>
+                  <goal>exec</goal>
+                </goals>
+                <configuration>
+                  <classpathScope>test</classpathScope>
+                  <executable>java</executable>
+                  <arguments>
+                    <argument>-classpath</argument>
+                    <classpath/>
+                    <argument>org.openjdk.jmh.Main</argument>
+                    <argument>-rf</argument>
+                    <argument>json</argument>
+                    <argument>-rff</argument>
+                    <argument>target/jmh-result.json</argument>
+                    <argument>${benchmark}</argument>
+                  </arguments>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
   </profiles>
-  
+
 </project>

Added: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/BCELBenchmark.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/bcel/BCELBenchmark.java?rev=1646584&view=auto
==============================================================================
--- commons/proper/bcel/trunk/src/test/java/org/apache/bcel/BCELBenchmark.java (added)
+++ commons/proper/bcel/trunk/src/test/java/org/apache/bcel/BCELBenchmark.java Thu Dec 18 23:48:27 2014
@@ -0,0 +1,125 @@
+/*
+ * 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.IOException;
+import java.util.concurrent.TimeUnit;
+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.apache.commons.collections4.Predicate;
+import org.apache.commons.collections4.iterators.EnumerationIterator;
+import org.apache.commons.collections4.iterators.FilterIterator;
+import org.apache.commons.collections4.iterators.IteratorIterable;
+import org.apache.commons.io.IOUtils;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+@BenchmarkMode(Mode.AverageTime)
+@Fork(value = 1, jvmArgs = "-server")
+@Threads(1)
+@Warmup(iterations = 10)
+@Measurement(iterations = 20)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+public class BCELBenchmark {
+
+    private JarFile getJarFile() throws IOException {
+        String javaHome = System.getProperty("java.home");
+        return new JarFile(javaHome + "/lib/rt.jar");
+    }
+
+    private Iterable<JarEntry> getClasses(JarFile jar) {
+        return new IteratorIterable<JarEntry>(new FilterIterator<JarEntry>(new EnumerationIterator<JarEntry>(jar.entries()), new Predicate<JarEntry>() {
+            public boolean evaluate(JarEntry entry) {
+                return entry.getName().endsWith(".class");
+            }
+        }));
+    }
+
+    /**
+     * Baseline benchmark. Read the classes but don't parse them.
+     */
+    @Benchmark
+    public void baseline(Blackhole bh) throws IOException {
+        JarFile jar = getJarFile();
+
+        for (JarEntry entry : getClasses(jar)) {
+            byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));
+            bh.consume(bytes);
+        }
+
+        jar.close();
+    }
+
+    @Benchmark
+    public void parser(Blackhole bh) throws IOException {
+        JarFile jar = getJarFile();
+
+        for (JarEntry entry : getClasses(jar)) {
+            byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));
+
+            JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse();
+            bh.consume(clazz);
+        }
+
+        jar.close();
+    }
+
+    @Benchmark
+    public void generator(Blackhole bh) throws IOException {
+        JarFile jar = getJarFile();
+
+        for (JarEntry entry : getClasses(jar)) {
+            byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));
+
+            JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse();
+
+            ClassGen cg = new ClassGen(clazz);
+
+            for (Method m : cg.getMethods()) {
+                MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool());
+                InstructionList il = mg.getInstructionList();
+
+                if (il != null) {
+                    mg.getInstructionList().setPositions();
+                    mg.setMaxLocals();
+                    mg.setMaxStack();
+                }
+                cg.replaceMethod(m, mg.getMethod());
+            }
+
+            bh.consume(cg.getJavaClass().getBytes());
+        }
+
+        jar.close();
+    }
+}

Propchange: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/BCELBenchmark.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/BCELBenchmark.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: commons/proper/bcel/trunk/src/test/java/org/apache/bcel/PerformanceTest.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/test/java/org/apache/bcel/PerformanceTest.java?rev=1646584&r1=1646583&r2=1646584&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/test/java/org/apache/bcel/PerformanceTest.java (original)
+++ commons/proper/bcel/trunk/src/test/java/org/apache/bcel/PerformanceTest.java Thu Dec 18 23:48:27 2014
@@ -73,7 +73,7 @@ public final class PerformanceTest exten
 
         String javaHome = System.getProperty("java.home");
 
-        JarFile jar = new JarFile(javaHome + "/lib/dt.jar");
+        JarFile jar = new JarFile(javaHome + "/lib/rt.jar");
         Enumeration<?> en = jar.entries();
         int i = 0;