LCOV - code coverage report
Current view: top level - server/cache - CacheInfo.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 53 59 89.8 %
Date: 2022-11-19 15:00:39 Functions: 13 13 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2021 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.cache;
      16             : 
      17             : import com.google.common.cache.Cache;
      18             : import com.google.common.cache.CacheStats;
      19             : import com.google.gerrit.common.Nullable;
      20             : 
      21             : public class CacheInfo {
      22             : 
      23             :   public String name;
      24             :   public CacheType type;
      25             :   public EntriesInfo entries;
      26             :   public String averageGet;
      27             :   public HitRatioInfo hitRatio;
      28             : 
      29             :   public CacheInfo(Cache<?, ?> cache) {
      30           2 :     this(null, cache);
      31           2 :   }
      32             : 
      33          17 :   public CacheInfo(String name, Cache<?, ?> cache) {
      34          17 :     this.name = name;
      35             : 
      36          17 :     CacheStats stat = cache.stats();
      37             : 
      38          17 :     entries = new EntriesInfo();
      39          17 :     entries.setMem(cache.size());
      40             : 
      41          17 :     averageGet = duration(stat.averageLoadPenalty());
      42             : 
      43          17 :     hitRatio = new HitRatioInfo();
      44          17 :     hitRatio.setMem(stat.hitCount(), stat.requestCount());
      45             : 
      46          17 :     if (cache instanceof PersistentCache) {
      47          15 :       type = CacheType.DISK;
      48          15 :       PersistentCache.DiskStats diskStats = ((PersistentCache) cache).diskStats();
      49          15 :       entries.setDisk(diskStats.size());
      50          15 :       entries.setSpace(diskStats.space());
      51          15 :       hitRatio.setDisk(diskStats.hitCount(), diskStats.requestCount());
      52          15 :     } else {
      53          17 :       type = CacheType.MEM;
      54             :     }
      55          17 :   }
      56             : 
      57             :   @Nullable
      58             :   private static String duration(double ns) {
      59          17 :     if (ns < 0.5) {
      60          17 :       return null;
      61             :     }
      62          17 :     String suffix = "ns";
      63          17 :     if (ns >= 1000.0) {
      64          17 :       ns /= 1000.0;
      65          17 :       suffix = "us";
      66             :     }
      67          17 :     if (ns >= 1000.0) {
      68          17 :       ns /= 1000.0;
      69          17 :       suffix = "ms";
      70             :     }
      71          17 :     if (ns >= 1000.0) {
      72           0 :       ns /= 1000.0;
      73           0 :       suffix = "s";
      74             :     }
      75          17 :     return String.format("%4.1f%s", ns, suffix).trim();
      76             :   }
      77             : 
      78          17 :   public static class EntriesInfo {
      79             :     public Long mem;
      80             :     public Long disk;
      81             :     public String space;
      82             : 
      83             :     public void setMem(long mem) {
      84          17 :       this.mem = mem != 0 ? mem : null;
      85          17 :     }
      86             : 
      87             :     public void setDisk(long disk) {
      88          15 :       this.disk = disk != 0 ? disk : null;
      89          15 :     }
      90             : 
      91             :     public void setSpace(double value) {
      92          15 :       space = bytes(value);
      93          15 :     }
      94             : 
      95             :     private static String bytes(double value) {
      96          15 :       value /= 1024;
      97          15 :       String suffix = "k";
      98             : 
      99          15 :       if (value > 1024) {
     100           0 :         value /= 1024;
     101           0 :         suffix = "m";
     102             :       }
     103          15 :       if (value > 1024) {
     104           0 :         value /= 1024;
     105           0 :         suffix = "g";
     106             :       }
     107          15 :       return String.format("%1$6.2f%2$s", value, suffix).trim();
     108             :     }
     109             :   }
     110             : 
     111          17 :   public static class HitRatioInfo {
     112             :     public Integer mem;
     113             :     public Integer disk;
     114             : 
     115             :     public void setMem(long value, long total) {
     116          17 :       mem = percent(value, total);
     117          17 :     }
     118             : 
     119             :     public void setDisk(long value, long total) {
     120          15 :       disk = percent(value, total);
     121          15 :     }
     122             : 
     123             :     @Nullable
     124             :     private static Integer percent(long value, long total) {
     125          17 :       if (total <= 0) {
     126          17 :         return null;
     127             :       }
     128          17 :       return (int) ((100 * value) / total);
     129             :     }
     130             :   }
     131             : 
     132          17 :   public enum CacheType {
     133          17 :     MEM,
     134          17 :     DISK
     135             :   }
     136             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750