Class CacheMetrics

java.lang.Object
cloud.opencode.base.cache.CacheMetrics

public final class CacheMetrics extends Object
Cache Metrics - Detailed latency tracking with percentile calculations 缓存指标 - 带百分位数计算的详细延迟跟踪

Provides comprehensive metrics including P50, P95, P99 latency tracking for cache operations using HDR histogram approximation.

使用 HDR 直方图近似提供包括 P50、P95、P99 延迟跟踪在内的综合指标。

Features | 主要功能:

  • Percentile latency tracking (P50/P95/P99) - 百分位延迟跟踪
  • Min/Max/Mean latency - 最小/最大/平均延迟
  • Throughput calculation - 吞吐量计算
  • Operation-specific metrics - 操作特定指标
  • Thread-safe concurrent recording - 线程安全并发记录

Usage Examples | 使用示例:

CacheMetrics metrics = CacheMetrics.create();

// Record operation latency - 记录操作延迟
long start = System.nanoTime();
cache.get(key);
metrics.recordGetLatency(System.nanoTime() - start);

// Get percentiles - 获取百分位数
long p50 = metrics.getLatencyP50();
long p99 = metrics.getLatencyP99();

// Get snapshot - 获取快照
MetricsSnapshot snapshot = metrics.snapshot();

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Memory-bounded: Yes (fixed histogram size) - 内存有界: 是(固定直方图大小)
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Immutable metrics snapshot 不可变指标快照
  • Method Summary

    Modifier and Type
    Method
    Description
    Create new metrics instance 创建新的指标实例
    double
    Get average get latency in nanoseconds 获取平均获取延迟(纳秒)
    double
    Get average load latency in nanoseconds 获取平均加载延迟(纳秒)
    double
    Get average put latency in nanoseconds 获取平均放入延迟(纳秒)
    long
    Get total eviction count 获取总淘汰数
    long
    Get total get operation count 获取总获取操作数
    long
    Get P50 (median) get latency in nanoseconds 获取 P50(中位数)获取延迟(纳秒)
    long
    Get P95 get latency in nanoseconds 获取 P95 获取延迟(纳秒)
    long
    Get P99 get latency in nanoseconds 获取 P99 获取延迟(纳秒)
    double
    Get operations per second for get operations 获取获取操作的每秒操作数
    long
    Get total load operation count 获取总加载操作数
    long
    Get P50 (median) load latency in nanoseconds 获取 P50(中位数)加载延迟(纳秒)
    long
    Get P95 load latency in nanoseconds 获取 P95 加载延迟(纳秒)
    long
    Get P99 load latency in nanoseconds 获取 P99 加载延迟(纳秒)
    long
    Get maximum get latency in nanoseconds 获取最大获取延迟(纳秒)
    long
    Get minimum get latency in nanoseconds 获取最小获取延迟(纳秒)
    long
    Get total put operation count 获取总放入操作数
    long
    Get P50 (median) put latency in nanoseconds 获取 P50(中位数)放入延迟(纳秒)
    long
    Get P95 put latency in nanoseconds 获取 P95 放入延迟(纳秒)
    long
    Get P99 put latency in nanoseconds 获取 P99 放入延迟(纳秒)
    double
    Get operations per second for put operations 获取放入操作的每秒操作数
    Get uptime duration 获取运行时间
    void
    Record eviction 记录淘汰
    void
    recordGetLatency(long latencyNanos)
    Record get operation latency 记录获取操作延迟
    void
    recordLoadLatency(long latencyNanos)
    Record load operation latency 记录加载操作延迟
    void
    recordPutLatency(long latencyNanos)
    Record put operation latency 记录放入操作延迟
    void
    Reset all metrics 重置所有指标
    Create immutable snapshot of current metrics 创建当前指标的不可变快照

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static CacheMetrics create()
      Create new metrics instance 创建新的指标实例
      Returns:
      new metrics | 新指标
    • recordGetLatency

      public void recordGetLatency(long latencyNanos)
      Record get operation latency 记录获取操作延迟
      Parameters:
      latencyNanos - latency in nanoseconds | 纳秒延迟
    • recordPutLatency

      public void recordPutLatency(long latencyNanos)
      Record put operation latency 记录放入操作延迟
      Parameters:
      latencyNanos - latency in nanoseconds | 纳秒延迟
    • recordLoadLatency

      public void recordLoadLatency(long latencyNanos)
      Record load operation latency 记录加载操作延迟
      Parameters:
      latencyNanos - latency in nanoseconds | 纳秒延迟
    • recordEviction

      public void recordEviction()
      Record eviction 记录淘汰
    • getGetCount

      public long getGetCount()
      Get total get operation count 获取总获取操作数
      Returns:
      get count | 获取数
    • getPutCount

      public long getPutCount()
      Get total put operation count 获取总放入操作数
      Returns:
      put count | 放入数
    • getLoadCount

      public long getLoadCount()
      Get total load operation count 获取总加载操作数
      Returns:
      load count | 加载数
    • getEvictionCount

      public long getEvictionCount()
      Get total eviction count 获取总淘汰数
      Returns:
      eviction count | 淘汰数
    • getGetLatencyP50

      public long getGetLatencyP50()
      Get P50 (median) get latency in nanoseconds 获取 P50(中位数)获取延迟(纳秒)
      Returns:
      P50 latency | P50 延迟
    • getGetLatencyP95

      public long getGetLatencyP95()
      Get P95 get latency in nanoseconds 获取 P95 获取延迟(纳秒)
      Returns:
      P95 latency | P95 延迟
    • getGetLatencyP99

      public long getGetLatencyP99()
      Get P99 get latency in nanoseconds 获取 P99 获取延迟(纳秒)
      Returns:
      P99 latency | P99 延迟
    • getPutLatencyP50

      public long getPutLatencyP50()
      Get P50 (median) put latency in nanoseconds 获取 P50(中位数)放入延迟(纳秒)
      Returns:
      P50 latency | P50 延迟
    • getPutLatencyP95

      public long getPutLatencyP95()
      Get P95 put latency in nanoseconds 获取 P95 放入延迟(纳秒)
      Returns:
      P95 latency | P95 延迟
    • getPutLatencyP99

      public long getPutLatencyP99()
      Get P99 put latency in nanoseconds 获取 P99 放入延迟(纳秒)
      Returns:
      P99 latency | P99 延迟
    • getLoadLatencyP50

      public long getLoadLatencyP50()
      Get P50 (median) load latency in nanoseconds 获取 P50(中位数)加载延迟(纳秒)
      Returns:
      P50 latency | P50 延迟
    • getLoadLatencyP95

      public long getLoadLatencyP95()
      Get P95 load latency in nanoseconds 获取 P95 加载延迟(纳秒)
      Returns:
      P95 latency | P95 延迟
    • getLoadLatencyP99

      public long getLoadLatencyP99()
      Get P99 load latency in nanoseconds 获取 P99 加载延迟(纳秒)
      Returns:
      P99 latency | P99 延迟
    • getAverageGetLatency

      public double getAverageGetLatency()
      Get average get latency in nanoseconds 获取平均获取延迟(纳秒)
      Returns:
      average latency | 平均延迟
    • getAveragePutLatency

      public double getAveragePutLatency()
      Get average put latency in nanoseconds 获取平均放入延迟(纳秒)
      Returns:
      average latency | 平均延迟
    • getAverageLoadLatency

      public double getAverageLoadLatency()
      Get average load latency in nanoseconds 获取平均加载延迟(纳秒)
      Returns:
      average latency | 平均延迟
    • getMinGetLatency

      public long getMinGetLatency()
      Get minimum get latency in nanoseconds 获取最小获取延迟(纳秒)
      Returns:
      min latency | 最小延迟
    • getMaxGetLatency

      public long getMaxGetLatency()
      Get maximum get latency in nanoseconds 获取最大获取延迟(纳秒)
      Returns:
      max latency | 最大延迟
    • getGetThroughput

      public double getGetThroughput()
      Get operations per second for get operations 获取获取操作的每秒操作数
      Returns:
      ops/sec | 操作/秒
    • getPutThroughput

      public double getPutThroughput()
      Get operations per second for put operations 获取放入操作的每秒操作数
      Returns:
      ops/sec | 操作/秒
    • getUptime

      public Duration getUptime()
      Get uptime duration 获取运行时间
      Returns:
      uptime | 运行时间
    • snapshot

      public CacheMetrics.MetricsSnapshot snapshot()
      Create immutable snapshot of current metrics 创建当前指标的不可变快照
      Returns:
      metrics snapshot | 指标快照
    • reset

      public void reset()
      Reset all metrics 重置所有指标