Skip to content

Commit

Permalink
Add some extra tests for dependency mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
asodja committed Nov 13, 2021
1 parent 03eb0e8 commit b0baab1
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 14 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,12 @@ They can be added to a scenario file like this:

apply-build-script-change-to = "build.gradle.kts"
apply-dependency-change-to {
files = ['build.gradle']
files = ["build.gradle"]
// Example: This will apply a unique combination of 5 projects from a set of 10 projects.
// Number of combinations can be calculated by typing "10 choosen 5" in your favorite search engine.
// Default values are 10 and 5. When values are equal there is just one combination to apply, so all
// runs will have same dependencies applied.
// When both values are equal there is just one combination possible, so all
// runs will have the same combination of the projects applied as dependency.
// Default values are 10 and 5.
projects-set-size = 10
applied-projects-size = 5
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/gradle/profiler/ScenarioLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.*;
import java.util.stream.Collectors;

public class ScenarioLoader {
class ScenarioLoader {
private static final String TITLE = "title";
private static final String VERSIONS = "versions";
private static final String TASKS = "tasks";
Expand Down Expand Up @@ -295,7 +295,7 @@ private static int getBuildCount(InvocationSettings settings) {
return getBuildCount(settings, (Integer) null);
}

public static int getBuildCount(InvocationSettings settings, Config scenario) {
private static int getBuildCount(InvocationSettings settings, Config scenario) {
return getBuildCount(settings, ConfigUtil.optionalInteger(scenario, ITERATIONS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void beforeBuild(BuildContext context) {
FileSupport.writeUnchecked(sourceFile.toPath(), modifiedText.toString());
}

protected String readText(File file) {
private String readText(File file) {
return FileSupport.readUnchecked(file.toPath());
}

Expand All @@ -34,7 +34,7 @@ public void afterScenario(ScenarioContext context) {
revert(sourceFile, originalText);
}

protected void revert(File file, String originalText) {
private void revert(File file, String originalText) {
FileSupport.writeUnchecked(file.toPath(), originalText);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ private ProjectCombinationsSupport() {
}

public static ProjectCombinations createProjectCombinations(int projectsToGenerate, int appliedProjectDependencies) {
checkArgument(projectsToGenerate >= appliedProjectDependencies, "Projects to generate should be at least equal to applied project dependencies ");
checkArgument(projectsToGenerate > 0 && appliedProjectDependencies > 0, "Projects-set-size and applied-project-size should be greater than 0.");
checkArgument(projectsToGenerate >= appliedProjectDependencies, "Projects-set-size should be at least equal to applied-project-size.");
String salt = UUID.randomUUID().toString().substring(0, 5);
List<String> projectNames = IntStream.range(0, projectsToGenerate)
.mapToObj(it -> String.format("project-%s-%s", salt, it))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.gradle.profiler.mutations


import org.gradle.profiler.DefaultScenarioContext
import org.gradle.profiler.Phase
import org.gradle.profiler.ScenarioDefinition
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

abstract class AbstractMutatorTest extends Specification {
@Rule TemporaryFolder tmpDir = new TemporaryFolder()
def scenarioDefinition = Mock(ScenarioDefinition) {
getName() >> "testScenario"
}
def scenarioContext = new DefaultScenarioContext(UUID.fromString("276d92f3-16ac-4064-9a18-5f1dfd67992f"), scenarioDefinition)
def scenarioContext = new DefaultScenarioContext(UUID.fromString("276d92f3-16ac-4064-9a18-5f1dfd67992f"), "testScenario")
def buildContext = scenarioContext.withBuild(Phase.MEASURE, 7)

protected static File file(File parent, String... paths) {
Expand All @@ -21,5 +18,4 @@ abstract class AbstractMutatorTest extends Specification {
}
return new File(parent, paths.join(File.separator))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.gradle.profiler.mutations

import org.gradle.profiler.AbstractProfilerIntegrationTest
import org.gradle.profiler.Main

class ApplyDependencyChangeMutatorIntegrationTest extends AbstractProfilerIntegrationTest {

def "successfully benchmarks gradle projects with dependency mutations with default values"() {
given:
instrumentedBuildScript()
println buildFile
def scenarioFile = file("performance.scenarios") << """
s1 {
tasks = assemble
apply-dependency-change-to {
files = ["build.gradle"]
}
}
"""

when:
new Main().run("--project-dir", projectDir.absolutePath, "--output-dir", outputDir.absolutePath, "--gradle-version", latestSupportedGradleVersion, "--scenario-file", scenarioFile.absolutePath, "--benchmark", "s1")

then:
resultFile.containsWarmDaemonScenario(latestSupportedGradleVersion, "s1", ["assemble"])
}

def "successfully benchmarks gradle projects with dependency mutations"() {
given:
instrumentedBuildScript()
println buildFile
def scenarioFile = file("performance.scenarios") << """
s1 {
tasks = assemble
apply-dependency-change-to {
files = ["build.gradle"]
projects-set-size = 2
applied-projects-size = 1
}
}
"""

when:
new Main().run("--project-dir", projectDir.absolutePath, "--output-dir", outputDir.absolutePath, "--gradle-version", latestSupportedGradleVersion, "--scenario-file", scenarioFile.absolutePath, "--benchmark", "s1")

then:
resultFile.containsWarmDaemonScenario(latestSupportedGradleVersion, "s1", ["assemble"])
}

def "fails benchmarks gradle projects if projects set size is less than applied projects size"() {
given:
instrumentedBuildScript()
println buildFile
def scenarioFile = file("performance.scenarios") << """
s1 {
tasks = assemble
apply-dependency-change-to {
files = ["build.gradle"]
projects-set-size = 2
applied-projects-size = 3
}
}
"""

when:
new Main().run("--project-dir", projectDir.absolutePath, "--output-dir", outputDir.absolutePath, "--gradle-version", latestSupportedGradleVersion, "--scenario-file", scenarioFile.absolutePath, "--benchmark", "s1")

then:
def e = thrown(IllegalArgumentException)
e.message == "Projects-set-size should be at least equal to applied-project-size."
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gradle.profiler.mutations.support

import spock.lang.Specification

class ProjectCombinationsSupportTest extends Specification {

def "creates combinations for given number of projects"() {
when:
def combinations = ProjectCombinationsSupport.createProjectCombinations(3, 2)

then:
def salt = combinations.getSalt()
combinations.nextCombination == ["project-$salt-0", "project-$salt-1"] as Set<String>
combinations.nextCombination == ["project-$salt-0", "project-$salt-2"] as Set<String>
combinations.nextCombination == ["project-$salt-1", "project-$salt-2"] as Set<String>
// Combinations repeats circular
combinations.nextCombination == ["project-$salt-0", "project-$salt-1"] as Set<String>
combinations.nextCombination == ["project-$salt-0", "project-$salt-2"] as Set<String>
combinations.nextCombination == ["project-$salt-1", "project-$salt-2"] as Set<String>
}

}

0 comments on commit b0baab1

Please sign in to comment.