Path: blob/master/test/micro/org/openjdk/bench/java/io/FileOpen.java
41161 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.io;2324import org.openjdk.jmh.annotations.*;25import org.openjdk.jmh.infra.Blackhole;2627import java.io.File;28import java.io.IOException;29import java.nio.file.Path;30import java.util.concurrent.TimeUnit;3132/**33* Tests the overheads of creating File objects, and converting such objects to Paths.34*/35@BenchmarkMode(Mode.AverageTime)36@OutputTimeUnit(TimeUnit.NANOSECONDS)37@State(Scope.Thread)38@Warmup(time=2, iterations=5)39@Measurement(time=3, iterations=5)40@Fork(value=2, jvmArgs="-Xmx1g")41public class FileOpen {4243private String normalFile = "/test/dir/file/name.txt";44private String root = "/";45private String trailingSlash = "/test/dir/file/name.txt/";46private String notNormalizedFile = "/test/dir/file//name.txt";4748public File tmp;4950@Setup51public void setup() throws IOException {52tmp = new File("FileOpen.tmp");53tmp.createNewFile();54tmp.deleteOnExit();55}5657@Benchmark58public void mix(Blackhole bh) {59bh.consume(new File(normalFile));60bh.consume(new File(root));61bh.consume(new File(trailingSlash));62bh.consume(new File(notNormalizedFile));63}6465@Benchmark66public File normalized() {67return new File(normalFile);68}6970@Benchmark71public File root() {72return new File(root);73}7475@Benchmark76public File trailingSlash() {77return new File(trailingSlash);78}7980@Benchmark81public File notNormalized() {82return new File(notNormalizedFile);83}8485@Benchmark86public boolean booleanAttributes() {87return tmp.exists()88&& tmp.isHidden()89&& tmp.isDirectory()90&& tmp.isFile();91}9293@Benchmark94public void mixToPath(Blackhole bh) {95bh.consume(new File(normalFile).toPath());96bh.consume(new File(root).toPath());97bh.consume(new File(trailingSlash).toPath());98bh.consume(new File(notNormalizedFile).toPath());99}100101@Benchmark102public Path normalizedToPath() {103return new File(normalFile).toPath();104}105106@Benchmark107public Path rootToPath() {108return new File(root).toPath();109}110111@Benchmark112public Path trailingSlashToPath() {113return new File(trailingSlash).toPath();114}115116@Benchmark117public Path notNormalizedToPath() {118return new File(notNormalizedFile).toPath();119}120}121122123