summaryrefslogtreecommitdiff
path: root/benchmarks/src/jmh/java/org/objectweb/asm/benchmarks/AbstractBenchmark.java
blob: 3fa6f1d0976dce9cdd244d83bca9d6e1a8adc230 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// ASM: a very small and fast Java bytecode manipulation framework
// Copyright (c) 2000-2011 INRIA, France Telecom
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holders nor the names of its
//    contributors may be used to endorse or promote products derived from
//    this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm.benchmarks;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;

/**
 * A benchmark to benchmark different versions of ASM and possibly other bytecode libraries.
 *
 * @author Eric Bruneton
 */
public abstract class AbstractBenchmark {

  // The directories where the different versions of ASM can be found.
  private static final String BUILD_DIR = "/benchmarks/build/";
  private static final String ASM4_0 = BUILD_DIR + "asm4.0/";
  private static final String ASM5_0 = BUILD_DIR + "asm5.0.1/";
  private static final String ASM6_0 = BUILD_DIR + "asm6.0/";
  private static final String ASM6_1 = BUILD_DIR + "asm6.1.1/";
  private static final String ASM6_2 = BUILD_DIR + "asm6.2.1/";
  private static final String ASM_CORE_CURRENT = "/asm/build/classes/java/main/";
  private static final String ASM_TREE_CURRENT = "/asm-tree/build/classes/java/main/";

  private final String asmBenchmarkClass;
  private final String userDir;

  /** Some class files that can be used as input data for benchmarks. */
  protected ArrayList<byte[]> classFiles;

  /** The ASM versions that can be benchmarked. */
  public enum AsmVersion {
    V4_0,
    V5_0,
    V6_0,
    V6_1,
    V6_2,
    V_CURRENT;

    URL[] getUrls(final String baseUrl) throws MalformedURLException {
      switch (this) {
        case V4_0:
          return new URL[] {new URL(baseUrl + ASM4_0)};
        case V5_0:
          return new URL[] {new URL(baseUrl + ASM5_0)};
        case V6_0:
          return new URL[] {new URL(baseUrl + ASM6_0)};
        case V6_1:
          return new URL[] {new URL(baseUrl + ASM6_1)};
        case V6_2:
          return new URL[] {new URL(baseUrl + ASM6_2)};
        case V_CURRENT:
          return new URL[] {
            new URL(baseUrl + ASM_CORE_CURRENT), new URL(baseUrl + ASM_TREE_CURRENT)
          };
        default:
          throw new AssertionError();
      }
    }
  }

  /**
   * Constructs a new {@link AbstractBenchmark}.
   *
   * @param asmBenchmarkClass the benchmark class to instantiate for the ASM benchmarks.
   */
  protected AbstractBenchmark(final String asmBenchmarkClass) {
    this.asmBenchmarkClass = asmBenchmarkClass;
    this.userDir = System.getProperty("user.dir");
  }

  /** Creates and populates {@link #classFiles} with some class files read from disk. */
  protected void prepareClasses() throws IOException {
    classFiles = new ArrayList<byte[]>();
    findClasses(new File(userDir + ASM_CORE_CURRENT), classFiles);
    findClasses(new File(userDir + ASM_TREE_CURRENT), classFiles);
  }

  private static void findClasses(final File directory, final ArrayList<byte[]> classFiles)
      throws IOException {
    for (File file : directory.listFiles()) {
      if (file.isDirectory()) {
        findClasses(file, classFiles);
      } else if (file.getName().endsWith(".class")) {
        classFiles.add(readInputStream(new FileInputStream(file)));
      }
    }
  }

  private static byte[] readInputStream(final InputStream inputStream) throws IOException {
    try {
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      byte[] data = new byte[8192];
      int bytesRead;
      while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
        outputStream.write(data, 0, bytesRead);
      }
      outputStream.flush();
      return outputStream.toByteArray();
    } finally {
      try {
        inputStream.close();
      } catch (IOException e) {
        // Nothing to do.
      }
    }
  }

  /** A factory of ASM benchmark objects, using a specific version of the ASM library. */
  class AsmBenchmarkFactory extends URLClassLoader {

    /**
     * Constructs an {@link AsmBenchmarkFactory}.
     *
     * @param asmVersion the ASM version to use.
     * @throws MalformedURLException
     */
    AsmBenchmarkFactory(final AsmVersion asmVersion) throws MalformedURLException {
      super(asmVersion.getUrls("file://" + userDir));
    }

    /**
     * @return a new instance of the class specified in the benchmark's constructor.
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public Object newAsmBenchmark()
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
      return loadClass(asmBenchmarkClass).newInstance();
    }

    @Override
    protected Class<?> loadClass(final String name, final boolean resolve)
        throws ClassNotFoundException {
      // Force the loading of the asmBenchmarkClass class by this class loader (and not its parent).
      // This is needed to make sure that the classes it references (i.e. the ASM library classes)
      // will be loaded by this class loader too.
      if (name.startsWith(asmBenchmarkClass)) {
        try {
          byte[] classFile =
              readInputStream(getResourceAsStream(name.replace('.', '/') + ".class"));
          Class<?> c = defineClass(name, classFile, 0, classFile.length);
          if (resolve) {
            resolveClass(c);
          }
          return c;
        } catch (Exception e) {
          throw new ClassNotFoundException(name, e);
        }
      }
      // Look for the specified class *first* in asmDirectories, *then* using the parent class
      // loader. This is the reverse of the default lookup order, and is necessary to make sure we
      // load the correct version of ASM (the parent class loader may have an ASM version in its
      // class path, because some components of the JMH framework depend on ASM).
      try {
        Class<?> c = findClass(name);
        if (resolve) {
          resolveClass(c);
        }
        return c;
      } catch (ClassNotFoundException e) {
        return super.loadClass(name, resolve);
      }
    }
  }
}