1*8a44216eSMatthias Sohn /* 2*8a44216eSMatthias Sohn * Copyright (C) 2020, Matthias Sohn <matthias.sohn@sap.com> and others 3*8a44216eSMatthias Sohn * 4*8a44216eSMatthias Sohn * This program and the accompanying materials are made available under the 5*8a44216eSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 6*8a44216eSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 7*8a44216eSMatthias Sohn * 8*8a44216eSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 9*8a44216eSMatthias Sohn */ 10*8a44216eSMatthias Sohn package org.eclipse.jgit.benchmarks; 11*8a44216eSMatthias Sohn 12*8a44216eSMatthias Sohn import java.io.IOException; 13*8a44216eSMatthias Sohn import java.nio.file.Files; 14*8a44216eSMatthias Sohn import java.nio.file.NoSuchFileException; 15*8a44216eSMatthias Sohn import java.nio.file.Path; 16*8a44216eSMatthias Sohn import java.nio.file.StandardCopyOption; 17*8a44216eSMatthias Sohn import java.util.concurrent.TimeUnit; 18*8a44216eSMatthias Sohn 19*8a44216eSMatthias Sohn import org.eclipse.jgit.util.FileUtils; 20*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.Benchmark; 21*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.BenchmarkMode; 22*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.Measurement; 23*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.Mode; 24*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.OutputTimeUnit; 25*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.Scope; 26*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.Setup; 27*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.State; 28*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.TearDown; 29*8a44216eSMatthias Sohn import org.openjdk.jmh.annotations.Warmup; 30*8a44216eSMatthias Sohn import org.openjdk.jmh.runner.Runner; 31*8a44216eSMatthias Sohn import org.openjdk.jmh.runner.RunnerException; 32*8a44216eSMatthias Sohn import org.openjdk.jmh.runner.options.Options; 33*8a44216eSMatthias Sohn import org.openjdk.jmh.runner.options.OptionsBuilder; 34*8a44216eSMatthias Sohn 35*8a44216eSMatthias Sohn @State(Scope.Thread) 36*8a44216eSMatthias Sohn public class FileMoveBenchmark { 37*8a44216eSMatthias Sohn int i; 38*8a44216eSMatthias Sohn 39*8a44216eSMatthias Sohn Path testDir; 40*8a44216eSMatthias Sohn 41*8a44216eSMatthias Sohn Path targetDir; 42*8a44216eSMatthias Sohn 43*8a44216eSMatthias Sohn @Setup setupBenchmark()44*8a44216eSMatthias Sohn public void setupBenchmark() throws IOException { 45*8a44216eSMatthias Sohn testDir = Files.createTempDirectory("dir"); 46*8a44216eSMatthias Sohn targetDir = testDir.resolve("target"); 47*8a44216eSMatthias Sohn Files.createDirectory(targetDir); 48*8a44216eSMatthias Sohn } 49*8a44216eSMatthias Sohn 50*8a44216eSMatthias Sohn @TearDown teardown()51*8a44216eSMatthias Sohn public void teardown() throws IOException { 52*8a44216eSMatthias Sohn FileUtils.delete(testDir.toFile(), 53*8a44216eSMatthias Sohn FileUtils.RECURSIVE | FileUtils.RETRY); 54*8a44216eSMatthias Sohn } 55*8a44216eSMatthias Sohn 56*8a44216eSMatthias Sohn @Benchmark 57*8a44216eSMatthias Sohn @BenchmarkMode({ Mode.AverageTime }) 58*8a44216eSMatthias Sohn @OutputTimeUnit(TimeUnit.MICROSECONDS) 59*8a44216eSMatthias Sohn @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS) 60*8a44216eSMatthias Sohn @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS) moveFileToExistingDir()61*8a44216eSMatthias Sohn public Path moveFileToExistingDir() throws IOException { 62*8a44216eSMatthias Sohn i++; 63*8a44216eSMatthias Sohn Path tmp = testDir.resolve("tmp" + i++); 64*8a44216eSMatthias Sohn Files.createFile(tmp); 65*8a44216eSMatthias Sohn Path targetDirectory = targetDir; 66*8a44216eSMatthias Sohn Path targetFile = targetDirectory.resolve("tmp" + i); 67*8a44216eSMatthias Sohn try { 68*8a44216eSMatthias Sohn return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE); 69*8a44216eSMatthias Sohn } catch (NoSuchFileException e) { 70*8a44216eSMatthias Sohn Files.createDirectory(targetDirectory); 71*8a44216eSMatthias Sohn return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE); 72*8a44216eSMatthias Sohn } 73*8a44216eSMatthias Sohn } 74*8a44216eSMatthias Sohn 75*8a44216eSMatthias Sohn @Benchmark 76*8a44216eSMatthias Sohn @BenchmarkMode({ Mode.AverageTime }) 77*8a44216eSMatthias Sohn @OutputTimeUnit(TimeUnit.MICROSECONDS) 78*8a44216eSMatthias Sohn @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS) 79*8a44216eSMatthias Sohn @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS) moveFileToExistingDirExists()80*8a44216eSMatthias Sohn public Path moveFileToExistingDirExists() throws IOException { 81*8a44216eSMatthias Sohn Path tmp = testDir.resolve("tmp" + i++); 82*8a44216eSMatthias Sohn Files.createFile(tmp); 83*8a44216eSMatthias Sohn Path targetDirectory = targetDir; 84*8a44216eSMatthias Sohn Path targetFile = targetDir.resolve("tmp" + i); 85*8a44216eSMatthias Sohn if (!targetDirectory.toFile().exists()) { 86*8a44216eSMatthias Sohn Files.createDirectory(targetDirectory); 87*8a44216eSMatthias Sohn } 88*8a44216eSMatthias Sohn return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE); 89*8a44216eSMatthias Sohn } 90*8a44216eSMatthias Sohn 91*8a44216eSMatthias Sohn @Benchmark 92*8a44216eSMatthias Sohn @BenchmarkMode({ Mode.AverageTime }) 93*8a44216eSMatthias Sohn @OutputTimeUnit(TimeUnit.MICROSECONDS) 94*8a44216eSMatthias Sohn @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS) 95*8a44216eSMatthias Sohn @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS) moveFileToMissingDir()96*8a44216eSMatthias Sohn public Path moveFileToMissingDir() throws IOException { 97*8a44216eSMatthias Sohn i++; 98*8a44216eSMatthias Sohn Path tmp = testDir.resolve("tmp" + i); 99*8a44216eSMatthias Sohn Files.createFile(tmp); 100*8a44216eSMatthias Sohn Path targetDirectory = testDir.resolve("target" + i); 101*8a44216eSMatthias Sohn Path targetFile = targetDirectory.resolve("tmp" + i); 102*8a44216eSMatthias Sohn try { 103*8a44216eSMatthias Sohn return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE); 104*8a44216eSMatthias Sohn } catch (NoSuchFileException e) { 105*8a44216eSMatthias Sohn Files.createDirectory(targetDirectory); 106*8a44216eSMatthias Sohn return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE); 107*8a44216eSMatthias Sohn } 108*8a44216eSMatthias Sohn } 109*8a44216eSMatthias Sohn 110*8a44216eSMatthias Sohn @Benchmark 111*8a44216eSMatthias Sohn @BenchmarkMode({ Mode.AverageTime }) 112*8a44216eSMatthias Sohn @OutputTimeUnit(TimeUnit.MICROSECONDS) 113*8a44216eSMatthias Sohn @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS) 114*8a44216eSMatthias Sohn @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS) moveFileToMissingDirExists()115*8a44216eSMatthias Sohn public Path moveFileToMissingDirExists() throws IOException { 116*8a44216eSMatthias Sohn i++; 117*8a44216eSMatthias Sohn Path tmp = testDir.resolve("tmp" + i); 118*8a44216eSMatthias Sohn Files.createFile(tmp); 119*8a44216eSMatthias Sohn Path targetDirectory = testDir.resolve("target" + i); 120*8a44216eSMatthias Sohn Path targetFile = targetDirectory.resolve("tmp" + i); 121*8a44216eSMatthias Sohn if (!targetDirectory.toFile().exists()) { 122*8a44216eSMatthias Sohn Files.createDirectory(targetDirectory); 123*8a44216eSMatthias Sohn } 124*8a44216eSMatthias Sohn return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE); 125*8a44216eSMatthias Sohn } 126*8a44216eSMatthias Sohn main(String[] args)127*8a44216eSMatthias Sohn public static void main(String[] args) throws RunnerException { 128*8a44216eSMatthias Sohn Options opt = new OptionsBuilder() 129*8a44216eSMatthias Sohn .include(FileMoveBenchmark.class 130*8a44216eSMatthias Sohn .getSimpleName()) 131*8a44216eSMatthias Sohn // .addProfiler(StackProfiler.class) 132*8a44216eSMatthias Sohn // .addProfiler(GCProfiler.class) 133*8a44216eSMatthias Sohn .forks(1).jvmArgs("-ea").build(); 134*8a44216eSMatthias Sohn new Runner(opt).run(); 135*8a44216eSMatthias Sohn } 136*8a44216eSMatthias Sohn }