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