LCOV - code coverage report
Current view: top level - server/cache - CacheDisplay.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 46 47 97.9 %
Date: 2022-11-19 15:00:39 Functions: 9 9 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.base.Strings;
      18             : import java.io.IOException;
      19             : import java.io.Writer;
      20             : import java.util.Collection;
      21             : 
      22             : public class CacheDisplay {
      23             : 
      24             :   private final Writer stdout;
      25             :   private final int nw;
      26             :   private final Collection<CacheInfo> caches;
      27             : 
      28          15 :   public CacheDisplay(Writer stdout, int nw, Collection<CacheInfo> caches) {
      29          15 :     this.stdout = stdout;
      30          15 :     this.nw = nw;
      31          15 :     this.caches = caches;
      32          15 :   }
      33             : 
      34             :   public CacheDisplay(Writer stdout, Collection<CacheInfo> caches) {
      35          15 :     this(stdout, 30, caches);
      36          15 :   }
      37             : 
      38             :   public void displayCaches() throws IOException {
      39          15 :     stdout.write(
      40          15 :         String.format( //
      41             :             "%1s %-" + nw + "s|%-21s|  %-5s |%-9s|\n" //
      42             :             ,
      43             :             "" //
      44             :             ,
      45             :             "Name" //
      46             :             ,
      47             :             "Entries" //
      48             :             ,
      49             :             "AvgGet" //
      50             :             ,
      51             :             "Hit Ratio" //
      52             :             ));
      53          15 :     stdout.write(
      54          15 :         String.format( //
      55             :             "%1s %-" + nw + "s|%6s %6s %7s|  %-5s  |%-4s %-4s|\n" //
      56             :             ,
      57             :             "" //
      58             :             ,
      59             :             "" //
      60             :             ,
      61             :             "Mem" //
      62             :             ,
      63             :             "Disk" //
      64             :             ,
      65             :             "Space" //
      66             :             ,
      67             :             "" //
      68             :             ,
      69             :             "Mem" //
      70             :             ,
      71             :             "Disk" //
      72             :             ));
      73          15 :     stdout.write("--");
      74          15 :     for (int i = 0; i < nw; i++) {
      75          15 :       stdout.write('-');
      76             :     }
      77          15 :     stdout.write("+---------------------+---------+---------+\n");
      78          15 :     printMemoryCoreCaches(caches);
      79          15 :     printMemoryPluginCaches(caches);
      80          15 :     printDiskCaches(caches);
      81          15 :     stdout.write('\n');
      82          15 :   }
      83             : 
      84             :   private void printMemoryCoreCaches(Collection<CacheInfo> caches) throws IOException {
      85          15 :     for (CacheInfo cache : caches) {
      86          15 :       if (!cache.name.contains("-") && CacheInfo.CacheType.MEM.equals(cache.type)) {
      87          15 :         printCache(cache);
      88             :       }
      89          15 :     }
      90          15 :   }
      91             : 
      92             :   private void printMemoryPluginCaches(Collection<CacheInfo> caches) throws IOException {
      93          15 :     for (CacheInfo cache : caches) {
      94          15 :       if (cache.name.contains("-") && CacheInfo.CacheType.MEM.equals(cache.type)) {
      95           0 :         printCache(cache);
      96             :       }
      97          15 :     }
      98          15 :   }
      99             : 
     100             :   private void printDiskCaches(Collection<CacheInfo> caches) throws IOException {
     101          15 :     for (CacheInfo cache : caches) {
     102          15 :       if (CacheInfo.CacheType.DISK.equals(cache.type)) {
     103          15 :         printCache(cache);
     104             :       }
     105          15 :     }
     106          15 :   }
     107             : 
     108             :   private void printCache(CacheInfo cache) throws IOException {
     109          15 :     stdout.write(
     110          15 :         String.format(
     111             :             "%1s %-" + nw + "s|%6s %6s %7s| %7s |%4s %4s|\n",
     112          15 :             CacheInfo.CacheType.DISK.equals(cache.type) ? "D" : "",
     113             :             cache.name,
     114          15 :             nullToEmpty(cache.entries.mem),
     115          15 :             nullToEmpty(cache.entries.disk),
     116          15 :             Strings.nullToEmpty(cache.entries.space),
     117          15 :             Strings.nullToEmpty(cache.averageGet),
     118          15 :             formatAsPercent(cache.hitRatio.mem),
     119          15 :             formatAsPercent(cache.hitRatio.disk)));
     120          15 :   }
     121             : 
     122             :   private static String nullToEmpty(Long l) {
     123          15 :     return l != null ? String.valueOf(l) : "";
     124             :   }
     125             : 
     126             :   private static String formatAsPercent(Integer i) {
     127          15 :     return i != null ? i + "%" : "";
     128             :   }
     129             : }

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