View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.filter;
15  
16  
17  abstract public class PeriodicStats {
18    
19    private long nextPeriodBegins = 0;
20    private long lastTotal = 0;
21    private long lastCount = 0;
22  
23    private double average;
24    private int n;
25   
26    PeriodicStats() {
27      this(System.currentTimeMillis());
28    }
29    
30    PeriodicStats(long now) {
31      nextPeriodBegins = computeStartOfNextPeriod(now);
32    }
33   
34    void update(long now, long total) {
35      if (now > nextPeriodBegins) {     
36        lastCount = total - lastTotal;
37        lastTotal = total;
38        average = (average * n + lastCount) / (++n);
39        nextPeriodBegins = computeStartOfNextPeriod(now);
40      }
41    }
42  
43    public double getAverage() {
44      return average;
45    }
46  
47    public long getLastCount() {
48      return lastCount;
49    }
50    
51    void reset(long now) {
52      nextPeriodBegins = computeStartOfNextPeriod(now);
53      lastTotal = 0;
54      lastCount = 0;
55      average = 0.0;
56      n = 0;
57    }
58    
59    void reset() {
60      reset(System.currentTimeMillis());
61    }
62    
63    abstract long computeStartOfNextPeriod(long now);
64  
65  }