Skip to content

Commit

Permalink
Removed duplicated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
kausandr committed May 21, 2021
1 parent 9732e74 commit f72f18f
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ public static File createFile(String name) {
public static File generateBinaryFileForTests() throws Exception {
createRandomBinaryFileIfNotExists(FILE_NAME_AS_SRC, fileSizeMultiplierPerChunkSize,
randomFileChunkSize * fileSizeMultiplierPerChunkSize);
File f = new File(FILE_NAME_AS_SRC);
return f;
return new File(FILE_NAME_AS_SRC);
}

public static File generateSmallBinaryFileForTests() throws Exception {
createRandomBinaryFileIfNotExists(FILE_NAME_AS_SRC_FOR_SMALL_CASES, fileSizeSmallMultiplierPerChunkSize,
randomFileChunkSize * fileSizeSmallMultiplierPerChunkSize);
File f = new File(FILE_NAME_AS_SRC_FOR_SMALL_CASES);
return f;
return new File(FILE_NAME_AS_SRC_FOR_SMALL_CASES);
}

/*
Expand Down Expand Up @@ -95,53 +93,29 @@ public static void removeFile(File file) {

public static long copyFileUsingFileStreams(File srcFile, File targetFile, int bufferSize, boolean isSyncWrite)
throws IOException {
long bytesCopied = 0L;
byte[] buffer = new byte[bufferSize];

try (InputStream in = new FileInputStream(srcFile)) {
try (OutputStream out = new FileOutputStream(targetFile)) {
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
if (isSyncWrite) {
out.flush();
}
bytesCopied += bytesRead;
}
}
}

return bytesCopied;
return copyFileUsingStreams(new FileInputStream(srcFile), new FileOutputStream(targetFile), bufferSize,
isSyncWrite);
}

public static long copyFileUsingBufferedStreams(File srcFile, File targetFile, int bufferSize, boolean isSyncWrite)
throws IOException {
long bytesCopied = 0L;
byte[] buffer = new byte[bufferSize];

try (InputStream in = new BufferedInputStream(new FileInputStream(srcFile))) {
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile))) {
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
if (isSyncWrite) {
out.flush();
}
bytesCopied += bytesRead;
}
}
}

return bytesCopied;
return copyFileUsingStreams(new BufferedInputStream(new FileInputStream(srcFile)),
new BufferedOutputStream(new FileOutputStream(targetFile)), bufferSize, isSyncWrite);
}

public static long copyFileUsingDirectBufferedStreams(File srcFile, File targetFile, int bufferSize,
boolean isSyncWrite) throws IOException {
return copyFileUsingStreams(new BufferedInputStream(new FileInputStream(srcFile), bufferSize * 2),
new BufferedOutputStream(new FileOutputStream(targetFile), bufferSize * 2), bufferSize, isSyncWrite);
}

private static long copyFileUsingStreams(InputStream inStr, OutputStream outStr, int bufferSize,
boolean isSyncWrite) throws IOException {
long bytesCopied = 0L;
byte[] buffer = new byte[bufferSize];

try (InputStream in = new BufferedInputStream(new FileInputStream(srcFile), bufferSize * 2)) {
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile), bufferSize * 2)) {
try (InputStream in = inStr) {
try (OutputStream out = outStr) {
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
Expand Down Expand Up @@ -182,7 +156,7 @@ public static void storeResultsToFile(String fileName, String content) {
boolean exists = pFile.exists();
if (!exists) {
if (!pFile.mkdir()) {
throw new IOException("Coluld not create folder=" + pFile);
throw new IOException("Could not create folder=" + pFile);
}
}
try (Writer file = new FileWriter(fileName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStream;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -44,6 +45,7 @@
import com.gocypher.cybench.core.annotation.CyBenchMetadataList;
import com.gocypher.cybench.core.utils.IOUtils;
import com.gocypher.cybench.core.utils.JMHUtils;
import com.gocypher.cybench.core.utils.JSONUtils;
import com.gocypher.cybench.core.utils.SecurityUtils;
import com.gocypher.cybench.launcher.environment.model.HardwareProperties;
import com.gocypher.cybench.launcher.environment.model.JVMProperties;
Expand All @@ -55,7 +57,6 @@
import com.gocypher.cybench.launcher.services.ConfigurationHandler;
import com.gocypher.cybench.launcher.utils.ComputationUtils;
import com.gocypher.cybench.launcher.utils.Constants;
import com.gocypher.cybench.launcher.utils.JSONUtils;
import com.gocypher.cybench.launcher.utils.SecurityBuilder;

public class BenchmarkRunner {
Expand Down Expand Up @@ -231,7 +232,7 @@ public static void main(String[] args) throws Exception {
Class<?> aClass = Class.forName(clazz);
Optional<Method> benchmarkMethod = JMHUtils.getBenchmarkMethod(method, aClass);
appendMetadataFromClass(aClass, benchmarkReport);
appendMetadataFromMethod(benchmarkMethod, benchmarkReport);
appendMetadataFromAnnotated(benchmarkMethod, benchmarkReport);
appendMetadataFromJavaDoc(aClass, benchmarkMethod, benchmarkReport);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Expand Down Expand Up @@ -366,42 +367,26 @@ protected static void appendMetadataFromClass(Class<?> aClass, BenchmarkReport b
}
for (Class<?> anInterface : aClass.getInterfaces()) {
appendMetadataFromClass(anInterface, benchmarkReport);

}

CyBenchMetadataList annotation = aClass.getDeclaredAnnotation(CyBenchMetadataList.class);

if (annotation != null) {
Arrays.stream(annotation.value()).forEach(annot -> {
checkSetOldMetadataProps(annot.key(), annot.value(), benchmarkReport);
benchmarkReport.addMetadata(annot.key(), annot.value());
// LOG.info("added metadata " + annot.key() + "=" + annot.value());
});
}
BenchmarkMetaData singleAnnotation = aClass.getDeclaredAnnotation(BenchmarkMetaData.class);
if (singleAnnotation != null) {
checkSetOldMetadataProps(singleAnnotation.key(), singleAnnotation.value(), benchmarkReport);
benchmarkReport.addMetadata(singleAnnotation.key(), singleAnnotation.value());
// LOG.info("added metadata " + singleAnnotation.key() + "=" + singleAnnotation.value());
}

appendMetadataFromAnnotated(Optional.of(aClass), benchmarkReport);
}

protected static void appendMetadataFromMethod(Optional<Method> benchmarkMethod, BenchmarkReport benchmarkReport) {
CyBenchMetadataList annotation = benchmarkMethod.get().getDeclaredAnnotation(CyBenchMetadataList.class);
protected static void appendMetadataFromAnnotated(Optional<? extends AnnotatedElement> annotated,
BenchmarkReport benchmarkReport) {
CyBenchMetadataList annotation = annotated.get().getDeclaredAnnotation(CyBenchMetadataList.class);
if (annotation != null) {
Arrays.stream(annotation.value()).forEach(annot -> {
checkSetOldMetadataProps(annot.key(), annot.value(), benchmarkReport);
benchmarkReport.addMetadata(annot.key(), annot.value());
// LOG.info("added metadata " + annot.key() + "=" + annot.value());
});
}
BenchmarkMetaData singleAnnotation = benchmarkMethod.get().getDeclaredAnnotation(BenchmarkMetaData.class);
BenchmarkMetaData singleAnnotation = annotated.get().getDeclaredAnnotation(BenchmarkMetaData.class);
if (singleAnnotation != null) {
checkSetOldMetadataProps(singleAnnotation.key(), singleAnnotation.value(), benchmarkReport);
benchmarkReport.addMetadata(singleAnnotation.key(), singleAnnotation.value());
// LOG.info("added metadata " + singleAnnotation.key() + "=" + singleAnnotation.value());

}

}
Expand Down
Loading

0 comments on commit f72f18f

Please sign in to comment.