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.

FileMoveBenchmark.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import java.io.IOException;
  12. import java.nio.file.Files;
  13. import java.nio.file.NoSuchFileException;
  14. import java.nio.file.Path;
  15. import java.nio.file.StandardCopyOption;
  16. import java.util.concurrent.TimeUnit;
  17. import org.eclipse.jgit.util.FileUtils;
  18. import org.openjdk.jmh.annotations.Benchmark;
  19. import org.openjdk.jmh.annotations.BenchmarkMode;
  20. import org.openjdk.jmh.annotations.Measurement;
  21. import org.openjdk.jmh.annotations.Mode;
  22. import org.openjdk.jmh.annotations.OutputTimeUnit;
  23. import org.openjdk.jmh.annotations.Scope;
  24. import org.openjdk.jmh.annotations.Setup;
  25. import org.openjdk.jmh.annotations.State;
  26. import org.openjdk.jmh.annotations.TearDown;
  27. import org.openjdk.jmh.annotations.Warmup;
  28. import org.openjdk.jmh.runner.Runner;
  29. import org.openjdk.jmh.runner.RunnerException;
  30. import org.openjdk.jmh.runner.options.Options;
  31. import org.openjdk.jmh.runner.options.OptionsBuilder;
  32. @State(Scope.Thread)
  33. public class FileMoveBenchmark {
  34. int i;
  35. Path testDir;
  36. Path targetDir;
  37. @Setup
  38. public void setupBenchmark() throws IOException {
  39. testDir = Files.createTempDirectory("dir");
  40. targetDir = testDir.resolve("target");
  41. Files.createDirectory(targetDir);
  42. }
  43. @TearDown
  44. public void teardown() throws IOException {
  45. FileUtils.delete(testDir.toFile(),
  46. FileUtils.RECURSIVE | FileUtils.RETRY);
  47. }
  48. @Benchmark
  49. @BenchmarkMode({ Mode.AverageTime })
  50. @OutputTimeUnit(TimeUnit.MICROSECONDS)
  51. @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
  52. @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS)
  53. public Path moveFileToExistingDir() throws IOException {
  54. i++;
  55. Path tmp = testDir.resolve("tmp" + i++);
  56. Files.createFile(tmp);
  57. Path targetDirectory = targetDir;
  58. Path targetFile = targetDirectory.resolve("tmp" + i);
  59. try {
  60. return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE);
  61. } catch (NoSuchFileException e) {
  62. Files.createDirectory(targetDirectory);
  63. return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE);
  64. }
  65. }
  66. @Benchmark
  67. @BenchmarkMode({ Mode.AverageTime })
  68. @OutputTimeUnit(TimeUnit.MICROSECONDS)
  69. @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
  70. @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS)
  71. public Path moveFileToExistingDirExists() throws IOException {
  72. Path tmp = testDir.resolve("tmp" + i++);
  73. Files.createFile(tmp);
  74. Path targetDirectory = targetDir;
  75. Path targetFile = targetDir.resolve("tmp" + i);
  76. if (!targetDirectory.toFile().exists()) {
  77. Files.createDirectory(targetDirectory);
  78. }
  79. return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE);
  80. }
  81. @Benchmark
  82. @BenchmarkMode({ Mode.AverageTime })
  83. @OutputTimeUnit(TimeUnit.MICROSECONDS)
  84. @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
  85. @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS)
  86. public Path moveFileToMissingDir() throws IOException {
  87. i++;
  88. Path tmp = testDir.resolve("tmp" + i);
  89. Files.createFile(tmp);
  90. Path targetDirectory = testDir.resolve("target" + i);
  91. Path targetFile = targetDirectory.resolve("tmp" + i);
  92. try {
  93. return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE);
  94. } catch (NoSuchFileException e) {
  95. Files.createDirectory(targetDirectory);
  96. return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE);
  97. }
  98. }
  99. @Benchmark
  100. @BenchmarkMode({ Mode.AverageTime })
  101. @OutputTimeUnit(TimeUnit.MICROSECONDS)
  102. @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
  103. @Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS)
  104. public Path moveFileToMissingDirExists() throws IOException {
  105. i++;
  106. Path tmp = testDir.resolve("tmp" + i);
  107. Files.createFile(tmp);
  108. Path targetDirectory = testDir.resolve("target" + i);
  109. Path targetFile = targetDirectory.resolve("tmp" + i);
  110. if (!targetDirectory.toFile().exists()) {
  111. Files.createDirectory(targetDirectory);
  112. }
  113. return Files.move(tmp, targetFile, StandardCopyOption.ATOMIC_MOVE);
  114. }
  115. public static void main(String[] args) throws RunnerException {
  116. Options opt = new OptionsBuilder()
  117. .include(FileMoveBenchmark.class
  118. .getSimpleName())
  119. // .addProfiler(StackProfiler.class)
  120. // .addProfiler(GCProfiler.class)
  121. .forks(1).jvmArgs("-ea").build();
  122. new Runner(opt).run();
  123. }
  124. }