Created
          June 25, 2019 13:17 
        
      - 
      
- 
        Save rzwitserloot/d6eda377fcb357a460524573cd3030d9 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // save this file as src/main/java/perftest/MyBenchmark.java relative to pom.xml | |
| package perftest; | |
| import java.io.BufferedInputStream; | |
| import java.io.FileInputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.nio.MappedByteBuffer; | |
| import java.nio.channels.FileChannel; | |
| import java.nio.charset.StandardCharsets; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| import java.nio.file.Paths; | |
| import java.nio.file.StandardCopyOption; | |
| import java.nio.file.StandardOpenOption; | |
| import java.util.List; | |
| import java.util.Random; | |
| import org.openjdk.jmh.annotations.Benchmark; | |
| import org.openjdk.jmh.annotations.Level; | |
| import org.openjdk.jmh.annotations.Scope; | |
| import org.openjdk.jmh.annotations.Setup; | |
| import org.openjdk.jmh.annotations.State; | |
| /* --- Run [001] --- | |
| java -version: | |
| openjdk version "11" 2018-09-25 | |
| OpenJDK Runtime Environment 18.9 (build 11+28) | |
| OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode) | |
| hardware: Macbook Pro 13" touchbar, NVMe disks, 2,9 GHz Intel Core i5, 8MB RAM. | |
| notes: running other apps in the mean time. | |
| parameters: buffer: 65536, filesize: 550737, files: 1. | |
| */ | |
| public class MyBenchmark { | |
| private static final Random rnd = new Random(); | |
| private static final String TESTFILE_DIR = "testfiles"; | |
| private static final String TESTFILE_PREFIX = "file-"; | |
| private static final long FILE_SIZE = 550737L; | |
| private static final int BUFFER_DEFAULT = 65536; | |
| private static final int GEN_FILES = 1000; | |
| @State(Scope.Benchmark) // without this, benchmark tries to stop me from using @Setup even though in this case the setup really is 'stateless' as far as the JVM memory is concerned; it's all an on-disk show. | |
| public static class FileGenerator { | |
| /** | |
| * This method generates GEN_FILES test files, each FILE_SIZE large, to be used for these tests in an attempt to get around any disk caching by the OS. Each file is filled with random data to get | |
| * around any attempt by the file system to do duplicate detection and from there offer the kernel the option to disk cache anyway. | |
| * | |
| * In order to avoid a lot of disk thrashing and wasting time, the generation process detects if a previous run already generated precisely the right files and if so, skips this step. | |
| */ | |
| @Setup(Level.Trial) | |
| public void genFiles() throws IOException { | |
| Path tgt = Paths.get(TESTFILE_DIR); | |
| Files.createDirectories(tgt); | |
| Path trk = tgt.resolve("track"); | |
| if (Files.isRegularFile(trk)) { | |
| List<String> x = Files.readAllLines(trk, StandardCharsets.UTF_8); | |
| if (x.size() >= 2 && Integer.parseInt(x.get(0)) == GEN_FILES && Long.parseLong(x.get(1)) == FILE_SIZE) return; | |
| } | |
| if (Files.exists(trk)) Files.delete(trk); | |
| byte[] b = new byte[(int) FILE_SIZE]; | |
| for (int i = 0; i < GEN_FILES; i++) { | |
| rnd.nextBytes(b); | |
| Files.write(tgt.resolve(String.format("%s%04d", TESTFILE_PREFIX, i)), b); | |
| } | |
| Path trkT = tgt.resolve("track.tmp"); | |
| // Record the files that are here and atomically move in to place. | |
| Files.write(trkT, (GEN_FILES + "\n" + FILE_SIZE + "\n").getBytes(StandardCharsets.US_ASCII)); | |
| Files.move(trkT, trk, StandardCopyOption.ATOMIC_MOVE); | |
| } | |
| /** Produces the file path to a single for-testing file. Uses randomization instead of cycling through the files in order because JMH forks and thus there's no good place to store the accumulator. */ | |
| public String getPath() { | |
| return String.format("%s/%s%04d", TESTFILE_DIR, TESTFILE_PREFIX, rnd.nextInt(GEN_FILES)); | |
| } | |
| } | |
| // [001] 3100 ops/sec | |
| @Benchmark | |
| public long readViaDoubleBufferedFis(FileGenerator gen) throws Exception { | |
| InputStream in = new BufferedInputStream(new FileInputStream(gen.getPath())); | |
| byte[] b = new byte[BUFFER_DEFAULT]; | |
| long sum = 0L; | |
| while (true) { | |
| int r = in.read(b); | |
| if (r == -1) break; | |
| for (int i = 0; i < r; i++) sum += (b[i] & 0xFF); | |
| } | |
| in.close(); | |
| return sum; | |
| } | |
| // [001] 3100 ops/sec | |
| @Benchmark | |
| public long readViaSelfBufferedFis(FileGenerator gen) throws Exception { | |
| InputStream in = new FileInputStream(gen.getPath()); | |
| byte[] b = new byte[BUFFER_DEFAULT]; | |
| long sum = 0L; | |
| while (true) { | |
| int r = in.read(b); | |
| if (r == -1) break; | |
| for (int i = 0; i < r; i++) sum += (b[i] & 0xFF); | |
| } | |
| in.close(); | |
| return sum; | |
| } | |
| // [001] 615 ops/sec | |
| @Benchmark | |
| public long readViaBufferedFis(FileGenerator gen) throws Exception { | |
| InputStream in = new BufferedInputStream(new FileInputStream(gen.getPath())); | |
| long sum = 0L; | |
| while (true) { | |
| int r = in.read(); | |
| if (r == -1) break; | |
| sum += r; | |
| } | |
| in.close(); | |
| return sum; | |
| } | |
| // [001] 1.6 ops/sec | |
| @Benchmark | |
| public long readViaUnbufferedFis(FileGenerator gen) throws Exception { | |
| InputStream in = new FileInputStream(gen.getPath()); | |
| long sum = 0L; | |
| while (true) { | |
| int r = in.read(); | |
| if (r == -1) break; | |
| sum += r; | |
| } | |
| in.close(); | |
| return sum; | |
| } | |
| // [001] 3200 ops/sec | |
| @Benchmark | |
| public long readViaFilesAll(FileGenerator gen) throws Exception { | |
| byte[] all = Files.readAllBytes(Paths.get(gen.getPath())); | |
| long sum = 0L; | |
| for (int i = 0; i < all.length; i++) sum += (all[i] & 0xFF); | |
| return sum; | |
| } | |
| // [001] 70 ops/sec | |
| @Benchmark | |
| public long readViaMMap(FileGenerator gen) throws Exception { | |
| FileChannel fc = FileChannel.open(Paths.get(gen.getPath()), StandardOpenOption.READ); | |
| MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, FILE_SIZE); | |
| long sum = 0L; | |
| while (bb.hasRemaining()) { | |
| sum += (bb.get() & 0xFF); | |
| } | |
| return sum; | |
| } | |
| // [001] 1000 ops/sec | |
| @Benchmark | |
| public long readViaBufferedMMap(FileGenerator gen) throws Exception { | |
| FileChannel fc = FileChannel.open(Paths.get(gen.getPath()), StandardOpenOption.READ); | |
| MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, FILE_SIZE); | |
| long sum = 0L; | |
| byte[] b = new byte[BUFFER_DEFAULT]; | |
| while (true) { | |
| int r = Math.min(BUFFER_DEFAULT, bb.remaining()); | |
| if (r == 0) break; | |
| bb.get(b, 0, r); | |
| for (int i = 0; i < r; i++) sum += (b[i] & 0xFF); | |
| } | |
| return sum; | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <!-- | |
| 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> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment