Skip to content

Commit

Permalink
minor touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Jul 7, 2023
1 parent 7958d2c commit 3ae5d9f
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import java.util.Map;

import com.github.benmanes.caffeine.cache.Policy.CacheEntry;
import com.google.errorprone.annotations.Immutable;

/**
* An immutable entry that includes a snapshot of the policy metadata at its time of creation.
*
* @author [email protected] (Ben Manes)
*/
@Immutable(containerOf = {"K", "V"})
class SnapshotEntry<K, V> implements CacheEntry<K, V> {
private final long snapshot;
private final V value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Integer poll() {
}

/**
* This test checks that the concurrent map is linearizable with bounded model checking. Unlike
* This test checks that the concurrent queue is linearizable with bounded model checking. Unlike
* stress testing, this approach can also provide a trace of an incorrect execution. However, it
* uses sequential consistency model, so it can not find any low-level bugs (e.g., missing
* 'volatile'), and thus, it is recommended to have both test modes.
Expand All @@ -69,7 +69,7 @@ public void modelCheckingTest() {
new LinChecker(getClass(), options).check();
}

/** This test checks that the concurrent map is linearizable with stress testing. */
/** This test checks that the concurrent queue is linearizable with stress testing. */
@Test(groups = "lincheck")
public void stressTest() {
var options = new StressOptions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-rc-2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ json-bind = "1.0"
jsoup = "1.16.1"
junit-testng = "1.0.4"
junit4 = "4.13.2"
junit5 = "5.10.0-M1"
junit5 = "5.10.0-RC1"
kotlin = "1.9.0"
lincheck = "2.18.1"
mockito = "5.4.0"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-rc-3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public void run() {
}

try {
broadcast(policies, trace);
report(policies, trace.characteristics());
broadcast(trace, policies);
report(trace, policies);
} catch (RuntimeException e) {
throwError(policies, e);
throwError(e, policies);
}
}

private void broadcast(List<PolicyActor> policies, TraceReader trace) {
private void broadcast(TraceReader trace, List<PolicyActor> policies) {
long skip = settings.trace().skip();
long limit = settings.trace().limit();
int batchSize = settings.actor().batchSize();
Expand Down Expand Up @@ -112,8 +112,8 @@ private void broadcast(List<PolicyActor> policies, TraceReader trace) {
}
}

private void report(List<PolicyActor> policies, Set<Characteristic> characteristics) {
var reporter = settings.report().format().create(settings.config(), characteristics);
private void report(TraceReader trace, List<PolicyActor> policies) {
var reporter = settings.report().format().create(settings.config(), trace.characteristics());
var results = policies.stream().map(PolicyActor::stats).collect(toImmutableList());
reporter.print(results);
}
Expand All @@ -137,22 +137,22 @@ private ImmutableList<PolicyActor> getPolicyActors(Set<Characteristic> character
}

/** Throws the underlying cause for the simulation failure. */
private void throwError(Iterable<PolicyActor> policies, RuntimeException e) {
private void throwError(RuntimeException error, Iterable<PolicyActor> policies) {
if (!Thread.currentThread().isInterrupted()) {
throw e;
throw error;
}
for (var policy : policies) {
if (policy.future().isCompletedExceptionally()) {
try {
policy.future().join();
} catch (CompletionException error) {
Throwables.throwIfUnchecked(error.getCause());
error.addSuppressed(e);
throw error;
} catch (CompletionException e) {
Throwables.throwIfUnchecked(e.getCause());
e.addSuppressed(error);
throw e;
}
}
}
throw e;
throw error;
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package com.github.benmanes.caffeine.cache.simulator;

import static java.util.Locale.US;
import static java.util.Objects.requireNonNull;

import java.util.stream.LongStream;

import com.github.benmanes.caffeine.cache.simulator.BasicSettings.SyntheticSettings.HotspotSettings;
import com.github.benmanes.caffeine.cache.simulator.BasicSettings.SyntheticSettings.UniformSettings;
import com.github.benmanes.caffeine.cache.simulator.BasicSettings.TraceSettings;
import com.github.benmanes.caffeine.cache.simulator.parser.TraceReader.KeyOnlyTraceReader;

Expand Down Expand Up @@ -48,26 +48,26 @@ public static KeyOnlyTraceReader generate(TraceSettings settings) {
int events = settings.synthetic().events();
switch (settings.synthetic().distribution().toLowerCase(US)) {
case "counter":
return () -> counter(settings.synthetic().counter().start(), events);
return counter(settings.synthetic().counter().start(), events);
case "repeating":
return () -> repeating(settings.synthetic().repeating().items(), events);
return repeating(settings.synthetic().repeating().items(), events);
case "uniform":
UniformSettings uniform = settings.synthetic().uniform();
return () -> uniform(uniform.lowerBound(), uniform.upperBound(), events);
var uniform = settings.synthetic().uniform();
return uniform(uniform.lowerBound(), uniform.upperBound(), events);
case "exponential":
return () -> exponential(settings.synthetic().exponential().mean(), events);
return exponential(settings.synthetic().exponential().mean(), events);
case "hotspot":
HotspotSettings hotspot = settings.synthetic().hotspot();
return () -> Synthetic.hotspot(hotspot.lowerBound(), hotspot.upperBound(),
return Synthetic.hotspot(hotspot.lowerBound(), hotspot.upperBound(),
hotspot.hotOpnFraction(), hotspot.hotsetFraction(), events);
case "zipfian":
return () -> zipfian(settings.synthetic().zipfian().items(),
return zipfian(settings.synthetic().zipfian().items(),
settings.synthetic().zipfian().constant(), events);
case "scrambled-zipfian":
return () -> scrambledZipfian(settings.synthetic().zipfian().items(),
return scrambledZipfian(settings.synthetic().zipfian().items(),
settings.synthetic().zipfian().constant(), events);
case "skewed-zipfian-latest":
return () -> skewedZipfianLatest(settings.synthetic().zipfian().items(), events);
return skewedZipfianLatest(settings.synthetic().zipfian().items(), events);
default:
throw new IllegalStateException("Unknown distribution: "
+ settings.synthetic().distribution());
Expand All @@ -80,7 +80,7 @@ public static KeyOnlyTraceReader generate(TraceSettings settings) {
* @param start the number that the counter starts from
* @param events the number of events in the distribution
*/
public static LongStream counter(int start, int events) {
public static KeyOnlyTraceReader counter(int start, int events) {
return generate(new CounterGenerator(start), events);
}

Expand All @@ -90,7 +90,7 @@ public static LongStream counter(int start, int events) {
* @param items the number of items in the distribution
* @param events the number of events in the distribution
*/
public static LongStream repeating(int items, int events) {
public static KeyOnlyTraceReader repeating(int items, int events) {
return generate(new SequentialGenerator(0, items), events);
}

Expand All @@ -103,7 +103,7 @@ public static LongStream repeating(int items, int events) {
* @param events the number of events in the distribution
* @return a stream of cache events
*/
public static LongStream uniform(int lowerBound, int upperBound, int events) {
public static KeyOnlyTraceReader uniform(int lowerBound, int upperBound, int events) {
return generate(new UniformLongGenerator(lowerBound, upperBound), events);
}

Expand All @@ -114,7 +114,7 @@ public static LongStream uniform(int lowerBound, int upperBound, int events) {
* @param mean mean arrival rate of gamma (a half life of 1/gamma)
* @param events the number of events in the distribution
*/
public static LongStream exponential(double mean, int events) {
public static KeyOnlyTraceReader exponential(double mean, int events) {
return generate(new ExponentialGenerator(mean), events);
}

Expand All @@ -131,7 +131,7 @@ public static LongStream exponential(double mean, int events) {
* @param hotOpnFraction percentage of operations accessing the hot set
* @param events the number of events in the distribution
*/
public static LongStream hotspot(int lowerBound, int upperBound,
public static KeyOnlyTraceReader hotspot(int lowerBound, int upperBound,
double hotsetFraction, double hotOpnFraction, int events) {
return generate(new HotspotIntegerGenerator(lowerBound,
upperBound, hotsetFraction, hotOpnFraction), events);
Expand All @@ -147,7 +147,7 @@ public static LongStream hotspot(int lowerBound, int upperBound,
* @param constant the skew factor for the distribution
* @param events the number of events in the distribution
*/
public static LongStream scrambledZipfian(int items, double constant, int events) {
public static KeyOnlyTraceReader scrambledZipfian(int items, double constant, int events) {
return generate(new ScrambledZipfianGenerator(0, items - 1, constant), events);
}

Expand All @@ -158,7 +158,7 @@ public static LongStream scrambledZipfian(int items, double constant, int events
* @param items the number of items in the distribution
* @param events the number of events in the distribution
*/
public static LongStream skewedZipfianLatest(int items, int events) {
public static KeyOnlyTraceReader skewedZipfianLatest(int items, int events) {
return generate(new SkewedLatestGenerator(new CounterGenerator(items)), events);
}

Expand All @@ -170,12 +170,13 @@ public static LongStream skewedZipfianLatest(int items, int events) {
* @param constant the skew factor for the distribution
* @param events the number of events in the distribution
*/
public static LongStream zipfian(int items, double constant, int events) {
public static KeyOnlyTraceReader zipfian(int items, double constant, int events) {
return generate(new ZipfianGenerator(items, constant), events);
}

/** Returns a sequence of items constructed by the generator. */
private static LongStream generate(NumberGenerator generator, long count) {
return LongStream.range(0, count).map(ignored -> generator.nextValue().longValue());
private static KeyOnlyTraceReader generate(NumberGenerator generator, long count) {
requireNonNull(generator);
return () -> LongStream.range(0, count).map(ignored -> generator.nextValue().longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.benmanes.caffeine.cache.simulator.policy.AccessEvent;
import com.github.benmanes.caffeine.cache.simulator.policy.Policy.Characteristic;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.MustBeClosed;

/**
* A reader to an access trace.
Expand All @@ -42,6 +43,7 @@ public interface TraceReader {
*
* @return a lazy stream of cache events
*/
@MustBeClosed
Stream<AccessEvent> events();

/** A trace reader that that does not contain external event metadata. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import java.util.Objects;

import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.Immutable;

/**
* The key and metadata for accessing a cache.
*
* @author [email protected] (Ben Manes)
*/
@Immutable
public class AccessEvent {
private final long key;

Expand Down

0 comments on commit 3ae5d9f

Please sign in to comment.