You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CreateFileSnapshotBenchmark.java 2.4KB

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