Created
April 4, 2019 09:41
-
-
Save ohadeytan/712bb2c9183319ae0d306a42bda8d6e6 to your computer and use it in GitHub Desktop.
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
import com.github.benmanes.caffeine.cache.Caffeine; | |
import com.github.benmanes.caffeine.cache.Cache; | |
import com.github.benmanes.caffeine.cache.simulator.parser.TextTraceReader; | |
import java.io.IOException; | |
import java.util.stream.Stream; | |
import java.util.stream.LongStream; | |
import java.util.AbstractMap.SimpleEntry; | |
import java.util.Map.Entry; | |
/** | |
* Run Caffeine on sized traces. | |
* Trace format and output format are equivalent to those of webcachesim | |
* (https://github.com/dasebe/webcachesim). | |
**/ | |
public class RunCaffeine { | |
public static final class SizedTraceReader extends TextTraceReader { | |
public SizedTraceReader(String filePath) { | |
super(filePath); | |
} | |
@Override | |
public LongStream events() throws IOException { | |
return null; | |
} | |
public Stream<Entry<Long, Integer>> sizedEvents() throws IOException { | |
return lines().map(line -> { | |
String[] array = line.split(" ", 3); | |
long key = Long.parseLong(array[1]); | |
int size = Integer.parseInt(array[2]); | |
return new SimpleEntry<>(key, size); | |
}); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
String tracePath = args[0]; | |
long cacheSize = Long.parseLong(args[1]); | |
Cache<Long, Integer> cache = Caffeine.newBuilder() | |
.maximumWeight(cacheSize) | |
.weigher((key, weight) -> (int) weight) | |
.executor(Runnable::run) // disable async eviction | |
.recordStats() | |
.build(); | |
SizedTraceReader trace = new SizedTraceReader(tracePath); | |
trace.sizedEvents().forEach(item -> { cache.get(item.getKey(), x -> item.getValue()); }); | |
System.out.println("Caffeine " + cacheSize + " " + cache.stats().requestCount() + " " + cache.stats().hitCount() + " " + cache.stats().hitRate()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment