Skip to content

Commit

Permalink
Refactor ScenarioLoader
Browse files Browse the repository at this point in the history
- extract large method into separate methods for the different build scenarios

Signed-off-by: Alex Beggs <[email protected]>
  • Loading branch information
AlexBeggs committed Feb 8, 2021
1 parent 2269d0a commit db2b173
Showing 1 changed file with 129 additions and 59 deletions.
188 changes: 129 additions & 59 deletions src/main/java/org/gradle/profiler/ScenarioLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigParseOptions;
import java.util.Collections;
import org.gradle.profiler.mutations.ApplyAbiChangeToSourceFileMutator;
import org.gradle.profiler.mutations.ApplyChangeToAndroidLayoutFileMutator;
import org.gradle.profiler.mutations.ApplyChangeToAndroidManifestFileMutator;
Expand Down Expand Up @@ -220,72 +221,19 @@ static List<ScenarioDefinition> loadScenarios(File scenarioFile, InvocationSetti
int buildCount = getBuildCount(settings, scenario);
File scenarioBaseDir = new File(settings.getOutputDir(), GradleScenarioDefinition.safeFileName(scenarioName));

if (scenario.hasPath(BAZEL) && settings.isBazel()) {
Config executionInstructions = getConfig(scenarioFile, settings, scenarioName, scenario, BAZEL, BAZEL_KEYS);

List<String> targets = ConfigUtil.strings(executionInstructions, TARGETS);
File bazelHome = getToolHome(executionInstructions);
File outputDir = new File(scenarioBaseDir, "bazel");
int warmUpCount = getWarmUpCount(settings, scenario);
definitions.add(new BazelScenarioDefinition(scenarioName, title, targets, mutators, warmUpCount, buildCount, outputDir, bazelHome));
} else if (scenario.hasPath(BUCK) && settings.isBuck()) {
Config executionInstructions = getConfig(scenarioFile, settings, scenarioName, scenario, BUCK, BUCK_KEYS);
List<String> targets = ConfigUtil.strings(executionInstructions, TARGETS);
String type = ConfigUtil.string(executionInstructions, TYPE, null);
File buckHome = getToolHome(executionInstructions);
File outputDir = new File(scenarioBaseDir, "buck");
int warmUpCount = getWarmUpCount(settings, scenario);
definitions.add(new BuckScenarioDefinition(scenarioName, title, targets, type, mutators, warmUpCount, buildCount, outputDir, buckHome));
} else if (scenario.hasPath(MAVEN) && settings.isMaven()) {
Config executionInstructions = getConfig(scenarioFile, settings, scenarioName, scenario, MAVEN, MAVEN_KEYS);
List<String> targets = ConfigUtil.strings(executionInstructions, TARGETS);
File mavenHome = getToolHome(executionInstructions);
File outputDir = new File(scenarioBaseDir, "maven");
int warmUpCount = getWarmUpCount(settings, scenario);
definitions.add(new MavenScenarioDefinition(scenarioName, title, targets, mutators, warmUpCount, buildCount, outputDir, mavenHome));
} else if (!settings.isBazel() && !settings.isBuck() && !settings.isMaven()) {
List<GradleBuildConfiguration> versions = ConfigUtil.strings(scenario, VERSIONS, settings.getVersions()).stream().map(inspector::readConfiguration).collect(
Collectors.toList());
if (versions.isEmpty()) {
versions.add(inspector.readConfiguration());
}

List<String> gradleArgs = ConfigUtil.strings(scenario, GRADLE_ARGS);
BuildAction buildAction = getBuildAction(scenario, scenarioFile, settings);
GradleBuildInvoker invoker = invoker(scenario, (GradleBuildInvoker) settings.getInvoker(), buildAction);
int warmUpCount = getWarmUpCount(settings, invoker, scenario);
List<String> measuredBuildOperations = getMeasuredBuildOperations(settings, scenario);
BuildAction cleanupAction = getCleanupAction(scenario);
Map<String, String> systemProperties = ConfigUtil.map(scenario, SYSTEM_PROPERTIES, settings.getSystemProperties());
List<String> jvmArgs = ConfigUtil.strings(scenario, JVM_ARGS);
for (GradleBuildConfiguration version : versions) {
File outputDir = versions.size() == 1 ? scenarioBaseDir : new File(scenarioBaseDir, version.getGradleVersion().getVersion());
definitions.add(new GradleScenarioDefinition(
scenarioName,
title,
invoker,
version,
buildAction,
cleanupAction,
gradleArgs,
systemProperties,
mutators,
warmUpCount,
buildCount,
outputDir,
jvmArgs,
measuredBuildOperations
));
}
}
definitions.addAll(
getScenarioDefinitions(
scenarioFile, settings, scenarioName, scenario, title, mutators, buildCount,
scenarioBaseDir, inspector)
);
}

