-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose project dir in BuildContext and ScenarioContext (#232)
- Loading branch information
Showing
5 changed files
with
124 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
src/main/java/org/gradle/profiler/DefaultBuildContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/main/java/org/gradle/profiler/DefaultScenarioContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
16 changes: 13 additions & 3 deletions
16
src/test/groovy/org/gradle/profiler/mutations/AbstractMutatorTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |