/** Do a reset of all cachers */
  protected void doReset() {
    M_log.debug("doReset()");

    final List<Ehcache> allCaches = getAllCaches(false);
    for (Ehcache ehcache : allCaches) {
      ehcache.removeAll(); // TODO should we doNotNotifyCacheReplicators? Ian?
      ehcache.clearStatistics();
    }

    M_log.info("doReset():  Low Memory Recovery to: " + Runtime.getRuntime().freeMemory());
  } // doReset
Beispiel #2
0
  public void doTestElementUpdateRemove(
      boolean statsEnabled, boolean nonZeroStatsExpected, boolean asListener)
      throws InterruptedException {
    Random rand = new Random();
    int min = 100;
    for (int loop = 0; loop < 5; loop++) {
      int total = rand.nextInt(10000) + min;

      // always ensure enough capacity. Otherwise cannot predict
      // updateCount with eviction (based on capacity)
      Ehcache ehcache =
          new net.sf.ehcache.Cache(
              "test-" + nonZeroStatsExpected + "-" + loop, total + 1, false, false, 1200, 1200);
      manager.addCache(ehcache);
      // add as a listener
      AnotherStatistics anotherStats = new AnotherStatistics();
      if (asListener) {
        ehcache.registerCacheUsageListener(anotherStats);
      } else {
        // register and remove as we want to test remove listener
        ehcache.registerCacheUsageListener(anotherStats);
        ehcache.removeCacheUsageListener(anotherStats);
      }
      ehcache.setStatisticsEnabled(statsEnabled);

      assertEquals(0, anotherStats.getEvictedCount());
      assertEquals(0, anotherStats.getPutCount());
      assertEquals(0, anotherStats.getRemovedCount());
      assertEquals(0, anotherStats.getUpdateCount());

      for (int i = 0; i < total; i++) {
        ehcache.put(new Element("" + i, "value1"));
      }
      if (nonZeroStatsExpected) {
        assertEquals(total, anotherStats.getPutCount());
        assertEquals(0, anotherStats.getEvictedCount());
        // assertEquals(total, anotherStats.getSize());
        assertEquals(0, anotherStats.getUpdateCount());
        assertEquals(0, anotherStats.getRemovedCount());
      } else {
        assertEquals(0, anotherStats.getPutCount());
        assertEquals(0, anotherStats.getEvictedCount());
        // assertEquals(0, anotherStats.getSize());
        assertEquals(0, anotherStats.getRemovedCount());
        assertEquals(0, anotherStats.getUpdateCount());
      }

      // minimum 1 update
      int updates = rand.nextInt(total - 1) + 1;
      assertTrue(updates >= 1);
      for (int i = 0; i < updates; i++) {
        ehcache.put(new Element("" + i, "value1"));
      }
      if (nonZeroStatsExpected) {
        // assertEquals(total, anotherStats.getSize());
        assertEquals(updates, anotherStats.getUpdateCount());
        assertEquals(total, anotherStats.getPutCount());
        assertEquals(0, anotherStats.getEvictedCount());
        assertEquals(0, anotherStats.getRemovedCount());
      } else {
        // assertEquals(0, anotherStats.getSize());
        assertEquals(0, anotherStats.getPutCount());
        assertEquals(0, anotherStats.getRemovedCount());
        assertEquals(0, anotherStats.getEvictedCount());
        assertEquals(0, anotherStats.getUpdateCount());
      }

      // minimum 1 remove
      int remove = rand.nextInt(total - 1) + 1;
      assertTrue(updates >= 1);
      for (int i = 0; i < remove; i++) {
        ehcache.remove("" + i);
      }
      if (nonZeroStatsExpected) {
        // assertEquals(total - remove, anotherStats.getSize());
        assertEquals(updates, anotherStats.getUpdateCount());
        assertEquals(remove, anotherStats.getRemovedCount());
        assertEquals(total, anotherStats.getPutCount());
        assertEquals(0, anotherStats.getEvictedCount());
      } else {
        // assertEquals(0, anotherStats.getSize());
        assertEquals(0, anotherStats.getPutCount());
        assertEquals(0, anotherStats.getRemovedCount());
        assertEquals(0, anotherStats.getEvictedCount());
        assertEquals(0, anotherStats.getUpdateCount());
      }

      ehcache.clearStatistics();

      assertEquals(0, anotherStats.getPutCount());
      assertEquals(0, anotherStats.getRemovedCount());
      assertEquals(0, anotherStats.getEvictedCount());
      assertEquals(0, anotherStats.getUpdateCount());

      manager.removeCache(ehcache.getName());
    }
  }
Beispiel #3
0
  /**
   * Tests eviction statistics - evictedCount - missCountNotFound - missCountExpired - missCount -
   * expiredCount - size
   */
  public void doTestEvictionStatistics(
      boolean statsEnabled, boolean nonZeroStatsExpected, boolean asListener)
      throws InterruptedException {
    // run 5 times with random total and capacity values
    Random rand = new Random();
    int min = 100;
    for (int loop = 0; loop < 5; loop++) {
      int a = rand.nextInt(10000) + min;
      int b = rand.nextInt(10000) + min;
      if (a == b) {
        a += min;
      }
      int total = Math.max(a, b);
      int capacity = Math.min(a, b);
      Ehcache ehcache =
          new net.sf.ehcache.Cache(
              "test-" + nonZeroStatsExpected + "-" + loop, capacity, false, false, 2, 2);
      manager.addCache(ehcache);
      AnotherStatistics anotherStats = new AnotherStatistics();
      if (asListener) {
        ehcache.registerCacheUsageListener(anotherStats);
      } else {
        // register and remove as we want to test remove listener
        ehcache.registerCacheUsageListener(anotherStats);
        ehcache.removeCacheUsageListener(anotherStats);
      }
      ehcache.setStatisticsEnabled(statsEnabled);

      assertEquals(0, anotherStats.getEvictedCount());

      for (int i = 0; i < total; i++) {
        ehcache.put(new Element("" + i, "value1"));
      }
      if (nonZeroStatsExpected) {
        assertEquals(total - capacity, anotherStats.getEvictedCount());
      } else {
        assertEquals(0, anotherStats.getEvictedCount());
      }

      Thread.sleep(3010);

      // expiries do not count as eviction
      if (nonZeroStatsExpected) {
        assertEquals(total - capacity, anotherStats.getEvictedCount());
      } else {
        assertEquals(0, anotherStats.getEvictedCount());
      }

      // no expiration till a get is tried
      assertEquals(0, anotherStats.getCacheMissCount());
      assertEquals(0, anotherStats.getCacheMissCountExpired());
      assertEquals(0, anotherStats.getExpiredCount());
      assertEquals(0, anotherStats.getCacheMissCount());

      for (int i = 0; i < total; i++) {
        ehcache.get("" + i);
      }

      if (nonZeroStatsExpected) {
        assertEquals(total, anotherStats.getCacheMissCount());
        assertEquals(capacity, anotherStats.getCacheMissCountExpired());
        assertEquals(capacity, anotherStats.getExpiredCount());
        assertEquals(total, anotherStats.getCacheMissCount());
        // assertEquals(0, anotherStats.getSize());
      } else {
        assertEquals(0, anotherStats.getCacheMissCount());
        assertEquals(0, anotherStats.getCacheMissCountExpired());
        assertEquals(0, anotherStats.getExpiredCount());
        assertEquals(0, anotherStats.getCacheMissCount());
        // assertEquals(0, anotherStats.getSize());
      }

      ehcache.clearStatistics();

      assertEquals(0, anotherStats.getCacheMissCount());
      assertEquals(0, anotherStats.getCacheMissCountExpired());
      assertEquals(0, anotherStats.getExpiredCount());
      assertEquals(0, anotherStats.getCacheMissCount());
      // assertEquals(0, anotherStats.getSize());

      manager.removeCache(ehcache.getName());
    }
  }