JCSCacheStatisticsMXBean.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.jcs3.jcache.jmx;

  20. import javax.cache.management.CacheStatisticsMXBean;

  21. import org.apache.commons.jcs3.jcache.Statistics;

  22. public class JCSCacheStatisticsMXBean implements CacheStatisticsMXBean
  23. {
  24.     private final Statistics statistics;

  25.     public JCSCacheStatisticsMXBean(final Statistics stats)
  26.     {
  27.         this.statistics = stats;
  28.     }

  29.     @Override
  30.     public void clear()
  31.     {
  32.         statistics.reset();
  33.     }

  34.     @Override
  35.     public long getCacheHits()
  36.     {
  37.         return statistics.getHits();
  38.     }

  39.     @Override
  40.     public float getCacheHitPercentage()
  41.     {
  42.         final long hits = getCacheHits();
  43.         if (hits == 0)
  44.         {
  45.             return 0;
  46.         }
  47.         return (float) hits / getCacheGets() * 100.0f;
  48.     }

  49.     @Override
  50.     public long getCacheMisses()
  51.     {
  52.         return statistics.getMisses();
  53.     }

  54.     @Override
  55.     public float getCacheMissPercentage()
  56.     {
  57.         final long misses = getCacheMisses();
  58.         if (misses == 0)
  59.         {
  60.             return 0;
  61.         }
  62.         return (float) misses / getCacheGets() * 100.0f;
  63.     }

  64.     @Override
  65.     public long getCacheGets()
  66.     {
  67.         return getCacheHits() + getCacheMisses();
  68.     }

  69.     @Override
  70.     public long getCachePuts()
  71.     {
  72.         return statistics.getPuts();
  73.     }

  74.     @Override
  75.     public long getCacheRemovals()
  76.     {
  77.         return statistics.getRemovals();
  78.     }

  79.     @Override
  80.     public long getCacheEvictions()
  81.     {
  82.         return statistics.getEvictions();
  83.     }

  84.     @Override
  85.     public float getAverageGetTime()
  86.     {
  87.         return averageTime(statistics.getTimeTakenForGets());
  88.     }

  89.     @Override
  90.     public float getAveragePutTime()
  91.     {
  92.         return averageTime(statistics.getTimeTakenForPuts());
  93.     }

  94.     @Override
  95.     public float getAverageRemoveTime()
  96.     {
  97.         return averageTime(statistics.getTimeTakenForRemovals());
  98.     }

  99.     private float averageTime(final long timeTaken)
  100.     {
  101.         final long gets = getCacheGets();
  102.         if (timeTaken == 0 || gets == 0)
  103.         {
  104.             return 0;
  105.         }
  106.         return timeTaken / gets;
  107.     }
  108. }