Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5827 amar.kumar 1
/*
2
 *	Copyright 2005 stat4j.org
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *	You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.sourceforge.stat4j.calculators;
17
 
18
import java.util.Properties;
19
 
20
import net.sourceforge.stat4j.Metric;
21
 
22
 
23
/**
24
 * Name:		Rate.java
25
 * Date:		Aug 29, 2004
26
 * Description:
27
 * 
28
 * 
29
 * @author Lara D'Abreo
30
 */
31
public class Rate extends CalculatorAdapter {
32
 
33
 
34
	protected long starttimestamp;
35
	protected double	rate;
36
	protected long 		period;
37
	protected long		occurances;
38
 
39
 
40
	public Rate() {
41
		reset();
42
	}
43
 
44
 
45
	public void applyMetric(Metric metric) {
46
 
47
 
48
		occurances = occurances + 1;
49
		if (metric.isSingle()) {
50
 
51
			double elapsedTime = metric.getTimestamp() - starttimestamp;
52
 
53
			if (elapsedTime == 0.0) return;
54
			double divisor = elapsedTime / period;
55
			rate = occurances / divisor;		
56
			//System.out.println("e:" + elapsedTime + " d:" + divisor + " rate:" + rate);
57
			setTimestamp(metric.getTimestamp());
58
		}
59
	}
60
 
61
 
62
	public double getResult() {
63
		return rate;
64
	}
65
 
66
	public void reset() {
67
		occurances = 0;
68
		this.rate = 0.0;
69
		starttimestamp = System.currentTimeMillis();
70
	}
71
 
72
	public void init(String name,Properties properties) {
73
		// read per
74
		String periodStr = properties.getProperty("period","1000");
75
		this.period = Long.parseLong(periodStr);
76
 
77
		super.init(name);
78
	}
79
}