下面列出了怎么用com.google.common.cache.CacheStats的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public int getIdFromValueBytesWithoutCache(byte[] value, int offset, int len, int roundingFlag) {
byte[] val = Arrays.copyOfRange(value, offset, offset + len);
AppendDictSliceKey sliceKey = metadata.sliceFileMap.floorKey(AppendDictSliceKey.wrap(val));
if (sliceKey == null) {
sliceKey = metadata.sliceFileMap.firstKey();
}
AppendDictSlice slice;
try {
slice = dictCache.get(sliceKey);
} catch (ExecutionException e) {
throw new IllegalStateException("Failed to load slice with key " + sliceKey, e.getCause());
}
CacheStats stats = dictCache.stats();
if (evictionThreshold > 0 && stats.evictionCount() > evictionThreshold * metadata.sliceFileMap.size()
&& stats.loadCount() > (evictionThreshold + 1) * metadata.sliceFileMap.size()) {
logger.warn(
"Too many dict slice evictions and reloads, maybe the memory is not enough to hold all the dictionary");
throw new RuntimeException("Too many dict slice evictions: " + stats + " for "
+ metadata.sliceFileMap.size() + " dict slices. "
+ "Maybe the memory is not enough to hold all the dictionary, try to enlarge the mapreduce/spark executor memory.");
}
return slice.getIdFromValueBytesImpl(value, offset, len, roundingFlag);
}
@Test
public void testLookupCaching() {
KeySpace keySpace = new KeySpace(new KeySpaceDirectory("path", KeyType.STRING, "path"));
ResolvedKeySpacePath path1;
try (FDBRecordContext context = database.openContext()) {
path1 = keySpace.resolveFromKey(context, Tuple.from("path"));
}
LocatableResolver resolver = scopedDirectoryGenerator.apply(database, path1);
Long value = resolver.resolve("foo").join();
for (int i = 0; i < 5; i++) {
Long fetched = resolver.resolve("foo").join();
assertThat("we should always get the original value", fetched, is(value));
}
CacheStats stats = database.getDirectoryCacheStats();
assertThat("subsequent lookups should hit the cache", stats.hitCount(), is(5L));
}
/**
* Get the current WatchedByAttributeCache status. Returns the cumulative status of
* <ul>
* <li>hitCount
* <li>missCount;
* <li>loadSuccessCount;
* <li>loadExceptionCount;
* <li>totalLoadTime;
* <li>evictionCount;
* </ul>
*
* @return cache status map.
*/
@RequestMapping(value = "/cache/stats", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getCacheStats() {
Map<String, Object> stat = new LinkedHashMap<>(5);
stat.put("status", "ok");
stat.put("maxSize", cache.getMaxSize());
stat.put("currentSize", cache.instance().size());
stat.put("timeout", cache.getTimeout());
CacheStats cs = cache.instance().stats();
stat.put("hitCount", cs.hitCount());
stat.put("missCount", cs.missCount());
stat.put("loadSuccessCount", cs.loadSuccessCount());
stat.put("totalLoadTime", TimeUnit.SECONDS.convert(cs.totalLoadTime(), TimeUnit.NANOSECONDS));
stat.put("loadExceptionCount", cs.loadExceptionCount());
stat.put("evictionCount", cs.evictionCount());
return stat;
}
/**
* Get the current sink cache status. Returns the cumulative status of
* <ul>
* <li>hitCount
* <li>missCount;
* <li>loadSuccessCount;
* <li>loadExceptionCount;
* <li>totalLoadTime;
* <li>evictionCount;
* </ul>
*
* @return cache status map.
*/
@RequestMapping(value = "/cache/stats", method = GET)
@ResponseBody
public Map<String, Object> getCacheStats() {
Map<String, Object> stat = new LinkedHashMap<>(5);
stat.put("status", "ok");
stat.put("maxSize", cache.getMaxSize());
stat.put("currentSize", cache.instance().size());
stat.put("timeout", cache.getTimeout());
CacheStats cs = cache.instance().stats();
stat.put("hitCount", cs.hitCount());
stat.put("missCount", cs.missCount());
stat.put("loadSuccessCount", cs.loadSuccessCount());
stat.put("totalLoadTime", SECONDS.convert(cs.totalLoadTime(), NANOSECONDS));
stat.put("loadExceptionCount", cs.loadExceptionCount());
stat.put("evictionCount", cs.evictionCount());
return stat;
}
public void execSelectPlan(
final RootPlan plan,
final List<Binding> newValues,
final Context context) {
Objects.nonNull(ContextUtils.getSelectOutput(context));
final ExecutionKey key = new ExecutionKey(plan, newValues);
if (++nbselect % 2000 == 00) {
CacheStats stats = selectExecutions.stats();
LOG.info("call select " + nbselect + " count " + stats.loadCount() + " - hit count " + stats.hitCount() + " - rate " + stats.hitRate());
}
ResultSetRewindable resultSet = selectExecutions.getIfPresent(key);
if (resultSet != null) {
resultSet.reset();
} else {
ResultSet memResultSet = plan.execSelect(newValues, context);
resultSet = ResultSetFactory.copyResults(memResultSet);
selectExecutions.put(key, resultSet);
}
ContextUtils.getSelectOutput(context).accept(resultSet);
}
@Override
public int getIdFromValueBytesWithoutCache(byte[] value, int offset, int len, int roundingFlag) {
byte[] val = Arrays.copyOfRange(value, offset, offset + len);
AppendDictSliceKey sliceKey = metadata.sliceFileMap.floorKey(AppendDictSliceKey.wrap(val));
if (sliceKey == null) {
sliceKey = metadata.sliceFileMap.firstKey();
}
AppendDictSlice slice;
try {
slice = dictCache.get(sliceKey);
} catch (ExecutionException e) {
throw new IllegalStateException("Failed to load slice with key " + sliceKey, e.getCause());
}
CacheStats stats = dictCache.stats();
if (evictionThreshold > 0 && stats.evictionCount() > evictionThreshold * metadata.sliceFileMap.size()
&& stats.loadCount() > (evictionThreshold + 1) * metadata.sliceFileMap.size()) {
logger.warn(
"Too many dict slice evictions and reloads, maybe the memory is not enough to hold all the dictionary");
throw new RuntimeException("Too many dict slice evictions: " + stats + " for "
+ metadata.sliceFileMap.size() + " dict slices. "
+ "Maybe the memory is not enough to hold all the dictionary, try to enlarge the mapreduce/spark executor memory.");
}
return slice.getIdFromValueBytesImpl(value, offset, len, roundingFlag);
}
private long getSize() {
final long size = cache.size();
final CacheStats stats = cache.stats();
getCounter("hits").increment(stats.hitCount());
getCounter("miss").increment(stats.missCount());
getCounter("evictions").increment(stats.evictionCount());
getCounter("requests").increment(stats.requestCount());
getCounter("loadExceptionCount").increment(stats.loadExceptionCount());
getCounter("loadCount").increment(stats.loadCount());
getCounter("loadSuccessCount").increment(stats.loadSuccessCount());
getCounter("totalLoadTime-ms").increment(cache.stats().totalLoadTime()/1000000);
getGauge("hitrate").set(stats.hitRate());
getGauge("loadExceptionRate").set(stats.loadExceptionRate());
getGauge("averageLoadTime-ms").set(stats.averageLoadPenalty()/1000000);
return size;
}
@Override
public void stat(){
if (STAT_ENABLED) {
CacheStats shortStats = shortTermCache.stats();
CacheStats longStats = longTermCache.stats();
System.out.println("--------- GUAVA CACHE Statistics ---------");
System.out.println("|>--- SHORT-TERM");
System.out.println("| Hits (count/rate): " + shortStats.hitCount() + " / "
+ String.format("%.2f%%", shortStats.hitRate() * 100));
System.out.println("| Misses (count/rate): " + shortStats.missCount() + " / "
+ String.format("%.2f%%", shortStats.missRate() * 100));
System.out.println("|>--- LONG-TERM ");
System.out.println("| Hits (count/rate): " + longStats.hitCount() + " / "
+ String.format("%.2f%%", longStats.hitRate() * 100));
System.out.println("| Misses (count/rate): " + longStats.missCount() + " / "
+ String.format("%.2f%%", longStats.missRate() * 100));
System.out.println("------------------------------------------");
}
}
public ColumnarStoreCacheStats getCacheStats() {
ColumnarStoreCacheStats stats = new ColumnarStoreCacheStats();
CacheStats cacheStats = fragmentDataCache.stats();
stats.setHitCount(cacheStats.hitCount());
stats.setMissCount(cacheStats.missCount());
stats.setEvictionCount(cacheStats.evictionCount());
stats.setLoadSuccessCount(cacheStats.loadSuccessCount());
stats.setLoadExceptionCount(cacheStats.loadExceptionCount());
stats.setTotalLoadTime(cacheStats.totalLoadTime());
stats.setCacheEntriesNum(fragmentDataCache.size());
stats.setCachedDataBufferSize(currentBufferedSize.get());
return stats;
}
@Override
public Map<String, Object> getValue() {
CacheStats stats = cache.stats();
Map<String, Object> values = new LinkedHashMap<String, Object>(6);
values.put("size", cache.size());
values.put("count", stats.requestCount());
values.put("hits", stats.hitCount());
values.put("misses", stats.missCount());
values.put("load.count", stats.loadCount());
values.put("load.time", stats.totalLoadTime());
return values;
}
@Test
void reportExpectedMetrics() {
MeterRegistry registry = new SimpleMeterRegistry();
metrics.bindTo(registry);
verifyCommonCacheMetrics(registry, metrics);
// common metrics
Gauge cacheSize = fetch(registry, "cache.size").gauge();
assertThat(cacheSize.value()).isEqualTo(cache.size());
FunctionCounter hitCount = fetch(registry, "cache.gets", Tags.of("result", "hit")).functionCounter();
assertThat(hitCount.count()).isEqualTo(metrics.hitCount());
FunctionCounter missCount = fetch(registry, "cache.gets", Tags.of("result", "miss")).functionCounter();
assertThat(missCount.count()).isEqualTo(metrics.missCount().doubleValue());
FunctionCounter cachePuts = fetch(registry, "cache.puts").functionCounter();
assertThat(cachePuts.count()).isEqualTo(metrics.putCount());
FunctionCounter cacheEviction = fetch(registry, "cache.evictions").functionCounter();
assertThat(cacheEviction.count()).isEqualTo(metrics.evictionCount().doubleValue());
CacheStats stats = cache.stats();
TimeGauge loadDuration = fetch(registry, "cache.load.duration").timeGauge();
assertThat(loadDuration.value()).isEqualTo(stats.totalLoadTime());
FunctionCounter successfulLoad = fetch(registry, "cache.load", Tags.of("result", "success")).functionCounter();
assertThat(successfulLoad.count()).isEqualTo(stats.loadSuccessCount());
FunctionCounter failedLoad = fetch(registry, "cache.load", Tags.of("result", "failure")).functionCounter();
assertThat(failedLoad.count()).isEqualTo(stats.loadExceptionCount());
}
public long[] getMemberDescriptorsCountStats() throws ExecutionException {
CacheStats cacheStats = this.memberCache.stats();
return new long[] {
cacheStats.hitCount(),
cacheStats.loadCount(),
cacheStats.loadExceptionCount(),
cacheStats.loadSuccessCount(),
cacheStats.missCount(),
cacheStats.requestCount(),
};
}
@Override
public void readFrom(StreamInput in) throws IOException {
size = in.readVLong();
long hitCount = in.readVLong();
long misscount = in.readVLong();
long loadSuccessCount = in.readVLong();
long loadExceptionCount = in.readVLong();
long totalLoadTime = in.readVLong();
long evictionCount = in.readVLong();
cacheStats = new CacheStats(hitCount, misscount, loadSuccessCount, loadExceptionCount, totalLoadTime, evictionCount);
}
@Override
public CacheStats stats() {
try {
return delegate.stats();
} finally {
updateGaugeMap();
}
}
private void validateCacheRender(final String context,
final RenderParameters params,
final ImageProcessorCache imageProcessorCache,
final int expectedCacheSize,
final int expectedHitCount,
final String expectedDigestString)
throws Exception {
final BufferedImage targetImage = params.openTargetImage();
ArgbRenderer.render(params, targetImage, imageProcessorCache);
Assert.assertEquals(context + ": invalid number of items in cache",
expectedCacheSize, imageProcessorCache.size());
final CacheStats stats = imageProcessorCache.getStats();
Assert.assertEquals(context + ": invalid number of cache hits",
expectedHitCount, stats.hitCount());
Utils.saveImage(targetImage,
outputFile.getAbsolutePath(),
Utils.JPEG_FORMAT,
params.isConvertToGray(),
params.getQuality());
final String actualDigestString = getDigestString(outputFile);
Assert.assertEquals(context + ": stitched file MD5 hash differs from expected result",
expectedDigestString, actualDigestString);
}
public final String toJSON() {
final JsonObject result = new JsonObject()
.add("version", getPlogVersion())
.add("uptime", System.currentTimeMillis() - startTime)
.add("udp_simple_messages", udpSimpleMessages.get())
.add("udp_invalid_version", udpInvalidVersion.get())
.add("v0_invalid_type", v0InvalidType.get())
.add("v0_invalid_multipart_header", v0InvalidMultipartHeader.get())
.add("unknown_command", unknownCommand.get())
.add("v0_commands", v0Commands.get())
.add("exceptions", exceptions.get())
.add("unhandled_objects", unhandledObjects.get())
.add("holes_from_dead_port", holesFromDeadPort.get())
.add("holes_from_new_message", holesFromNewMessage.get())
.add("v0_fragments", arrayForLogStats(v0MultipartMessageFragments))
.add("v0_invalid_checksum", arrayForLogStats(v0InvalidChecksum))
.add("v0_invalid_fragments", arrayForLogLogStats(invalidFragments))
.add("dropped_fragments", arrayForLogLogStats(droppedFragments));
if (defragmenter != null) {
final CacheStats cacheStats = defragmenter.getCacheStats();
result.add("defragmenter", new JsonObject()
.add("evictions", cacheStats.evictionCount())
.add("hits", cacheStats.hitCount())
.add("misses", cacheStats.missCount()));
}
final JsonArray handlersStats = new JsonArray();
result.add("handlers", handlersStats);
for (Handler handler : handlers) {
final JsonObject statsCandidate = handler.getStats();
final JsonObject stats = (statsCandidate == null) ? new JsonObject() : statsCandidate;
handlersStats.add(stats.set("name", handler.getName()));
}
return result.toString();
}
@Override
public void commandComplete() {
if (stats != null) {
CacheStats newStats = DigestUtils.getCacheStats();
Preconditions.checkNotNull(newStats, "The cache is enabled so we must get some stats back");
logStats("Accumulated cache stats after command", newStats);
logStats("Cache stats for finished command", newStats.minus(stats));
stats = null; // Silence stats until next command that uses the executor.
}
}
@Override
public void stat(){
CacheStats shortStats = shortTermCache.stats();
System.out.println("--------- GUAVA CACHE Statistics ---------");
System.out.println("Hits (count/rate): " + shortStats.hitCount() + " / "
+ String.format("%.2f%%", shortStats.hitRate() * 100));
System.out.println("Misses (count/rate): " + shortStats.missCount() + " / "
+ String.format("%.2f%%", shortStats.missRate() * 100));
System.out.println("------------------------------------------");
}
@Override
public void showCache(Formatter f) {
CacheStats stats = cache.stats();
f.format("%n%s%n%s%n", name, stats);
}
@Override
public void showStats(Formatter f) {
CacheStats stats = cache.stats();
f.format("%s", stats);
}
public CacheStats getCacheStats() {
return dictCache.stats();
}
public CacheStats getDirectoryCacheStats() {
return directoryCache.stats();
}
@Override
public CacheStats stats()
{
throw new UnsupportedOperationException();
}
@Override
public CacheStats stats()
{
return STATS;
}
public double[] getMemberDescriptorsRateStats() throws ExecutionException {
CacheStats cacheStats = this.memberCache.stats();
return new double[] {
cacheStats.hitRate(), cacheStats.loadExceptionRate(), cacheStats.missRate(),
};
}
@Override
public CacheStats getCacheStats(){
return cache.stats();
}
@Override
public CacheStats getCacheStats() {
// TODO Auto-generated method stub
return null;
}
/** {@inheritDoc} */
@Override
public CacheStatistics getStatistics() {
CacheStats stats = backend.stats();
return new CacheStatistics(backend.size(), stats.hitCount(), stats.missCount());
}
public FilterJoinCacheStats(long size, CacheStats stats) {
this.cacheStats = stats;
this.size = size;
}
public CacheStats getCacheStats() {
return cacheStats;
}