definitions.forEach(ScenarioDefinition::validate);

return definitions;
}

private static Config getConfig(File scenarioFile, InvocationSettings settings, String scenarioName, Config scenario, String toolName, List<String> toolKeys) {
private static Config getExecutionInstructions(File scenarioFile, InvocationSettings settings, String scenarioName, Config scenario, String toolName, List<String> toolKeys) {
if (settings.isProfile()) {
throw new IllegalArgumentException("Can only profile scenario '" + scenarioName + "' when building using Gradle.");
}
Expand All @@ -298,6 +246,128 @@ private static Config getConfig(File scenarioFile, InvocationSettings settings,
return executionInstructions;
}

private static List<ScenarioDefinition> getScenarioDefinitions(File scenarioFile,
InvocationSettings settings, String scenarioName, Config scenario, String title,
List<BuildMutator> mutators, int buildCount, File scenarioBaseDir,
GradleBuildConfigurationReader inspector) {
List<? extends ScenarioDefinition> scenarioDefinitions;
if (scenario.hasPath(BAZEL) && settings.isBazel()) {
scenarioDefinitions = getBazelScenarioDefinitions(
scenarioFile, settings, scenarioName, scenario, title, mutators, buildCount,
scenarioBaseDir);
} else if (scenario.hasPath(BUCK) && settings.isBuck()) {
scenarioDefinitions = getBuckScenarioDefinitions(
scenarioFile, settings, scenarioName, scenario, title, mutators, buildCount,
scenarioBaseDir);
} else if (scenario.hasPath(MAVEN) && settings.isMaven()) {
scenarioDefinitions = getMavenScenarioDefinitions(
scenarioFile, settings, scenarioName, scenario, title, mutators, buildCount,
scenarioBaseDir);
} else if (!settings.isBazel() && !settings.isBuck() && !settings.isMaven()) {
scenarioDefinitions = getGradleScenarioDefinitions(
scenarioFile, settings, scenarioName, scenario, title, mutators, buildCount,
scenarioBaseDir, inspector
);
} else {
scenarioDefinitions = Collections.emptyList();
}
return Collections.unmodifiableList(scenarioDefinitions);
}

private static List<GradleScenarioDefinition> getGradleScenarioDefinitions(
File scenarioFile,
InvocationSettings settings, String scenarioName, Config scenario, String title,
List<BuildMutator> mutators, int buildCount, File scenarioBaseDir,
GradleBuildConfigurationReader inspector
) {
List<GradleScenarioDefinition> scenarioDefinitions = new ArrayList<>();
List<GradleBuildConfiguration> versions = ConfigUtil
.strings(scenario, VERSIONS, settings.getVersions()).stream()
.map(inspector::readConfiguration).collect(
Collectors.toList());
if (versions.isEmpty()) {
versions.add(inspector.readConfiguration());
}

List<String> gradleArgs = ConfigUtil.strings(scenario, GRADLE_ARGS);
BuildAction buildAction = getBuildAction(scenario, scenarioFile, settings);
GradleBuildInvoker invoker = invoker(scenario,
(GradleBuildInvoker) settings.getInvoker(), buildAction);
int warmUpCount = getWarmUpCount(settings, invoker, scenario);
List<String> measuredBuildOperations = getMeasuredBuildOperations(settings,
scenario);
BuildAction cleanupAction = getCleanupAction(scenario);
Map<String, String> systemProperties = ConfigUtil
.map(scenario, SYSTEM_PROPERTIES, settings.getSystemProperties());
List<String> jvmArgs = ConfigUtil.strings(scenario, JVM_ARGS);
for (GradleBuildConfiguration version : versions) {
File outputDir = versions.size() == 1 ? scenarioBaseDir
: new File(scenarioBaseDir, version.getGradleVersion().getVersion());
scenarioDefinitions.add(new GradleScenarioDefinition(
scenarioName,
title,
invoker,
version,
buildAction,
cleanupAction,
gradleArgs,
systemProperties,
mutators,
warmUpCount,
buildCount,
outputDir,
jvmArgs,
measuredBuildOperations
));
}
return scenarioDefinitions;
}

private static List<MavenScenarioDefinition> getMavenScenarioDefinitions(File scenarioFile,
InvocationSettings settings, String scenarioName, Config scenario, String title,
List<BuildMutator> mutators, int buildCount, File scenarioBaseDir) {

Config executionInstructions = getExecutionInstructions(scenarioFile, settings, scenarioName,
scenario, MAVEN, MAVEN_KEYS);
List<String> targets = ConfigUtil.strings(executionInstructions, TARGETS);
File mavenHome = getToolHome(executionInstructions);
File outputDir = new File(scenarioBaseDir, "maven");
int warmUpCount = getWarmUpCount(settings, scenario);
return Arrays.asList(new MavenScenarioDefinition(scenarioName,
title, targets, mutators, warmUpCount,
buildCount, outputDir, mavenHome));
}

private static List<BuckScenarioDefinition> getBuckScenarioDefinitions(File scenarioFile,
InvocationSettings settings, String scenarioName, Config scenario, String title,
List<BuildMutator> mutators, int buildCount, File scenarioBaseDir) {
Config executionInstructions = getExecutionInstructions(scenarioFile, settings, scenarioName,
scenario, BUCK, BUCK_KEYS);
List<String> targets = ConfigUtil.strings(executionInstructions, TARGETS);
String type = ConfigUtil.string(executionInstructions, TYPE, null);
File buckHome = getToolHome(executionInstructions);
File outputDir = new File(scenarioBaseDir, "buck");
int warmUpCount = getWarmUpCount(settings, scenario);
return Arrays
.asList(new BuckScenarioDefinition(scenarioName, title, targets, type, mutators,
warmUpCount, buildCount, outputDir, buckHome));
}

private static List<BazelScenarioDefinition> getBazelScenarioDefinitions(File scenarioFile,
InvocationSettings settings, String scenarioName, Config scenario, String title,
List<BuildMutator> mutators, int buildCount, File scenarioBaseDir) {
Config executionInstructions = getExecutionInstructions(scenarioFile, settings, scenarioName, scenario,
BAZEL, BAZEL_KEYS);

List<String> targets = ConfigUtil.strings(executionInstructions, TARGETS);
File bazelHome = getToolHome(executionInstructions);
File outputDir = new File(scenarioBaseDir, "bazel");
int warmUpCount = getWarmUpCount(settings, scenario);
return Arrays.asList(new BazelScenarioDefinition(
scenarioName, title, targets, mutators, warmUpCount, buildCount, outputDir,
bazelHome));
}

private static ImmutableList<String> getMeasuredBuildOperations(InvocationSettings settings, Config scenario) {
return ImmutableSet.<String>builder()
.addAll(settings.getMeasuredBuildOperations())
Expand Down

0 comments on commit db2b173

Please sign in to comment.