Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
small benchmark
/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * 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.
*
* * Neither the name of Oracle 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 HOLDER 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.sample;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@State(Scope.Benchmark)
public class MyBenchmark {
private final int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 4, 5, 6, 87, 8, 32, 4, 7, 87, 9, 4, 4, 6, 8, 9, 0, 8, 7, 6544434, 7, 86, 45, 2, 23, 4, 65, 76, 978, 98};
private final Test test = new Test();
@Benchmark
public int testParam() {
return test.method(arr);
}
@Benchmark
public int testLocalCopy() {
return test.loc(arr);
}
@Benchmark
public int testClassCopy() {
return test.classField(arr);
}
}
$ java -jar target/benchmarks.jar -i 5 -f 1
# JMH version: 1.21
# VM version: JDK 1.8.0_212, OpenJDK 64-Bit Server VM, 25.212-b03
# VM invoker: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
# VM options: <none>
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: org.sample.MyBenchmark.testClassCopy
# Run progress: 0.00% complete, ETA 00:05:00
# Fork: 1 of 1
# Warmup Iteration 1: 64800001.095 ops/s
# Warmup Iteration 2: 65055049.836 ops/s
# Warmup Iteration 3: 63534447.364 ops/s
# Warmup Iteration 4: 63545317.482 ops/s
# Warmup Iteration 5: 63378989.424 ops/s
Iteration 1: 63511033.941 ops/s
Iteration 2: 63453558.734 ops/s
Iteration 3: 63618092.448 ops/s
Iteration 4: 63556281.577 ops/s
Iteration 5: 63603030.018 ops/s
Result "org.sample.MyBenchmark.testClassCopy":
63548399.343 ±(99.9%) 260414.892 ops/s [Average]
(min, avg, max) = (63453558.734, 63548399.343, 63618092.448), stdev = 67628.920
CI (99.9%): [63287984.452, 63808814.235] (assumes normal distribution)
# JMH version: 1.21
# VM version: JDK 1.8.0_212, OpenJDK 64-Bit Server VM, 25.212-b03
# VM invoker: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
# VM options: <none>
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: org.sample.MyBenchmark.testLocalCopy
# Run progress: 33.33% complete, ETA 00:03:20
# Fork: 1 of 1
# Warmup Iteration 1: 64321961.806 ops/s
# Warmup Iteration 2: 65115790.522 ops/s
# Warmup Iteration 3: 67281544.848 ops/s
# Warmup Iteration 4: 67472289.955 ops/s
# Warmup Iteration 5: 67445783.201 ops/s
Iteration 1: 67461610.591 ops/s
Iteration 2: 67403248.766 ops/s
Iteration 3: 67398926.887 ops/s
Iteration 4: 67290297.918 ops/s
Iteration 5: 67415154.512 ops/s
Result "org.sample.MyBenchmark.testLocalCopy":
67393847.735 ±(99.9%) 242640.416 ops/s [Average]
(min, avg, max) = (67290297.918, 67393847.735, 67461610.591), stdev = 63012.946
CI (99.9%): [67151207.319, 67636488.151] (assumes normal distribution)
# JMH version: 1.21
# VM version: JDK 1.8.0_212, OpenJDK 64-Bit Server VM, 25.212-b03
# VM invoker: /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
# VM options: <none>
# Warmup: 5 iterations, 10 s each
# Measurement: 5 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: org.sample.MyBenchmark.testParam
# Run progress: 66.67% complete, ETA 00:01:40
# Fork: 1 of 1
# Warmup Iteration 1: 64486239.016 ops/s
# Warmup Iteration 2: 65021681.755 ops/s
# Warmup Iteration 3: 67343361.890 ops/s
# Warmup Iteration 4: 67439647.325 ops/s
# Warmup Iteration 5: 67441841.910 ops/s
Iteration 1: 67538886.867 ops/s
Iteration 2: 67428599.764 ops/s
Iteration 3: 67326100.852 ops/s
Iteration 4: 67082316.410 ops/s
Iteration 5: 67383565.073 ops/s
Result "org.sample.MyBenchmark.testParam":
67351893.793 ±(99.9%) 653449.076 ops/s [Average]
(min, avg, max) = (67082316.410, 67351893.793, 67538886.867), stdev = 169698.650
CI (99.9%): [66698444.717, 68005342.869] (assumes normal distribution)
# Run complete. Total time: 00:05:00
REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.
Benchmark Mode Cnt Score Error Units
MyBenchmark.testClassCopy thrpt 5 63548399.343 ± 260414.892 ops/s
MyBenchmark.testLocalCopy thrpt 5 67393847.735 ± 242640.416 ops/s
MyBenchmark.testParam thrpt 5 67351893.793 ± 653449.076 ops/s
<!--
Copyright (c) 2014, Oracle America, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* 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.
* Neither the name of Oracle 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 HOLDER 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>JMH benchmark sample: Java</name>
<!--
This is the demo/sample template build script for building Java benchmarks with JMH.
Edit as needed.
-->
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--
JMH version to use with this project.
-->
<jmh.version>1.21</jmh.version>
<!--
Java source/target to use for compilation.
-->
<javac.target>1.8</javac.target>
<!--
Name of the benchmark Uber-JAR to generate.
-->
<uberjar.name>benchmarks</uberjar.name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>${javac.target}</compilerVersion>
<source>${javac.target}</source>
<target>${javac.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${uberjar.name}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<!--
Shading signed JARs will fail without this.
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
-->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package org.sample;
public class Test {
public int method(int[] val) {
int ret = 0;
int l = val.length;
for (int i = 0; i < l; i++) {
ret += val[i];
}
return ret;
}
public int loc(int[] val) {
int ret = 0;
int[] loc = val;
int l = val.length;
for (int i = 0; i < l; i++) {
ret += loc[i];
}
return ret;
}
int[] classField;
public int classField(int[] val) {
int ret = 0;
classField = val;
int l = val.length;
for (int i = 0; i < l; i++) {
ret += classField[i];
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.