Skip to content

Commit

Permalink
Merge pull request #110 from mmk-1/optimize-remove-affectedtests
Browse files Browse the repository at this point in the history
Optimize method and hybrid starts by removing unecessary affected test classes computation
  • Loading branch information
owolabileg authored Nov 7, 2023
2 parents 46cb80a + e6737a5 commit b563ddf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public class HybridMojo extends DiffMojo {
@Parameter(property = "debug", defaultValue = TRUE)
private boolean debug;

/**
* Set this to "true" to compute affected test classes as well.
*/
@Parameter(property = "computeAffectedTests", defaultValue = FALSE)
private boolean computeAffectedTests;

public void setComputeImpactedMethods(boolean computeImpactedMethods) {
this.computeImpactedMethods = computeImpactedMethods;
}
Expand All @@ -71,6 +77,10 @@ public void setUpdateMethodsChecksums(boolean updateChecksums) {
this.updateMethodsChecksums = updateChecksums;
}

public void setComputeAffectedTests(boolean computeAffectedTests) {
this.computeAffectedTests = computeAffectedTests;
}

public Set<String> getAffectedMethods() {
Set<String> affectedMethods = new HashSet<>();
affectedMethods.addAll(changedMethods);
Expand Down Expand Up @@ -160,8 +170,11 @@ public void execute() throws MojoExecutionException {
// Build method level static dependencies
try {
MethodLevelStaticDepsBuilder.buildMethodsGraph(includeVariables);
methodToTestClasses = MethodLevelStaticDepsBuilder.computeMethodToTestClasses();

classesChecksum = MethodLevelStaticDepsBuilder.computeClassesChecksums(loader, cleanBytes);
if (computeAffectedTests) {
methodToTestClasses = MethodLevelStaticDepsBuilder.computeMethodToTestClasses();
}
} catch (Exception exception) {
throw new RuntimeException(exception);
}
Expand Down Expand Up @@ -196,14 +209,17 @@ protected void runHybrid(boolean impacted) throws MojoExecutionException {
methodsCheckSum = MethodLevelStaticDepsBuilder.getMethodsCheckSum();
changedMethods = new HashSet<>();
newMethods = MethodLevelStaticDepsBuilder.computeMethods();
affectedTestClasses = MethodLevelStaticDepsBuilder.computeTestClasses();
newClasses = MethodLevelStaticDepsBuilder.getClasses();
oldClasses = new HashSet<>();
deletedClasses = new HashSet<>();
changedClassesWithChangedHeaders = new HashSet<>();
changedClassesWithoutChangedHeaders = new HashSet<>();
nonAffectedMethods = new HashSet<>();

if (computeAffectedTests) {
affectedTestClasses = MethodLevelStaticDepsBuilder.computeTestClasses();
}

if (impacted) {
impactedMethods = newMethods;
impactedClasses = newClasses;
Expand All @@ -226,9 +242,11 @@ protected void runHybrid(boolean impacted) throws MojoExecutionException {

classDependencyGraph = MethodLevelStaticDepsBuilder.constructClassesDependencyGraph();
MethodLevelStaticDepsBuilder.constuctTestClassesToClassesGraph();
classToTestClassGraph = MethodLevelStaticDepsBuilder.constructClassesToTestClassesGraph();
methodToTestClasses = MethodLevelStaticDepsBuilder.computeMethodToTestClasses();
affectedTestClasses = new HashSet<>();
if (computeAffectedTests) {
classToTestClassGraph = MethodLevelStaticDepsBuilder.constructClassesToTestClassesGraph();
methodToTestClasses = MethodLevelStaticDepsBuilder.computeMethodToTestClasses();
affectedTestClasses = new HashSet<>();
}

setChangedAndNonaffectedMethods();

Expand Down Expand Up @@ -276,7 +294,9 @@ private void logInfo(boolean impacted) {
logger.log(Level.INFO, "DeletedClasses: " + deletedClasses.size());
logger.log(Level.INFO, "ChangedClassesWithChangedHeaders: " + changedClassesWithChangedHeaders.size());
logger.log(Level.INFO, "ChangedClassesWithoutChangedHeaders: " + changedClassesWithoutChangedHeaders.size());
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses.size());
if (computeAffectedTests) {
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses.size());
}

if (impacted) {
logger.log(Level.INFO, "ImpactedClasses: " + impactedClasses.size());
Expand All @@ -286,11 +306,13 @@ private void logInfo(boolean impacted) {
if (debug) {
logger.log(Level.INFO, "ImpactedMethods: " + impactedMethods);
logger.log(Level.INFO, "ImpactedClasses: " + impactedClasses);
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses);
logger.log(Level.INFO, "ClassDependencyGraph: " + classDependencyGraph);
logger.log(Level.INFO, "ClassToTestClassGraph: " + classToTestClassGraph);
logger.log(Level.INFO, "ChangedClassesWithChangedHeaders: " + changedClassesWithChangedHeaders);
logger.log(Level.INFO, "ChangedClassesWithoutChangedHeaders: " + changedClassesWithoutChangedHeaders);
if (computeAffectedTests) {
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses);
logger.log(Level.INFO, "ClassToTestClassGraph: " + classToTestClassGraph);
}
}
}

Expand Down Expand Up @@ -320,23 +342,24 @@ protected void setChangedAndNonaffectedMethods() throws MojoExecutionException {
changedMethods = methodsData == null ? new HashSet<String>() : methodsData.get(0);
newMethods = methodsData == null ? new HashSet<String>() : methodsData.get(1);

for (String newMethod : newMethods) {
affectedTestClasses.addAll(methodToTestClasses.getOrDefault(newMethod, new HashSet<>()));
}
if (computeAffectedTests) {
for (String newMethod : newMethods) {
affectedTestClasses.addAll(methodToTestClasses.getOrDefault(newMethod, new HashSet<>()));
}

for (String changedMethod : changedMethods) {
affectedTestClasses.addAll(methodToTestClasses.getOrDefault(changedMethod, new HashSet<>()));
}
for (String changedMethod : changedMethods) {
affectedTestClasses.addAll(methodToTestClasses.getOrDefault(changedMethod, new HashSet<>()));
}

for (String addedClass : newClasses) {
affectedTestClasses.addAll(classToTestClassGraph.getOrDefault(addedClass, new HashSet<>()));
}
for (String addedClass : newClasses) {
affectedTestClasses.addAll(classToTestClassGraph.getOrDefault(addedClass, new HashSet<>()));
}

for (String changedClassesWithChangedHeader : changedClassesWithChangedHeaders) {
affectedTestClasses
.addAll(classToTestClassGraph.getOrDefault(changedClassesWithChangedHeader, new HashSet<>()));
for (String changedClassesWithChangedHeader : changedClassesWithChangedHeaders) {
affectedTestClasses
.addAll(classToTestClassGraph.getOrDefault(changedClassesWithChangedHeader, new HashSet<>()));
}
}

}

/**
Expand All @@ -348,8 +371,10 @@ private void computeImpactedMethods() {
impactedMethods = new HashSet<>();
impactedMethods.addAll(findImpactedMethods(changedMethods));
impactedMethods.addAll(findImpactedMethods(newMethods));
for (String impactedMethod : impactedMethods) {
affectedTestClasses.addAll(methodToTestClasses.getOrDefault(impactedMethod, new HashSet<String>()));
if (computeAffectedTests) {
for (String impactedMethod : impactedMethods) {
affectedTestClasses.addAll(methodToTestClasses.getOrDefault(impactedMethod, new HashSet<String>()));
}
}
}

Expand All @@ -363,8 +388,10 @@ private void computeImpactedClasses() {
impactedClasses = new HashSet<>();
impactedClasses.addAll(findImpactedClasses(newClasses));
impactedClasses.addAll(findImpactedClasses(changedClassesWithChangedHeaders));
for (String impactedClass : impactedClasses) {
affectedTestClasses.addAll(classToTestClassGraph.getOrDefault(impactedClass, new HashSet<String>()));
if (computeAffectedTests) {
for (String impactedClass : impactedClasses) {
affectedTestClasses.addAll(classToTestClassGraph.getOrDefault(impactedClass, new HashSet<String>()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public class MethodsMojo extends DiffMojo {
@Parameter(property = "debug", defaultValue = FALSE)
private boolean debug;

/**
* Set this to "true" to compute affected test classes as well.
*/
@Parameter(property = "computeAffectedTests", defaultValue = FALSE)
private boolean computeAffectedTests;

public void setDebug(boolean debug) {
this.debug = debug;
}
Expand All @@ -88,6 +94,10 @@ public void setIncludeVariables(boolean includeVariables) {
this.includeVariables = includeVariables;
}

public void setComputeAffectedTests(boolean computeAffectedTests) {
this.computeAffectedTests = computeAffectedTests;
}

public Set<String> getAffectedMethods() {
Set<String> affectedMethods = new HashSet<>();
affectedMethods.addAll(changedMethods);
Expand Down Expand Up @@ -173,12 +183,15 @@ protected void runMethods(boolean impacted) throws MojoExecutionException {
if (!Files.exists(Paths.get(getArtifactsDir() + METHODS_CHECKSUMS_SERIALIZED_FILE))) {
changedMethods = new HashSet<>();
newMethods = MethodLevelStaticDepsBuilder.computeMethods();
affectedTestClasses = MethodLevelStaticDepsBuilder.computeTestClasses();
oldClasses = new HashSet<>();
changedClasses = new HashSet<>();
newClasses = MethodLevelStaticDepsBuilder.getClasses();
nonAffectedMethods = new HashSet<>();

if (computeAffectedTests) {
affectedTestClasses = MethodLevelStaticDepsBuilder.computeTestClasses();
}

if (impacted) {
impactedMethods = newMethods;
}
Expand All @@ -197,6 +210,9 @@ protected void runMethods(boolean impacted) throws MojoExecutionException {

if (impacted) {
computeImpactedMethods();

}
if (computeAffectedTests) {
computeAffectedTestClasses();
}

Expand Down Expand Up @@ -232,13 +248,18 @@ private void logInfoStatements(boolean impacted) {
logger.log(Level.INFO, "NewClasses: " + newClasses.size());
logger.log(Level.INFO, "OldClasses: " + oldClasses.size());
logger.log(Level.INFO, "ChangedClasses: " + changedClasses.size());
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses.size());

if (computeAffectedTests) {
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses.size());
}

// DEBUG PRINTS
if (debug) {
logger.log(Level.INFO, "ChangedMethods: " + changedMethods);
logger.log(Level.INFO, "ImpactedMethods: " + impactedMethods);
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses);
if (computeAffectedTests) {
logger.log(Level.INFO, "AffectedTestClasses: " + affectedTestClasses);
}
}
}

Expand Down

0 comments on commit b563ddf

Please sign in to comment.