StatLogger.java 6.51 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.cache.clustering.impl.CacheServiceClientStatistics
 *  com.scene7.is.cache.clustering.impl.CacheServiceClientStatisticsCounter
 *  com.scene7.is.cache.clustering.impl.CacheServiceClientStatisticsSnapshot
 *  com.scene7.is.cache.server.access.CacheServerAccessBean
 *  com.scene7.is.cacheserver.shared.CacheServerConnection
 *  com.scene7.is.util.Statistics
 *  com.scene7.is.util.StatisticsSnapshot
 *  com.scene7.is.util.diskcache.Cache
 *  org.jetbrains.annotations.NotNull
 */
package com.scene7.is.ps.provider.logging;

import com.scene7.is.cache.clustering.impl.CacheServiceClientStatistics;
import com.scene7.is.cache.clustering.impl.CacheServiceClientStatisticsCounter;
import com.scene7.is.cache.clustering.impl.CacheServiceClientStatisticsSnapshot;
import com.scene7.is.cache.server.access.CacheServerAccessBean;
import com.scene7.is.cacheserver.shared.CacheServerConnection;
import com.scene7.is.util.Statistics;
import com.scene7.is.util.StatisticsSnapshot;
import com.scene7.is.util.diskcache.Cache;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;

public class StatLogger
implements Runnable {
    private static final Logger LOGGER = Logger.getLogger(StatLogger.class.getName());
    private static final Runtime RUNTIME = Runtime.getRuntime();
    private final List<Statistics> statistics;
    private final Cache cache;
    private final CacheServiceClientStatistics cacheServiceStats;
    private final CacheServerAccessBean cacheServerAccessBean;

    public StatLogger(@NotNull List<Statistics> statistics, @NotNull Cache cache, @NotNull CacheServiceClientStatistics cacheServiceStats, @NotNull CacheServerAccessBean cacheServerAccessBean) {
        this.statistics = statistics;
        this.cache = cache;
        this.cacheServiceStats = cacheServiceStats;
        this.cacheServerAccessBean = cacheServerAccessBean;
    }

    @Override
    public void run() {
        long currentTime = System.currentTimeMillis();
        LOGGER.info("Memory:        size: " + RUNTIME.totalMemory() + " free: " + RUNTIME.freeMemory());
        LOGGER.info("Cache:         size: " + this.cache.getSize() + " count: " + this.cache.getCount());
        for (Statistics statistics : this.statistics) {
            this.logStatistics(currentTime, statistics);
        }
        this.logCacheClusterStatistics();
        this.logCacheServerStatistics();
    }

    private void logStatistics(long currentTime, @NotNull Statistics statistics) {
        StatisticsSnapshot snapshot = statistics.getSnapshot(currentTime, true);
        String statusDistribution = StatLogger.formatDistribution(snapshot.getStatusCounters());
        String mimeTypeDistribution = StatLogger.formatDistribution(snapshot.getMimeTypeCounters());
        String cacheUseDistribution = StatLogger.formatDistribution(snapshot.getCacheUseCounters());
        LOGGER.info(statistics.getName() + " statistics: ");
        LOGGER.info("Bytes served:  total: " + snapshot.getTotalBytesServed() + " max: " + snapshot.getMaxBytesServed());
        LOGGER.info("Request count: total : " + snapshot.getTotalRequestsServed() + " overlap: " + snapshot.getMaxRequestOverlap());
        LOGGER.info("Request time:  total: " + snapshot.getTotalRequestTime() + " max: " + snapshot.getMaxRequestTime() + " elapsed: " + snapshot.getElapsedTime() + " idle: " + snapshot.getTotalIdleTime());
        if (statusDistribution.length() > 0) {
            LOGGER.info("HTTP statuses: " + statusDistribution);
        }
        if (mimeTypeDistribution.length() > 0) {
            LOGGER.info("Mime types :   " + mimeTypeDistribution);
        }
        if (cacheUseDistribution.length() > 0) {
            LOGGER.info("Cache use:     " + cacheUseDistribution);
        }
    }

    private static <T> String formatDistribution(Map<T, Long> counters) {
        StringBuilder buffer = new StringBuilder();
        for (Map.Entry<T, Long> entry : counters.entrySet()) {
            buffer.append(entry.getKey()).append(": ").append(entry.getValue()).append(' ');
        }
        return buffer.toString();
    }

    private void logCacheClusterStatistics() {
        CacheServiceClientStatisticsSnapshot snapshot = this.cacheServiceStats.getSnapshot(true);
        String queryResponseDistribution = StatLogger.formatCacheClusterDistribution(snapshot.getQueryResponse());
        String fetchMissDistribution = StatLogger.formatCacheClusterDistribution(snapshot.getFetchMiss());
        String fetchHitDistribution = StatLogger.formatCacheClusterDistribution(snapshot.getFetchHit());
        String fetchErrorDistribution = StatLogger.formatCacheClusterDistribution(snapshot.getFetchError());
        LOGGER.info("Cache Cluster Client statistics: ");
        LOGGER.info("Query broadcast count:  total: " + snapshot.getTotalQueryBroadcasts());
        LOGGER.info("Query responses:        " + queryResponseDistribution);
        LOGGER.info("Fetch count:            total: " + snapshot.getTotalFetches());
        LOGGER.info("Fetch hits:             " + fetchHitDistribution);
        LOGGER.info("Fetch misses:           " + fetchMissDistribution);
        LOGGER.info("Fetch errors:           " + fetchErrorDistribution);
        LOGGER.info("Timed out count:        " + snapshot.getTotalTimedOut());
    }

    private static String formatCacheClusterDistribution(@NotNull CacheServiceClientStatisticsCounter counter) {
        StringBuilder buffer = new StringBuilder();
        buffer.append("count: ").append(counter.getCount());
        buffer.append(" total: ").append(counter.getTotalTime());
        buffer.append(" max: ").append(counter.getMaxTime());
        return buffer.toString();
    }

    private void logCacheServerStatistics() {
        List serverConnectionList = this.cacheServerAccessBean.getCacheServerListSnapshot(true);
        LOGGER.info("Cache Server statistics:");
        for (CacheServerConnection serverConnection : serverConnectionList) {
            StringBuilder buffer = new StringBuilder();
            buffer.append("server: ").append(serverConnection.getServerUrl());
            buffer.append(" connections: ").append(serverConnection.getNumConnections());
            buffer.append(" cache hits: ").append(serverConnection.getCacheHits());
            buffer.append(" cache misses: ").append(serverConnection.getCacheMisses());
            buffer.append(" cache creates: ").append(serverConnection.getCacheCreates());
            LOGGER.info(buffer.toString());
        }
    }
}