Skip to content

Commit

Permalink
Replace normal file usage with memory-mapped file (#116)
Browse files Browse the repository at this point in the history
* Replace normal file usage with memory-mapped file

Signed-off-by: André Silva <[email protected]>

* Refactor load method

Signed-off-by: André Silva <[email protected]>
  • Loading branch information
andre15silva authored Aug 11, 2021
1 parent 2d6d94d commit ee045f4
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 236 deletions.
8 changes: 6 additions & 2 deletions src/main/java/eu/stamp_project/testrunner/EntryPoint.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package eu.stamp_project.testrunner;

import eu.stamp_project.mutationtest.descartes.DescartesMutationEngine;
import eu.stamp_project.testrunner.listener.*;
import eu.stamp_project.testrunner.listener.Coverage;
import eu.stamp_project.testrunner.listener.CoveragePerTestMethod;
import eu.stamp_project.testrunner.listener.CoveredTestResultPerTestMethod;
import eu.stamp_project.testrunner.listener.TestResult;
import eu.stamp_project.testrunner.listener.impl.CoverageImpl;
import eu.stamp_project.testrunner.listener.impl.CoveragePerTestMethodImpl;
import eu.stamp_project.testrunner.listener.impl.CoveredTestResultPerTestMethodImpl;
Expand Down Expand Up @@ -33,7 +36,8 @@
import java.lang.reflect.Method;
import java.net.URL;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
*/
public interface Coverage {

public static final String SERIALIZE_NAME = "Coverage";
public static final String SHARED_MEMORY_FILE = "Coverage.dat";

public static final String OUTPUT_DIR = "target" + ConstantsHelper.FILE_SEPARATOR;

public static final String EXTENSION = ".ser";

public void setExecutionPath(String executionPath);

public int getInstructionsCovered();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
*/
public interface CoveragePerTestMethod extends Serializable {

public static final String SERIALIZE_NAME = "CoveragePerTest";
public static final String SHARED_MEMORY_FILE = "CoveragePerTestMethod.dat";

public static final String OUTPUT_DIR = "target" + ConstantsHelper.FILE_SEPARATOR;

public static final String EXTENSION = ".ser";

public Map<String, Coverage> getCoverageResultsMap();

public Coverage getCoverageOf(String testMethodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
*/
public interface CoveredTestResultPerTestMethod extends TestResult, Serializable {

public static final String SERIALIZE_NAME = "CoveredTestResultPerTest";
public static final String SHARED_MEMORY_FILE = "CoveredTestResultPerTest.dat";

public static final String OUTPUT_DIR = "target" + ConstantsHelper.FILE_SEPARATOR;

public static final String EXTENSION = ".ser";

public Map<String, Coverage> getCoverageResultsMap();

public Coverage getCoverageOf(String testMethodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
*/
public interface TestResult extends Serializable {

public static final String SERIALIZE_NAME = "TestResult";
public static final String SHARED_MEMORY_FILE = "TestResult.dat";

public static final String OUTPUT_DIR = "target" + ConstantsHelper.FILE_SEPARATOR;

public static final String EXTENSION = ".ser";

/**
* Aggregate result of this instance to the given instance
* @param that the other instance of TestResult of which we need to add the values to this instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package eu.stamp_project.testrunner.listener.impl;

import eu.stamp_project.testrunner.listener.Coverage;
import eu.stamp_project.testrunner.listener.utils.ListenerUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import eu.stamp_project.testrunner.listener.Coverage;
import eu.stamp_project.testrunner.listener.TestResult;
import eu.stamp_project.testrunner.runner.Loader;

/**
* created by Benjamin DANGLOT [email protected] on 14/11/18
Expand Down Expand Up @@ -54,33 +52,16 @@ public String toString() {

@Override
public void save() {
File outputDir = new File(TestResult.OUTPUT_DIR);
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
System.err.println("Error while creating output dir");
}
}
File f = new File(outputDir, SERIALIZE_NAME + EXTENSION);
try (FileOutputStream fout = new FileOutputStream(f)) {
try (ObjectOutputStream oos = new ObjectOutputStream(fout)) {
oos.writeObject(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
System.err.println("Error while writing serialized file.");
throw new RuntimeException(e);
}
System.out.println("File saved to the following path: " + f.getAbsolutePath());
ListenerUtils.saveToMemoryMappedFile(new File(OUTPUT_DIR, SHARED_MEMORY_FILE), this);
}

/**
* Load from serialized object
* Loads and deserializes the file from a memory mapped file
*
* @return an Instance of JUnit4Coverage loaded from a serialized file.
* @return loaded CoverageDetailed from the memory mapped file
*/
public static Coverage load() {
return new Loader<Coverage>().load(SERIALIZE_NAME);
return ListenerUtils.loadFromMemoryMappedFile(ListenerUtils.computeTargetFilePath(OUTPUT_DIR, SHARED_MEMORY_FILE));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package eu.stamp_project.testrunner.listener.impl;

import eu.stamp_project.testrunner.listener.Coverage;
import eu.stamp_project.testrunner.listener.TestResult;
import eu.stamp_project.testrunner.runner.Loader;
import org.jacoco.core.analysis.*;
import java.io.*;
import eu.stamp_project.testrunner.listener.utils.ListenerUtils;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.ICounter;
import org.jacoco.core.analysis.ILine;

import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -89,35 +92,22 @@ public String toString() {
return this.instructionsCovered + " / " + this.instructionsTotal;
}

/**
* Writes the serialized object to a memory mapped file.
* The location depends on the workspace set for the test runner process.
*/
@Override
public void save() {
File outputDir = new File(TestResult.OUTPUT_DIR);
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
System.err.println("Error while creating output dir");
}
}
File f = new File(outputDir, SERIALIZE_NAME + EXTENSION);
try (FileOutputStream fout = new FileOutputStream(f)) {
try (ObjectOutputStream oos = new ObjectOutputStream(fout)) {
oos.writeObject(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
System.err.println("Error while writing serialized file.");
throw new RuntimeException(e);
}
System.out.println("File saved to the following path: " + f.getAbsolutePath());
ListenerUtils.saveToMemoryMappedFile(new File(OUTPUT_DIR, SHARED_MEMORY_FILE), this);
}

/**
* Load from serialized object
* Loads and deserializes the file from a memory mapped file
*
* @return an Instance of JUnit4Coverage loaded from a serialized file.
* @return loaded Coverage from the memory mapped file
*/
public static Coverage load() {
return new Loader<Coverage>().load(SERIALIZE_NAME);
return ListenerUtils.loadFromMemoryMappedFile(ListenerUtils.computeTargetFilePath(OUTPUT_DIR, SHARED_MEMORY_FILE));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import eu.stamp_project.testrunner.listener.Coverage;
import eu.stamp_project.testrunner.listener.CoveragePerTestMethod;
import eu.stamp_project.testrunner.listener.CoverageTransformer;
import eu.stamp_project.testrunner.listener.TestResult;
import eu.stamp_project.testrunner.runner.Loader;
import eu.stamp_project.testrunner.listener.utils.ListenerUtils;
import eu.stamp_project.testrunner.utils.ConstantsHelper;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.data.SessionInfoStore;
import org.jacoco.core.runtime.RuntimeData;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -109,35 +106,22 @@ public Coverage getCoverageOf(String testMethodName) {
return this.getCoverageResultsMap().get(testMethodName);
}

/**
* Writes the serialized object to a memory mapped file.
* The location depends on the workspace set for the test runner process.
*/
@Override
public void save() {
File outputDir = new File(TestResult.OUTPUT_DIR);
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
System.err.println("Error while creating output dir");
}
}
File f = new File(outputDir, SERIALIZE_NAME + EXTENSION);
try (FileOutputStream fout = new FileOutputStream(f)) {
try (ObjectOutputStream oos = new ObjectOutputStream(fout)) {
oos.writeObject(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
System.err.println("Error while writing serialized file.");
throw new RuntimeException(e);
}
System.out.println("File saved to the following path: " + f.getAbsolutePath());
ListenerUtils.saveToMemoryMappedFile(new File(OUTPUT_DIR, SHARED_MEMORY_FILE), this);
}

/**
* Load from serialized object
* Loads and deserializes the file from a memory mapped file
*
* @return an Instance of CoveragePerTestMethod loaded from a serialized file.
* @return loaded CoveragePerTestMethodImpl from the memory mapped file
*/
public static CoveragePerTestMethodImpl load() {
return new Loader<CoveragePerTestMethodImpl>().load(SERIALIZE_NAME);
return ListenerUtils.loadFromMemoryMappedFile(ListenerUtils.computeTargetFilePath(OUTPUT_DIR, SHARED_MEMORY_FILE));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
import eu.stamp_project.testrunner.listener.CoverageTransformer;
import eu.stamp_project.testrunner.listener.CoveredTestResultPerTestMethod;
import eu.stamp_project.testrunner.listener.TestResult;
import eu.stamp_project.testrunner.listener.utils.ListenerUtils;
import eu.stamp_project.testrunner.runner.Failure;
import eu.stamp_project.testrunner.runner.Loader;
import eu.stamp_project.testrunner.utils.ConstantsHelper;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.data.SessionInfoStore;
import org.jacoco.core.runtime.RuntimeData;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -165,35 +163,22 @@ public Failure getFailureOf(String testMethodName) {
.orElseThrow(() -> new IllegalArgumentException(String.format("Could not find %s in failing test", testMethodName)));
}

/**
* Writes the serialized object to a memory mapped file.
* The location depends on the workspace set for the test runner process.
*/
@Override
public void save() {
File outputDir = new File(TestResult.OUTPUT_DIR);
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
System.err.println("Error while creating output dir");
}
}
File f = new File(outputDir, SERIALIZE_NAME + EXTENSION);
try (FileOutputStream fout = new FileOutputStream(f)) {
try (ObjectOutputStream oos = new ObjectOutputStream(fout)) {
oos.writeObject(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
System.err.println("Error while writing serialized file.");
throw new RuntimeException(e);
}
System.out.println("File saved to the following path: " + f.getAbsolutePath());
ListenerUtils.saveToMemoryMappedFile(new File(OUTPUT_DIR, SHARED_MEMORY_FILE), this);
}

/**
* Load from serialized object
* Loads and deserializes the file from a memory mapped file
*
* @return an Instance of CoveragePerTestMethod loaded from a serialized file.
* @return loaded CoveredTestResultPerTestMethodImpl from the memory mapped file
*/
public static CoveredTestResultPerTestMethodImpl load() {
return new Loader<CoveredTestResultPerTestMethodImpl>().load(SERIALIZE_NAME);
return ListenerUtils.loadFromMemoryMappedFile(ListenerUtils.computeTargetFilePath(OUTPUT_DIR, SHARED_MEMORY_FILE));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package eu.stamp_project.testrunner.listener.impl;

import eu.stamp_project.testrunner.listener.TestResult;
import eu.stamp_project.testrunner.listener.utils.ListenerUtils;
import eu.stamp_project.testrunner.runner.Failure;
import eu.stamp_project.testrunner.runner.Loader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -83,30 +79,22 @@ public Failure getFailureOf(String testMethodName) {
.orElseThrow(() -> new IllegalArgumentException(String.format("Could not find %s in failing test", testMethodName)));
}

/**
* Writes the serialized object to a memory mapped file.
* The location depends on the workspace set for the test runner process.
*/
@Override
public synchronized void save() {
File outputDir = new File(OUTPUT_DIR);
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
System.err.println("Error while creating output dir");
}
}
File f = new File(outputDir, SERIALIZE_NAME + EXTENSION);
try (FileOutputStream fout = new FileOutputStream(f)) {
try (ObjectOutputStream oos = new ObjectOutputStream(fout)) {
oos.writeObject(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
System.err.println("Error while writing serialized file.");
throw new RuntimeException(e);
}
System.out.println("File saved to the following path: " + f.getAbsolutePath());
ListenerUtils.saveToMemoryMappedFile(new File(OUTPUT_DIR, SHARED_MEMORY_FILE), this);
}

/**
* Loads and deserializes the file from a memory mapped file
*
* @return loaded TestResult from the memory mapped file
*/
public static TestResult load() {
return new Loader<TestResult>().load(SERIALIZE_NAME);
return ListenerUtils.loadFromMemoryMappedFile(ListenerUtils.computeTargetFilePath(OUTPUT_DIR, SHARED_MEMORY_FILE));
}

public String toString() {
Expand Down
Loading

0 comments on commit ee045f4

Please sign in to comment.