aplcachetf

changed optionalboolean for optional. avoid dividebyzero.

5/5/2020 5:12:55 AM

Details

diff --git a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metadata/GroupOfOccurrences.java b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metadata/GroupOfOccurrences.java
index 611b32a..f065eea 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metadata/GroupOfOccurrences.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metadata/GroupOfOccurrences.java
@@ -110,19 +110,10 @@ public class GroupOfOccurrences {
     }
 
     protected void calculateThresholds() {
-        Thresholds.sumExecutionTime += this.bestMetrics.getSameOccurrencesTotalExecutionTime();
         Thresholds.executionTimes.add(this.bestMetrics.getSameOccurrencesTotalExecutionTime());
-
-        Thresholds.sumHitRatio += this.bestMetrics.getHitRatio();
         Thresholds.hitRatios.add(this.bestMetrics.getHitRatio());
-
-        Thresholds.sumMissRatio += this.bestMetrics.getMissRatio();
         Thresholds.missRatios.add(this.bestMetrics.getMissRatio());
-
-        Thresholds.sumShareability += this.bestMetrics.getShareability();
         Thresholds.shareabilities.add(this.bestMetrics.getShareability());
-
-        Thresholds.sumHitsPerTimeInCache.add(this.bestMetrics.getHitsPerTimeInCache());
         Thresholds.hitsPerTimeInCache.add(this.bestMetrics.getHitsPerTimeInCache());
     }
 
diff --git a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityMetrics.java b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityMetrics.java
index 820778c..f508c10 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityMetrics.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityMetrics.java
@@ -1,34 +1,46 @@
 package br.ufrgs.inf.prosoft.aplcachetf.extension.metrics;
 
 import br.ufrgs.inf.prosoft.aplcachetf.extension.metadata.GroupOfOccurrences;
