[logback-dev] svn commit: r977 - in logback/trunk/logback-access/src: main/java/ch/qos/logback/access/filter test/java/ch/qos/logback/access test/java/ch/qos/logback/access/filter

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Nov 21 21:48:36 CET 2006


Author: ceki
Date: Tue Nov 21 21:48:36 2006
New Revision: 977

Added:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/PeriodicStats.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/StatsByDay.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/filter/
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/filter/AccessStatsTest.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/filter/PackageTest.java
Modified:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStats.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStatsImpl.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/CountingFilter.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/AllTest.java

Log:
Initial work on periodic statistics counters

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStats.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStats.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStats.java	Tue Nov 21 21:48:36 2006
@@ -5,8 +5,8 @@
   
   long getTotal();
   
-  long getDailyTotal();
-  long getDailyAverage();
+  long getLastDaysCount();
+  double getDailyAverage();
  
   
   long getWeeklyTotal();

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStatsImpl.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStatsImpl.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/AccessStatsImpl.java	Tue Nov 21 21:48:36 2006
@@ -1,30 +1,31 @@
 package ch.qos.logback.access.filter;
 
-public class AccessStatsImpl implements AccessStats {
+import ch.qos.logback.core.spi.LifeCycle;
+
+public class AccessStatsImpl implements AccessStats, LifeCycle {
 
   final CountingFilter countingFilter;
+  boolean started;
+
+  StatsByDay statsByDay = new StatsByDay(System.currentTimeMillis());
   
   AccessStatsImpl(CountingFilter countingFilter) {
     this.countingFilter = countingFilter;
   }
-  
-  public long getDailyAverage() {
-    // TODO Auto-generated method stub
-    return 0;
+
+  public double getDailyAverage() {
+    return statsByDay.getAverage();
   }
 
-  public long getDailyTotal() {
-    // TODO Auto-generated method stub
-    return 0;
+  public long getLastDaysCount() {
+    return statsByDay.getLastCount();
   }
 
   public long getMonthlyAverage() {
-    // TODO Auto-generated method stub
     return 0;
   }
 
   public long getMonthlyTotal() {
-    // TODO Auto-generated method stub
     return 0;
   }
 
@@ -33,13 +34,33 @@
   }
 
   public long getWeeklyAverage() {
-    // TODO Auto-generated method stub
     return 0;
   }
 
   public long getWeeklyTotal() {
-    // TODO Auto-generated method stub
     return 0;
   }
 
+  void refresh(long now) {
+    statsByDay.refresh(now, getTotal());
+  }
+
+  void refresh() {
+    long now = System.currentTimeMillis();
+    refresh(now);
+  }
+
+  public void start() {
+    started = true;
+
+  }
+
+  public boolean isStarted() {
+    return started;
+  }
+
+  public void stop() {
+    started = false;
+  }
+
 }

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/CountingFilter.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/CountingFilter.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/CountingFilter.java	Tue Nov 21 21:48:36 2006
@@ -20,9 +20,7 @@
   
   @Override
   public FilterReply decide(Object event) {
-
     total++;
-
     return FilterReply.NEUTRAL;
   }
 
@@ -31,8 +29,6 @@
   }
   
   
-  
-
   @Override
   public void start() {
     MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/PeriodicStats.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/PeriodicStats.java	Tue Nov 21 21:48:36 2006
@@ -0,0 +1,36 @@
+package ch.qos.logback.access.filter;
+
+
+abstract public class PeriodicStats {
+  
+  private long nextPeriodBegins = 0;
+  private long lastTotal = 0;
+  private long lastCount = 0;
+
+  private double average;
+  private int n;
+ 
+  PeriodicStats(long now) {
+    nextPeriodBegins = computeStartOfNextPeriod(now);
+  }
+ 
+  void refresh(long now, long total) {
+    if (now > nextPeriodBegins) {     
+      lastCount = total - lastTotal;
+      lastTotal = total;
+      average = (average * n + lastCount) / (++n);
+      nextPeriodBegins = computeStartOfNextPeriod(now);
+    }
+  }
+
+  public double getAverage() {
+    return average;
+  }
+
+  public long getLastCount() {
+    return lastCount;
+  }
+  
+  abstract long computeStartOfNextPeriod(long now);
+
+}

Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/StatsByDay.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/filter/StatsByDay.java	Tue Nov 21 21:48:36 2006
@@ -0,0 +1,15 @@
+package ch.qos.logback.access.filter;
+
+import ch.qos.logback.core.util.TimeUtil;
+
+public class StatsByDay extends PeriodicStats {
+  
+  StatsByDay(long now) {
+    super(now);
+  }
+  @Override
+  long computeStartOfNextPeriod(long now) {
+    return TimeUtil.computeStartOfNextDay(now);
+  }
+  
+}

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/AllTest.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/AllTest.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/AllTest.java	Tue Nov 21 21:48:36 2006
@@ -20,6 +20,7 @@
     suite.addTest(ch.qos.logback.access.net.PackageTest.suite());
     suite.addTest(ch.qos.logback.access.pattern.PackageTest.suite());
     suite.addTest(ch.qos.logback.access.jetty.PackageTest.suite());
+    suite.addTest(ch.qos.logback.access.filter.PackageTest.suite());
     return suite;
   }
 }

Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/filter/AccessStatsTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/filter/AccessStatsTest.java	Tue Nov 21 21:48:36 2006
@@ -0,0 +1,84 @@
+package ch.qos.logback.access.filter;
+
+import junit.framework.TestCase;
+import ch.qos.logback.access.spi.AccessEvent;
+import ch.qos.logback.core.util.TimeUtil;
+
+public class AccessStatsTest extends TestCase {
+
+  AccessEvent accessEvent = new AccessEvent(null, null, null);
+  public AccessStatsTest(String arg0) {
+    super(arg0);
+  }
+
+  protected void setUp() throws Exception {
+    super.setUp();
+  }
+
+  protected void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+  public void testBasic() {
+    
+    CountingFilter cf = new CountingFilter();
+    AccessStatsImpl asi = new AccessStatsImpl(cf);
+    asi.start();
+    // Tue Nov 21 18:05:36 CET 2006
+    long now = 1164128736369L;
+    
+    // test fresh start
+    asi.refresh(now);
+    assertEquals(0, asi.getLastDaysCount());
+    assertEquals(0, asi.getDailyAverage(), 0.01);
+
+    // first event
+    cf.decide(null);
+
+    asi.refresh(now);
+    assertEquals(0, asi.getLastDaysCount());
+    assertEquals(0.0, asi.getDailyAverage(), 0.01);
+
+    long nextDay0 = TimeUtil.computeStartOfNextDay(now);
+    nextDay0 += 99;
+    
+    // there should be one event the next day, avg should also be 1
+    asi.refresh(nextDay0);
+    assertEquals(1.0, asi.getLastDaysCount(), 0.01);
+    assertEquals(1.0, asi.getDailyAverage(), 0.01);
+
+    cf.decide(null); // 2nd event
+    cf.decide(null); // 3rd event
+
+    asi.refresh(nextDay0);
+    assertEquals(1, asi.getLastDaysCount());
+    assertEquals(1.0, asi.getDailyAverage(), 0.01);
+
+    long nextDay1 = TimeUtil.computeStartOfNextDay(nextDay0) + 6747;
+    asi.refresh(nextDay1);
+    assertEquals(2, asi.getLastDaysCount());
+    assertEquals(1.5, asi.getDailyAverage(), 0.01);
+
+    nextDay1 += 4444;
+    cf.decide(null); // 4th event
+    cf.decide(null); // 5th event
+    cf.decide(null); // 6th event
+    cf.decide(null); // 7th event
+
+    asi.refresh(nextDay1);
+    // values should remain unchanged
+    assertEquals(2, asi.getLastDaysCount());
+    assertEquals(1.5, asi.getDailyAverage(), 0.01);
+
+    
+    long nextDay2 = TimeUtil.computeStartOfNextDay(nextDay1) + 11177;
+
+    asi.refresh(nextDay2);
+    // values should remain unchanged
+    assertEquals(4, asi.getLastDaysCount());
+    assertEquals(7.0/3, asi.getDailyAverage(), 0.01);
+    
+
+    
+  }
+}

Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/filter/PackageTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/filter/PackageTest.java	Tue Nov 21 21:48:36 2006
@@ -0,0 +1,22 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework for Java.
+ * 
+ * Copyright (C) 2000-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package ch.qos.logback.access.filter;
+
+import junit.framework.*;
+
+public class PackageTest extends TestCase {
+
+  public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(AccessStatsTest.class);
+    return suite;
+  }
+}
\ No newline at end of file



More information about the logback-dev mailing list