Skip to content

Commit

Permalink
Minor test gc optimization
Browse files Browse the repository at this point in the history
Tests are hitting the time limit due to the exhaustive nature of brute
forcing with millions of cases. This change avoids some of the
unnecessary churn, though a deeper dive is needed or a request to extend
the free limit.

Using ParallelGC is only required for deterministic soft reference
collection in tests. Switching the other tests to use G1 reduces their
tasks by 1.5 minutes each.
  • Loading branch information
ben-manes committed Feb 22, 2019
1 parent 3ade58b commit 45feb12
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bin
build
build-cache
test-output
jitwatch.out
.java-version
Expand Down
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ before_cache:
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
cache:
directories:
- build-cache
- $HOME/.m2
- $HOME/.sonar/cache
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/
- $HOME/.gradle/caches
- $HOME/.gradle/wrapper
2 changes: 2 additions & 0 deletions caffeine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ task generateLocalCaches(type: JavaExec) {

outputs.upToDateWhen { !tasks.compileJavaPoetJava.didWork }
outputs.dir "${buildDir}/generated-sources/"
outputs.cacheIf { true }
}
compileJava.dependsOn(generateLocalCaches)

Expand All @@ -114,6 +115,7 @@ task generateNodes(type: JavaExec) {

outputs.upToDateWhen { !tasks.compileJavaPoetJava.didWork }
outputs.dir "${buildDir}/generated-sources/"
outputs.cacheIf { true }
}
compileJava.dependsOn(generateNodes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author [email protected] (Ben Manes)
*/
@Listeners(CacheValidationListener.class)
@Test(groups = "isolated", dataProviderClass = CacheProvider.class)
@Test(dataProviderClass = CacheProvider.class)
public final class HashClashTest {
private static final int STEP = 5;
private static final Long LONG_1 = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.github.benmanes.caffeine.cache.testing.CacheSpec.ReferenceType;
import com.github.benmanes.caffeine.cache.testing.CacheSpec.Stats;
import com.github.benmanes.caffeine.cache.testing.CacheSpec.Writer;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
Expand All @@ -51,6 +52,11 @@
* @author [email protected] (Ben Manes)
*/
final class CacheGenerator {
// Integer caches the object identity semantics of autoboxing for values between
// -128 and 127 (inclusive) as required by JLS (assuming default setting)
private static final List<Entry<Integer, Integer>> INTS = makeInts();
private static final int BASE = 1_000;

private final Options options;
private final CacheSpec cacheSpec;
private final boolean isAsyncOnly;
Expand Down Expand Up @@ -207,20 +213,18 @@ private void populate(CacheContext context, Cache<Integer, Integer> cache) {
return;
}

// Integer caches the object identity semantics of autoboxing for values between
// -128 and 127 (inclusive) as required by JLS
int base = 1000;

int maximum = (int) Math.min(context.maximumSize(), context.population.size());
int first = base + (int) Math.min(1, context.population.size());
int last = base + maximum;
int middle = Math.max(first, base + ((last - first) / 2));
int first = BASE + (int) Math.min(0, context.population.size());
int last = BASE + maximum - 1;
int middle = Math.max(first, BASE + ((last - first) / 2));

context.disableRejectingCacheWriter();
for (int i = 1; i <= maximum; i++) {
for (int i = 0; i < maximum; i++) {
Entry<Integer, Integer> entry = INTS.get(i);

// Reference caching (weak, soft) require unique instances for identity comparison
Integer key = new Integer(base + i);
Integer value = new Integer(-key);
Integer key = context.isStrongKeys() ? entry.getKey() : new Integer(BASE + i);
Integer value = context.isStrongValues() ? entry.getValue() : new Integer(-key);

if (key == first) {
context.firstKey = key;
Expand All @@ -240,4 +244,18 @@ private void populate(CacheContext context, Cache<Integer, Integer> cache) {
reset(context.cacheWriter());
}
}

/** Returns a cache of integers and their negation. */
@SuppressWarnings("BoxedPrimitiveConstructor")
private static List<Entry<Integer, Integer>> makeInts() {
int size = Stream.of(CacheSpec.Population.values())
.mapToInt(population -> Math.toIntExact(population.size()))
.max().getAsInt();
ImmutableList.Builder<Entry<Integer, Integer>> builder = ImmutableList.builder();
for (int i = 0; i < size; i++) {
int value = BASE + i;
builder.add(Maps.immutableEntry(new Integer(value), new Integer(-value)));
}
return builder.build();
}
}
3 changes: 3 additions & 0 deletions caffeine/testing.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ testNames.each { testName ->
systemProperties['values'] = strength[1]
systemProperties['compute'] = isAsync ? 'async' : 'sync'
systemProperties['stats'] = hasStats ? 'enabled' : 'disabled'

}
systemProperties['implementation'] = implementation

Expand Down Expand Up @@ -69,12 +70,14 @@ tasks.withType(Test) {
if (options instanceof TestNGOptions) {
if (name.startsWith('slow')) {
maxParallelForks = 2
jvmArgs '-XX:+UseParallelGC'
options.includeGroups = ['slow']
} else if (name.startsWith('isolated')) {
options.includeGroups = ['isolated']
} else {
options {
excludeGroups = ['slow', 'isolated']
jvmArgs '-XX:+UseG1GC'
parallel = 'methods'
threadCount = 6
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
org.gradle.jvmargs=-Xmx1024m -XX:+UseG1GC -XX:SoftRefLRUPolicyMSPerMB=0 -noverify
org.gradle.caching=true
org.gradle.daemon=true
nexusUsername=
nexusPassword=
4 changes: 2 additions & 2 deletions gradle/codeQuality.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tasks.sonarqube.dependsOn(jacocoMerge)

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs << '-Xlint:all'
options.compilerArgs << '-Xlint:all,-auxiliaryclass'
options.errorprone.errorproneArgs << '-Xep:NullAway:ERROR'
options.errorprone {
option('ClassName')
Expand Down Expand Up @@ -108,7 +108,7 @@ tasks.withType(Test) {
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}
options {
jvmArgs '-XX:SoftRefLRUPolicyMSPerMB=0', '-XX:+UseParallelGC', '-noverify'
jvmArgs '-XX:SoftRefLRUPolicyMSPerMB=0', '-noverify'
}
if (System.env.'CI') {
maxHeapSize = '512m'
Expand Down
9 changes: 9 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ include 'caffeine'
include 'guava'
include 'jcache'
include 'simulator'

buildCache {
local.enabled = true

local(DirectoryBuildCache) {
directory = new File(rootDir, 'build-cache')
removeUnusedEntriesAfterDays = 30
}
}
6 changes: 3 additions & 3 deletions travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ case "${1:?''}" in
run "sh -c 'cd examples/write-behind-rxjava && mvn test'"
;;
tests)
run "./gradlew check --scan --console plain"
runSlow "./gradlew :caffeine:slowCaffeineTest --scan --console plain"
runSlow "./gradlew :caffeine:slowGuavaTest --scan --console plain"
run "./gradlew check --console plain"
runSlow "./gradlew :caffeine:slowCaffeineTest --console plain"
runSlow "./gradlew :caffeine:slowGuavaTest --console plain"
run "./gradlew coveralls uploadArchives --console plain"
runSlow "./gradlew sonarqube --console plain"
;;
Expand Down

0 comments on commit 45feb12

Please sign in to comment.