Skip to content

Commit

Permalink
Expose project dir in BuildContext and ScenarioContext (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
lptr authored Aug 31, 2020
1 parent 7b83063 commit 8f44f2b
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 62 deletions.
29 changes: 5 additions & 24 deletions src/main/java/org/gradle/profiler/BuildContext.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
package org.gradle.profiler;

import java.util.UUID;
public interface BuildContext extends ScenarioContext {
String getUniqueBuildId();

public class BuildContext extends ScenarioContext {
private final Phase phase;
private final int iteration;
Phase getPhase();

protected BuildContext(UUID invocationId, String scenarioName, Phase phase, int iteration) {
super(invocationId, scenarioName);
this.phase = phase;
this.iteration = iteration;
}
int getIteration();

public String getUniqueBuildId() {
return String.format("%s_%s_%d", getUniqueScenarioId(), phase.name(), iteration);
}

public Phase getPhase() {
return phase;
}

public int getIteration() {
return iteration;
}

public String getDisplayName() {
return phase.displayBuildNumber(iteration);
}
String getDisplayName();
}
50 changes: 50 additions & 0 deletions src/main/java/org/gradle/profiler/DefaultBuildContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.gradle.profiler;

import java.io.File;

public class DefaultBuildContext implements BuildContext {
private final ScenarioContext scenarioContext;
private final Phase phase;
private final int iteration;

protected DefaultBuildContext(ScenarioContext scenarioContext, Phase phase, int iteration) {
this.scenarioContext = scenarioContext;
this.phase = phase;
this.iteration = iteration;
}

@Override
public String getUniqueScenarioId() {
return scenarioContext.getUniqueScenarioId();
}

@Override
public File getProjectDir() {
return scenarioContext.getProjectDir();
}

@Override
public BuildContext withBuild(Phase phase, int count) {
return scenarioContext.withBuild(phase, count);
}

@Override
public String getUniqueBuildId() {
return String.format("%s_%s_%d", getUniqueScenarioId(), phase.name(), iteration);
}

@Override
public Phase getPhase() {
return phase;
}

@Override
public int getIteration() {
return iteration;
}

@Override
public String getDisplayName() {
return phase.displayBuildNumber(iteration);
}
}
49 changes: 49 additions & 0 deletions src/main/java/org/gradle/profiler/DefaultScenarioContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.gradle.profiler;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.hash.Hashing;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class DefaultScenarioContext implements ScenarioContext {
private final UUID invocationId;
private final String scenarioName;
private final File projectDir;

@VisibleForTesting
public DefaultScenarioContext(UUID invocationId, String scenarioName, File projectDir) {
this.invocationId = invocationId;
this.scenarioName = scenarioName;
this.projectDir = projectDir;
}

@Override
public String getUniqueScenarioId() {
return String.format("_%s_%s", invocationId.toString().replaceAll("-", "_"), mangleName(scenarioName));
}

@Override
public File getProjectDir() {
return projectDir;
}

@Override
public BuildContext withBuild(Phase phase, int count) {
return new DefaultBuildContext(this, phase, count);
}

/**
* This is to ensure that the scenario ID is a valid Java identifier part, and it is also (reasonably) unique.
*/
private static String mangleName(String scenarioName) {
StringBuilder name = new StringBuilder();
for (char ch :scenarioName.toCharArray()){
name.append(Character.isJavaIdentifierPart(ch) ? ch : '_');
}
name.append('_');
name.append(Hashing.murmur3_32().hashString(scenarioName, StandardCharsets.UTF_8));
return name.toString();
}
}
42 changes: 7 additions & 35 deletions src/main/java/org/gradle/profiler/ScenarioContext.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
package org.gradle.profiler;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.hash.Hashing;
import java.io.File;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class ScenarioContext {
private final UUID invocationId;
private final String scenarioName;

public static ScenarioContext from(InvocationSettings invocationSettings, ScenarioDefinition scenarioDefinition) {
return new ScenarioContext(invocationSettings.getInvocationId(), scenarioDefinition.getName());
};

@VisibleForTesting
public ScenarioContext(UUID invocationId, String scenarioName) {
this.invocationId = invocationId;
this.scenarioName = scenarioName;
public interface ScenarioContext {
static ScenarioContext from(InvocationSettings invocationSettings, ScenarioDefinition scenarioDefinition) {
return new DefaultScenarioContext(invocationSettings.getInvocationId(), scenarioDefinition.getName(), invocationSettings.getProjectDir());
}

public String getUniqueScenarioId() {
return String.format("_%s_%s", invocationId.toString().replaceAll("-", "_"), mangleName(scenarioName));
}
String getUniqueScenarioId();

public BuildContext withBuild(Phase phase, int count) {
return new BuildContext(invocationId, scenarioName, phase, count);
}
File getProjectDir();

/**
* This is to ensure that the scenario ID is a valid Java identifier part, and it is also (reasonably) unique.
*/
private static String mangleName(String scenarioName) {
StringBuilder name = new StringBuilder();
for (char ch :scenarioName.toCharArray()){
name.append(Character.isJavaIdentifierPart(ch) ? ch : '_');
}
name.append('_');
name.append(Hashing.murmur3_32().hashString(scenarioName, StandardCharsets.UTF_8));
return name.toString();
}
BuildContext withBuild(Phase phase, int count);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package org.gradle.profiler.mutations

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

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

def setup() {
def projectDir = tmpDir.getRoot()
projectDir.mkdirs()
scenarioContext = new DefaultScenarioContext(UUID.fromString("276d92f3-16ac-4064-9a18-5f1dfd67992f"), "testScenario", projectDir)
buildContext = scenarioContext.withBuild(Phase.MEASURE, 7)
}
}

0 comments on commit 8f44f2b

Please sign in to comment.