Example usage for com.google.common.cache Cache stats

List of usage examples for com.google.common.cache Cache stats

Introduction

In this page you can find the example usage for com.google.common.cache Cache stats.

Prototype

CacheStats stats();

Source Link

Document

Returns a current snapshot of this cache's cumulative statistics.

Usage

From source file:org.nuxeo.ecm.core.storage.dbs.GuavaCacheMetric.java

public static MetricSet of(Cache cache, String name, String... names) {
    String basicName = MetricRegistry.name(name, names);

    GuavaCacheMetric metrics = new GuavaCacheMetric();
    metrics.putMetrics(() -> cache.stats().averageLoadPenalty(), basicName, "average", "load", "penalty");
    metrics.putMetrics(() -> cache.stats().evictionCount(), basicName, "eviction", "count");
    metrics.putMetrics(() -> cache.stats().hitCount(), basicName, "hit", "count");
    metrics.putMetrics(() -> cache.stats().hitRate(), basicName, "hit", "rate");
    metrics.putMetrics(() -> cache.stats().loadCount(), basicName, "load", "count");
    metrics.putMetrics(() -> cache.stats().loadExceptionCount(), basicName, "load", "exception", "count");
    metrics.putMetrics(() -> cache.stats().loadExceptionRate(), basicName, "load", "exception", "rate");
    metrics.putMetrics(() -> cache.stats().loadSuccessCount(), basicName, "load", "success", "count");
    metrics.putMetrics(() -> cache.stats().missCount(), basicName, "miss", "count");
    metrics.putMetrics(() -> cache.stats().missRate(), basicName, "miss", "rate");
    metrics.putMetrics(() -> cache.stats().requestCount(), basicName, "request", "count");
    metrics.putMetrics(() -> cache.stats().totalLoadTime(), basicName, "total", "load", "time");
    return metrics;
}

From source file:org.springframework.metrics.instrument.internal.AbstractMeterRegistry.java

@Override
public Cache monitor(String name, Stream<Tag> tags, Cache cache) {
    CacheStats stats = cache.stats();

    gauge(name + "_size", tags, cache, Cache::size);
    gauge(name + "_hit_total", tags, stats, CacheStats::hitCount);
    gauge(name + "_miss_total", tags, stats, CacheStats::missCount);
    gauge(name + "_requests_total", tags, stats, CacheStats::requestCount);
    gauge(name + "_eviction_total", tags, stats, CacheStats::evictionCount);
    gauge(name + "_load_duration", tags, stats, CacheStats::totalLoadTime);

    if (cache instanceof LoadingCache) {
        gauge(name + "_loads_total", tags, stats, CacheStats::loadCount);
        gauge(name + "_load_failure_total", tags, stats, CacheStats::loadExceptionCount);
    }//ww w .  j  a v  a  2 s. c o m

    return cache;
}

From source file:com.google.gerrit.server.cache.CacheMetrics.java

@Inject
public CacheMetrics(MetricMaker metrics, DynamicMap<Cache<?, ?>> cacheMap) {
    Field<String> F_NAME = Field.ofString("cache_name");

    CallbackMetric1<String, Long> memEnt = metrics.newCallbackMetric("caches/memory_cached", Long.class,
            new Description("Memory entries").setGauge().setUnit("entries"), F_NAME);
    CallbackMetric1<String, Double> memHit = metrics.newCallbackMetric("caches/memory_hit_ratio", Double.class,
            new Description("Memory hit ratio").setGauge().setUnit("percent"), F_NAME);
    CallbackMetric1<String, Long> memEvict = metrics.newCallbackMetric("caches/memory_eviction_count",
            Long.class, new Description("Memory eviction count").setGauge().setUnit("evicted entries"), F_NAME);
    CallbackMetric1<String, Long> perDiskEnt = metrics.newCallbackMetric("caches/disk_cached", Long.class,
            new Description("Disk entries used by persistent cache").setGauge().setUnit("entries"), F_NAME);
    CallbackMetric1<String, Double> perDiskHit = metrics.newCallbackMetric("caches/disk_hit_ratio",
            Double.class, new Description("Disk hit ratio for persistent cache").setGauge().setUnit("percent"),
            F_NAME);/*from  w  w  w .j  a v  a  2 s.  c o  m*/

    Set<CallbackMetric<?>> cacheMetrics = ImmutableSet.<CallbackMetric<?>>of(memEnt, memHit, memEvict,
            perDiskEnt, perDiskHit);

    metrics.newTrigger(cacheMetrics, () -> {
        for (DynamicMap.Entry<Cache<?, ?>> e : cacheMap) {
            Cache<?, ?> c = e.getProvider().get();
            String name = metricNameOf(e);
            CacheStats cstats = c.stats();
            memEnt.set(name, c.size());
            memHit.set(name, cstats.hitRate() * 100);
            memEvict.set(name, cstats.evictionCount());
            if (c instanceof PersistentCache) {
                PersistentCache.DiskStats d = ((PersistentCache) c).diskStats();
                perDiskEnt.set(name, d.size());
                perDiskHit.set(name, hitRatio(d));
            }
        }
        cacheMetrics.forEach(CallbackMetric::prune);
    });
}

