JAVA-12080 Create and delete test files dynamically

This commit is contained in:
anuragkumawat 2022-07-03 20:44:56 +05:30
parent bf6c478bb5
commit 2ff0071a65
1501 changed files with 34 additions and 1508 deletions

View File

@ -1,5 +1,6 @@
package com.baeldung.streams.parallel;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@ -7,18 +8,43 @@ import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
@State(Scope.Benchmark)
public class FileSearchCost {
private final static String FILE_NAME = "src/main/resources/Test";
@Setup(Level.Trial)
public void setup() throws IOException {
for (int i = 0; i < 1500; i++) {
File targetFile = new File(FILE_NAME + i);
FileUtils.writeStringToFile(targetFile, "Test", "UTF8");
}
}
@TearDown(Level.Trial)
public void tearDown() {
for (int i = 0; i < 1500; i++) {
File fileToDelete = new File(FILE_NAME + i);
fileToDelete.delete();
}
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void textFileSearchSequential() throws IOException {
Files.walk(Paths.get("src/main/resources/filesearch/")).map(Path::normalize).filter(Files::isRegularFile)
Files.walk(Paths.get("src/main/resources/")).map(Path::normalize).filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".txt")).collect(Collectors.toList());
}
@ -26,7 +52,7 @@ public class FileSearchCost {
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void textFileSearchParallel() throws IOException {
Files.walk(Paths.get("src/main/resources/filesearch/")).parallel().map(Path::normalize).filter(Files::isRegularFile)
Files.walk(Paths.get("src/main/resources/")).parallel().map(Path::normalize).filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().endsWith(".txt")).collect(Collectors.toList());
}

Some files were not shown because too many files have changed in this diff Show More