Home | History | Annotate | Download | only in cache
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.cache;
     18 
     19 import junit.framework.TestCase;
     20 
     21 /**
     22  * Unit test for {@link CacheStats}.
     23  *
     24  * @author Charles Fry
     25  */
     26 public class CacheStatsTest extends TestCase {
     27 
     28   public void testEmpty() {
     29     CacheStats stats = new CacheStats(0, 0, 0, 0, 0, 0);
     30     assertEquals(0, stats.requestCount());
     31     assertEquals(0, stats.hitCount());
     32     assertEquals(1.0, stats.hitRate());
     33     assertEquals(0, stats.missCount());
     34     assertEquals(0.0, stats.missRate());
     35     assertEquals(0, stats.loadSuccessCount());
     36     assertEquals(0, stats.loadExceptionCount());
     37     assertEquals(0.0, stats.loadExceptionRate());
     38     assertEquals(0, stats.loadCount());
     39     assertEquals(0, stats.totalLoadTime());
     40     assertEquals(0.0, stats.averageLoadPenalty());
     41     assertEquals(0, stats.evictionCount());
     42   }
     43 
     44   public void testSingle() {
     45     CacheStats stats = new CacheStats(11, 13, 17, 19, 23, 27);
     46     assertEquals(24, stats.requestCount());
     47     assertEquals(11, stats.hitCount());
     48     assertEquals(11.0/24, stats.hitRate());
     49     assertEquals(13, stats.missCount());
     50     assertEquals(13.0/24, stats.missRate());
     51     assertEquals(17, stats.loadSuccessCount());
     52     assertEquals(19, stats.loadExceptionCount());
     53     assertEquals(19.0/36, stats.loadExceptionRate());
     54     assertEquals(17 + 19, stats.loadCount());
     55     assertEquals(23, stats.totalLoadTime());
     56     assertEquals(23.0/(17 + 19), stats.averageLoadPenalty());
     57     assertEquals(27, stats.evictionCount());
     58   }
     59 
     60   public void testMinus() {
     61     CacheStats one = new CacheStats(11, 13, 17, 19, 23, 27);
     62     CacheStats two = new CacheStats(53, 47, 43, 41, 37, 31);
     63 
     64     CacheStats diff = two.minus(one);
     65     assertEquals(76, diff.requestCount());
     66     assertEquals(42, diff.hitCount());
     67     assertEquals(42.0/76, diff.hitRate());
     68     assertEquals(34, diff.missCount());
     69     assertEquals(34.0/76, diff.missRate());
     70     assertEquals(26, diff.loadSuccessCount());
     71     assertEquals(22, diff.loadExceptionCount());
     72     assertEquals(22.0/48, diff.loadExceptionRate());
     73     assertEquals(26 + 22, diff.loadCount());
     74     assertEquals(14, diff.totalLoadTime());
     75     assertEquals(14.0/(26 + 22), diff.averageLoadPenalty());
     76     assertEquals(4, diff.evictionCount());
     77 
     78     assertEquals(new CacheStats(0, 0, 0, 0, 0, 0), one.minus(two));
     79   }
     80 
     81   public void testPlus() {
     82     CacheStats one = new CacheStats(11, 13, 15, 13, 11, 9);
     83     CacheStats two = new CacheStats(53, 47, 41, 39, 37, 35);
     84 
     85     CacheStats sum = two.plus(one);
     86     assertEquals(124, sum.requestCount());
     87     assertEquals(64, sum.hitCount());
     88     assertEquals(64.0/124, sum.hitRate());
     89     assertEquals(60, sum.missCount());
     90     assertEquals(60.0/124, sum.missRate());
     91     assertEquals(56, sum.loadSuccessCount());
     92     assertEquals(52, sum.loadExceptionCount());
     93     assertEquals(52.0/108, sum.loadExceptionRate());
     94     assertEquals(56 + 52, sum.loadCount());
     95     assertEquals(48, sum.totalLoadTime());
     96     assertEquals(48.0/(56 + 52), sum.averageLoadPenalty());
     97     assertEquals(44, sum.evictionCount());
     98 
     99     assertEquals(sum, one.plus(two));
    100   }
    101 }
    102