xref: /JGit/org.eclipse.jgit.benchmarks/src/org/eclipse/jgit/benchmarks/CreateFileSnapshotBenchmark.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
106de3a1dSMatthias Sohn /*
2*5c5f7c6bSMatthias Sohn  * Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.com> and others
306de3a1dSMatthias Sohn  *
4*5c5f7c6bSMatthias Sohn  * This program and the accompanying materials are made available under the
5*5c5f7c6bSMatthias Sohn  * terms of the Eclipse Distribution License v. 1.0 which is available at
6*5c5f7c6bSMatthias Sohn  * https://www.eclipse.org/org/documents/edl-v10.php.
706de3a1dSMatthias Sohn  *
8*5c5f7c6bSMatthias Sohn  * SPDX-License-Identifier: BSD-3-Clause
906de3a1dSMatthias Sohn  */
1006de3a1dSMatthias Sohn package org.eclipse.jgit.benchmarks;
1106de3a1dSMatthias Sohn 
1206de3a1dSMatthias Sohn import java.io.IOException;
1306de3a1dSMatthias Sohn import java.nio.file.Files;
1406de3a1dSMatthias Sohn import java.nio.file.Path;
1506de3a1dSMatthias Sohn import java.util.concurrent.TimeUnit;
1606de3a1dSMatthias Sohn 
1706de3a1dSMatthias Sohn import org.eclipse.jgit.internal.storage.file.FileSnapshot;
1806de3a1dSMatthias Sohn import org.eclipse.jgit.util.FileUtils;
1906de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.Benchmark;
2006de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.BenchmarkMode;
2106de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.Measurement;
2206de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.Mode;
2306de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.OutputTimeUnit;
2406de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.Scope;
2506de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.Setup;
2606de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.State;
2706de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.TearDown;
2806de3a1dSMatthias Sohn import org.openjdk.jmh.annotations.Warmup;
2906de3a1dSMatthias Sohn import org.openjdk.jmh.runner.Runner;
3006de3a1dSMatthias Sohn import org.openjdk.jmh.runner.RunnerException;
3106de3a1dSMatthias Sohn import org.openjdk.jmh.runner.options.Options;
3206de3a1dSMatthias Sohn import org.openjdk.jmh.runner.options.OptionsBuilder;
3306de3a1dSMatthias Sohn 
3406de3a1dSMatthias Sohn @State(Scope.Thread)
3506de3a1dSMatthias Sohn public class CreateFileSnapshotBenchmark {
3606de3a1dSMatthias Sohn 
3706de3a1dSMatthias Sohn 	Path path;
3806de3a1dSMatthias Sohn 
3906de3a1dSMatthias Sohn 	Path testDir;
4006de3a1dSMatthias Sohn 
4106de3a1dSMatthias Sohn 	@Setup
setupBenchmark()4206de3a1dSMatthias Sohn 	public void setupBenchmark() throws IOException {
4306de3a1dSMatthias Sohn 		testDir = Files.createTempDirectory("dir");
4406de3a1dSMatthias Sohn 		path = testDir.resolve("toSnapshot");
4506de3a1dSMatthias Sohn 		Files.createFile(path);
4606de3a1dSMatthias Sohn 	}
4706de3a1dSMatthias Sohn 
4806de3a1dSMatthias Sohn 	@TearDown
teardown()4906de3a1dSMatthias Sohn 	public void teardown() throws IOException {
5006de3a1dSMatthias Sohn 		FileUtils.delete(testDir.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
5106de3a1dSMatthias Sohn 	}
5206de3a1dSMatthias Sohn 
5306de3a1dSMatthias Sohn 	@Benchmark
5406de3a1dSMatthias Sohn 	@BenchmarkMode({ Mode.AverageTime })
5506de3a1dSMatthias Sohn 	@OutputTimeUnit(TimeUnit.NANOSECONDS)
5606de3a1dSMatthias Sohn     @Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS)
5706de3a1dSMatthias Sohn     @Measurement(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS)
testCreateFile()5806de3a1dSMatthias Sohn 	public Path testCreateFile() throws IOException {
5906de3a1dSMatthias Sohn 		return Files.createTempFile(testDir, "create", "");
6006de3a1dSMatthias Sohn 	}
6106de3a1dSMatthias Sohn 
6206de3a1dSMatthias Sohn 	@Benchmark
6306de3a1dSMatthias Sohn 	@BenchmarkMode({ Mode.AverageTime })
6406de3a1dSMatthias Sohn 	@OutputTimeUnit(TimeUnit.NANOSECONDS)
testCreateFileSnapshot()6506de3a1dSMatthias Sohn 	public FileSnapshot testCreateFileSnapshot() {
6606de3a1dSMatthias Sohn 		return FileSnapshot.save(path.toFile());
6706de3a1dSMatthias Sohn 	}
6806de3a1dSMatthias Sohn 
main(String[] args)6906de3a1dSMatthias Sohn 	public static void main(String[] args) throws RunnerException {
7006de3a1dSMatthias Sohn 		Options opt = new OptionsBuilder()
7106de3a1dSMatthias Sohn 				.include(CreateFileSnapshotBenchmark.class.getSimpleName())
7206de3a1dSMatthias Sohn 				// .addProfiler(StackProfiler.class)
7306de3a1dSMatthias Sohn 				// .addProfiler(GCProfiler.class)
7406de3a1dSMatthias Sohn 				.forks(1).jvmArgs("-ea").build();
7506de3a1dSMatthias Sohn 		new Runner(opt).run();
7606de3a1dSMatthias Sohn 	}
7706de3a1dSMatthias Sohn }
78