From source file:com.netflix.servo.monitor.MonitoredCache.java

MonitoredCache(final Cache<?, ?> cache) {
    final Supplier<CacheStats> supplier = new Supplier<CacheStats>() {
        public CacheStats get() {
            return cache.stats();
        }/*from w ww. j  av  a 2 s  .c o m*/
    };
    statsSupplier = Suppliers.memoizeWithExpiration(supplier, CACHE_TIME, TimeUnit.SECONDS);
}

From source file:org.graylog2.metrics.CacheStatsSet.java

public CacheStatsSet(final String prefix, final Cache cache) {
    this.metrics = ImmutableMap.<String, Metric>builder().put(name(prefix, "requests"), new Gauge<Long>() {
        @Override/*from w w w . jav  a2s.  com*/
        public Long getValue() {
            return cache.stats().requestCount();
        }
    }).put(name(prefix, "hits"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.stats().hitCount();
        }
    }).put(name(prefix, "misses"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.stats().missCount();
        }
    }).put(name(prefix, "evictions"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.stats().evictionCount();
        }
    }).put(name(prefix, "total-load-time-ns"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.stats().totalLoadTime();
        }
    }).put(name(prefix, "load-successes"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.stats().loadSuccessCount();
        }
    }).put(name(prefix, "load-exceptions"), new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.stats().loadExceptionCount();
        }
    }).put(name(prefix, "hit-rate"), new Gauge<Double>() {
        @Override
        public Double getValue() {
            return cache.stats().hitRate();
        }
    }).put(name(prefix, "miss-rate"), new Gauge<Double>() {
        @Override
        public Double getValue() {
            return cache.stats().missRate();
        }
    }).build();
}

From source file:rickbw.incubator.cache.MultiCache.java

@Override
public final CacheStats stats() {
    long hitCount = 0L;
    long missCount = 0L;
    long loadSuccessCount = 0L;
    long loadExceptionCount = 0L;
    long totalLoadTime = 0L;
    long evictionCount = 0L;

    for (final Cache<?, ?> delegate : this.delegates.values()) {
        final CacheStats stats = delegate.stats();
        hitCount += stats.hitCount();/*from  w ww  . j a va  2  s . c  om*/
        missCount += stats.missCount();
        loadSuccessCount += stats.loadSuccessCount();
        loadExceptionCount += stats.loadExceptionCount();
        totalLoadTime += stats.totalLoadTime();
        evictionCount += stats.evictionCount();
    }

    return new CacheStats(hitCount, missCount, loadSuccessCount, loadExceptionCount, totalLoadTime,
            evictionCount);
}

From source file:org.gradle.api.internal.changedetection.state.InMemoryTaskArtifactCache.java

