xref: /JGit/org.eclipse.jgit.benchmarks/src/org/eclipse/jgit/benchmarks/CreateFileSnapshotBenchmark.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1 /*
2  * Copyright (C) 2019, 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.Path;
15 import java.util.concurrent.TimeUnit;
16 
17 import org.eclipse.jgit.internal.storage.file.FileSnapshot;
18 import org.eclipse.jgit.util.FileUtils;
19 import org.openjdk.jmh.annotations.Benchmark;
20 import org.openjdk.jmh.annotations.BenchmarkMode;
21 import org.openjdk.jmh.annotations.Measurement;
22 import org.openjdk.jmh.annotations.Mode;
23 import org.openjdk.jmh.annotations.OutputTimeUnit;
24 import org.openjdk.jmh.annotations.Scope;
25 import org.openjdk.jmh.annotations.Setup;
26 import org.openjdk.jmh.annotations.State;
27 import org.openjdk.jmh.annotations.TearDown;
28 import org.openjdk.jmh.annotations.Warmup;
29 import org.openjdk.jmh.runner.Runner;
30 import org.openjdk.jmh.runner.RunnerException;
31 import org.openjdk.jmh.runner.options.Options;
32 import org.openjdk.jmh.runner.options.OptionsBuilder;
33 
34 @State(Scope.Thread)
35 public class CreateFileSnapshotBenchmark {
36 
37 	Path path;
38 
39 	Path testDir;
40 
41 	@Setup
setupBenchmark()42 	public void setupBenchmark() throws IOException {
43 		testDir = Files.createTempDirectory("dir");
44 		path = testDir.resolve("toSnapshot");
45 		Files.createFile(path);
46 	}
47 
48 	@TearDown
teardown()49 	public void teardown() throws IOException {
50 		FileUtils.delete(testDir.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
51 	}
52 
53 	@Benchmark
54 	@BenchmarkMode({ Mode.AverageTime })
55 	@OutputTimeUnit(TimeUnit.NANOSECONDS)
56     @Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS)
57     @Measurement(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS)
testCreateFile()58 	public Path testCreateFile() throws IOException {
59 		return Files.createTempFile(testDir, "create", "");
60 	}
61 
62 	@Benchmark
63 	@BenchmarkMode({ Mode.AverageTime })
64 	@OutputTimeUnit(TimeUnit.NANOSECONDS)
testCreateFileSnapshot()65 	public FileSnapshot testCreateFileSnapshot() {
66 		return FileSnapshot.save(path.toFile());
67 	}
68 
main(String[] args)69 	public static void main(String[] args) throws RunnerException {
70 		Options opt = new OptionsBuilder()
71 				.include(CreateFileSnapshotBenchmark.class.getSimpleName())
72 				// .addProfiler(StackProfiler.class)
73 				// .addProfiler(GCProfiler.class)
74 				.forks(1).jvmArgs("-ea").build();
75 		new Runner(opt).run();
76 	}
77 }
78