-import java.util.Optional;
 
 public class CacheabilityMetrics {
 
     public static int K_STANDARD_DEVIATION = 0;
 
     public static boolean isStaticData(GroupOfOccurrences groupOfOccurrences) {
-        return groupOfOccurrences.getBestMetrics().getNumberOfDifferentReturnOccurrences() == 0;
+        if (groupOfOccurrences.getBestMetrics().getHitsPerTimeInCache().compareTo(Thresholds.hitsPerTimeInCacheThreshold(K_STANDARD_DEVIATION)) < 0) {
+            return false;
+        }
+        return groupOfOccurrences.getBestMetrics().getHitRatio() == 100.0;
+    }
+
+    public static boolean isNotStaticData(GroupOfOccurrences groupOfOccurrences) {
+        return !isStaticData(groupOfOccurrences);
     }
 
-    public static Optional<Boolean> changeMoreThanUsed(GroupOfOccurrences groupOfOccurrences) {
-        //+/- k sds
-        return Optional.of(groupOfOccurrences.getBestMetrics().getMissRatio() > Thresholds.missThreshold(K_STANDARD_DEVIATION));
+    public static boolean changeMoreThanUsed(GroupOfOccurrences groupOfOccurrences) {
+        return groupOfOccurrences.getBestMetrics().getMissRatio() > Thresholds.missThreshold(K_STANDARD_DEVIATION);
     }
 
-    public static Optional<Boolean> usedByManyRequests(GroupOfOccurrences groupOfOccurrences) {
-        return Optional.of(groupOfOccurrences.getBestMetrics().getHitsPerTimeInCache().compareTo(Thresholds.hitsPerTimeInCacheThreshold(K_STANDARD_DEVIATION)) >= 0);
+    public static boolean hasHighUsagePerCaching(GroupOfOccurrences groupOfOccurrences) {
+        return groupOfOccurrences.getBestMetrics().getHitsPerTimeInCache().compareTo(Thresholds.hitsPerTimeInCacheThreshold(K_STANDARD_DEVIATION)) >= 0;
     }
 
-    public static Optional<Boolean> isUserSpecific(GroupOfOccurrences groupOfOccurrences) {
+    public static boolean hasLowUsagePerCaching(GroupOfOccurrences groupOfOccurrences) {
+        return !hasHighUsagePerCaching(groupOfOccurrences);
+    }
+
+    public static boolean isUserSpecific(GroupOfOccurrences groupOfOccurrences) {
         if (groupOfOccurrences.getBestMetrics().getAmountOfIdentifiedSameOccurences() == 0) {
-            return Optional.empty();
+            return false;
         }
-        //the less shareable, the more user specific
-        return Optional.of(groupOfOccurrences.getBestMetrics().getShareability() < Thresholds.shareabilityThreshold(K_STANDARD_DEVIATION));
+        return groupOfOccurrences.getBestMetrics().getShareability() < Thresholds.shareabilityThreshold(K_STANDARD_DEVIATION);
+    }
+
+    public static boolean isExpensive(GroupOfOccurrences groupOfOccurrences) {
+        return groupOfOccurrences.getBestMetrics().getSameOccurrencesAverageExecutionTime() >= Thresholds.expensivenessThreshold(K_STANDARD_DEVIATION);
     }
 
-    public static Optional<Boolean> isExpensive(GroupOfOccurrences groupOfOccurrences) {
-        return Optional.of(groupOfOccurrences.getBestMetrics().getSameOccurrencesAverageExecutionTime() >= Thresholds.expensivenessThreshold(K_STANDARD_DEVIATION));
+    public static boolean isCheap(GroupOfOccurrences groupOfOccurrences) {
+        return !isExpensive(groupOfOccurrences);
     }
 }
diff --git a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityPatternDecider.java b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityPatternDecider.java
index 93fac50..72d97f8 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityPatternDecider.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/CacheabilityPatternDecider.java
@@ -1,7 +1,6 @@
 package br.ufrgs.inf.prosoft.aplcachetf.extension.metrics;
 
 import br.ufrgs.inf.prosoft.aplcachetf.extension.metadata.GroupOfOccurrences;
-import java.util.Optional;
 
 public class CacheabilityPatternDecider {
 
@@ -12,27 +11,23 @@ public class CacheabilityPatternDecider {
      * @return
      */
     public static boolean isCacheable(GroupOfOccurrences groupOfOccurrences) {
-        //Is the data completely static?
         boolean isStaticData = CacheabilityMetrics.isStaticData(groupOfOccurrences);
-        if (isStaticData) { // staticity yes
+        if (isStaticData) {
             return true;
         }
-        // staticity no/not sure
-        Optional<Boolean> changeMoreThanUsed = CacheabilityMetrics.changeMoreThanUsed(groupOfOccurrences);
-        if (changeMoreThanUsed.isPresent() && changeMoreThanUsed.get()) { //changeMoreThanUsed true
+        boolean changeMoreThanUsed = CacheabilityMetrics.changeMoreThanUsed(groupOfOccurrences);
+        if (changeMoreThanUsed) {
             return false;
         }
-        //changeMoreThanUsed not/not sure
-        Optional<Boolean> usedByManyRequests = CacheabilityMetrics.usedByManyRequests(groupOfOccurrences);
-        if (usedByManyRequests.isPresent() && !usedByManyRequests.get()) { //useByManyRequests no
+        boolean hasHighUsagePerCaching = CacheabilityMetrics.hasHighUsagePerCaching(groupOfOccurrences);
+        if (!hasHighUsagePerCaching) {
             return false;
         }
-        Optional<Boolean> isUserSpecific = CacheabilityMetrics.isUserSpecific(groupOfOccurrences);
-        if (isUserSpecific.isPresent() && isUserSpecific.get()) {
+        boolean isUserSpecific = CacheabilityMetrics.isUserSpecific(groupOfOccurrences);
+        if (isUserSpecific) {
             return true;
         }
-        Optional<Boolean> isExpensive = CacheabilityMetrics.isExpensive(groupOfOccurrences);
-        return isExpensive.isPresent() ? isExpensive.get() : true;
+        return CacheabilityMetrics.isExpensive(groupOfOccurrences);
     }
 
     public static boolean isNotCacheable(GroupOfOccurrences groupOfOccurrences) {
diff --git a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Metrics.java b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Metrics.java
index b486553..78cc8d2 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Metrics.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Metrics.java
@@ -62,6 +62,9 @@ public class Metrics extends br.ufrgs.inf.prosoft.tfcache.Metrics {
     }
 
     public double getSameOccurrencesAverageExecutionTime() {
+        if (sameOccurrences == 0) {
+            return 0;
+        }
         return new BigDecimal(getSameOccurrencesTotalExecutionTime())
                 .divide(new BigDecimal(sameOccurrences), 5, RoundingMode.HALF_UP)
                 .doubleValue();
diff --git a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Thresholds.java b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Thresholds.java
index edc8a75..ceba2bc 100644
--- a/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Thresholds.java
+++ b/src/main/java/br/ufrgs/inf/prosoft/aplcachetf/extension/metrics/Thresholds.java
@@ -18,12 +18,6 @@ import java.util.List;
  */
 public class Thresholds {
 
-    public static double sumMissRatio;
-    public static double sumHitRatio;
-    public static double sumExecutionTime;
-    public static double sumShareability;
-    public static BigDecimal sumHitsPerTimeInCache;
-
     public static BigDecimal averageHitsPerTimeInCache;
 
     public static Double stdDevHitRatio;
@@ -41,12 +35,6 @@ public class Thresholds {
     public static final List<BigDecimal> hitsPerTimeInCache = new ArrayList<>();
 
     public static void reset() {
-        Thresholds.sumMissRatio = 0;
-        Thresholds.sumHitRatio = 0;
-        Thresholds.sumExecutionTime = 0;
-        Thresholds.sumShareability = 0;
-        Thresholds.sumHitsPerTimeInCache = new BigDecimal(BigInteger.ZERO);
-
         Thresholds.averageHitsPerTimeInCache = null;
 
         Thresholds.stdDevHitRatio = null;
@@ -73,28 +61,28 @@ public class Thresholds {
         if (population == 0) {
             return 0;
         }
-        return new BigDecimal(sumHitRatio).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
+        return new BigDecimal(hitRatios.stream().reduce(Double::sum).orElse(0D)).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
     }
 
     public static double getAverageMissRatio() {
         if (population == 0) {
             return 0;
         }
-        return new BigDecimal(sumMissRatio).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
+        return new BigDecimal(missRatios.stream().reduce(Double::sum).orElse(0D)).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
     }
 
     public static double getAverageExecutionTime() {
         if (population == 0) {
             return 0;
         }
-        return new BigDecimal(sumExecutionTime).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
+        return new BigDecimal(executionTimes.stream().reduce(Long::sum).orElse(0L)).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
     }
 
     public static double getAverageShareability() {
         if (population == 0) {
             return 0;
         }
-        return new BigDecimal(sumShareability).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
+        return new BigDecimal(shareabilities.stream().reduce(Double::sum).orElse(0D)).divide(new BigDecimal(population), 5, RoundingMode.HALF_UP).doubleValue();
     }
 
     public static double getStdDevHitRatio() {
@@ -189,7 +177,8 @@ public class Thresholds {
             return averageHitsPerTimeInCache;
         }
 
-        averageHitsPerTimeInCache = sumHitsPerTimeInCache.divide(new BigDecimal(hitsPerTimeInCache.size()), 5, RoundingMode.HALF_UP);
+        averageHitsPerTimeInCache = hitsPerTimeInCache.stream().reduce(BigDecimal::add).orElse(new BigDecimal(BigInteger.ZERO))
+                .divide(new BigDecimal(hitsPerTimeInCache.size()), MathContext.DECIMAL128);
         return averageHitsPerTimeInCache;
     }