private Cache<Object, Object> loadData(String cacheId, String cacheName) {
    Cache<Object, Object> theData;
    synchronized (lock) {
        theData = this.cache.getIfPresent(cacheId);
        if (theData != null) {
            LOG.info("In-memory cache of {}: Size{{}}, {}", cacheId, theData.size(), theData.stats());
        } else {/*w ww .  ja  v  a 2 s. c  o m*/
            Integer maxSize = CACHE_CAPS.get(cacheName);
            assert maxSize != null : "Unknown cache.";
            LOG.info("Creating In-memory cache of {}: MaxSize{{}}", cacheId, maxSize);
            LoggingEvictionListener evictionListener = new LoggingEvictionListener(cacheId, maxSize);
            theData = CacheBuilder.newBuilder().maximumSize(maxSize).recordStats()
                    .removalListener(evictionListener).build();
            evictionListener.setCache(theData);
            this.cache.put(cacheId, theData);
        }
    }
    return theData;
}

From source file:com.attribyte.essem.metrics.GuavaCacheMetrics.java

/**
 * Create metrics for a cache.//from   w ww.  j  a va 2  s .com
 * @param cache The cache.
 */
public GuavaCacheMetrics(final Cache cache) {

    final Gauge<Long> sizeGauge = new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.size();
        }
    };

    final Gauge<Double> hitRatioGauge = new Gauge<Double>() {
        @Override
        public Double getValue() {
            return cache.stats().hitRate();
        }
    };

    final Gauge<Long> loadExceptions = new Gauge<Long>() {
        @Override
        public Long getValue() {
            return cache.stats().loadExceptionCount();
        }
    };

    final Gauge<Double> loadPenaltyGauge = new Gauge<Double>() {
        @Override
        public Double getValue() {
            return cache.stats().averageLoadPenalty();
        }
    };

    this.metrics = ImmutableMap.<String, Metric>builder().put("size", sizeGauge).put("hit-ratio", hitRatioGauge)
            .put("load-exceptions", loadExceptions).put("load-penalty", loadPenaltyGauge).build();
}

From source file:com.devnexus.ting.web.controller.admin.AdminController.java

@RequestMapping("/s/admin/{eventKey}/index")
public String getSpeakersForEvent(@PathVariable("eventKey") String eventKey, Model model) {
    final Event event = businessService.getEventByEventKey(eventKey);
    model.addAttribute("event", event);
    model.addAttribute("events", businessService.getAllEventsOrderedByName());

    final Collection<String> cacheNames = cacheManager.getCacheNames();
    final Map<String, CacheStats> cacheStats = new HashMap<>();

    for (String cacheName : cacheNames) {
        Cache c = (Cache) cacheManager.getCache(cacheName).getNativeCache();
        LOGGER.warn("Cache Stats for '{}' : {}", cacheName, c.stats().toString());
        cacheStats.put(cacheName, c.stats());
    }/* w ww.  j  a v a2 s  . com*/
    model.addAttribute("cacheStats", cacheStats);
    return "/admin/index";
}

From source file:org.springframework.metrics.instrument.MeterRegistry.java

/**
 * Record metrics on Guava caches.//from  ww  w .  j  a  v a2 s  .  com
 *
 * @param name  The name prefix of the metrics.
 * @param tags  Tags to apply to all recorded metrics.
 * @param cache The cache to instrument.
 * @return The instrumented cache, unchanged. The original cache is not
 * wrapped or proxied in any way.
 * @see com.google.common.cache.CacheStats
 */
default Cache monitor(String name, Stream<Tag> tags, Cache cache) {
    CacheStats stats = cache.stats();

    gauge(name + "_size", tags, cache, Cache::size);
    gauge(name + "_hit_total", tags, stats, CacheStats::hitCount);
    gauge(name + "_miss_total", tags, stats, CacheStats::missCount);
    gauge(name + "_requests_total", tags, stats, CacheStats::requestCount);
    gauge(name + "_eviction_total", tags, stats, CacheStats::evictionCount);
    gauge(name + "_load_duration", tags, stats, CacheStats::totalLoadTime);

    if (cache instanceof LoadingCache) {
        gauge(name + "_loads_total", tags, stats, CacheStats::loadCount);
        gauge(name + "_load_failure_total", tags, stats, CacheStats::loadExceptionCount);
    }

    return cache;
}