View Javadoc

1   package cz.cuni.amis.utils;
2   
3   public class Throughput {
4   	
5   	private long lastTime = -1;
6   	
7   	private long records = 0;
8   	
9   	private long data = 0;
10  	
11  	private long firstDataTime = -1;
12  	
13  	private long totalRecords = 0;
14  	
15  	private long totalData = 0;
16  	
17  	private String units;
18  
19  	private double throughput = 0;
20  	
21  	private boolean reporting = false;
22  	
23  	private String name = "Throughput"; 
24  	
25  	public Throughput(String units) {
26  		this.units = units;
27  	}
28  	
29  	public boolean isReporting() {
30  		return reporting;
31  	}
32  
33  	public void setReporting(boolean reporting) {
34  		this.reporting = reporting;
35  	}
36  	
37  	public double getThroughput() {
38  		return throughput;
39  	}
40  
41  	public String getName() {
42  		return name;
43  	}
44  
45  	public void setName(String name) {
46  		this.name = name;
47  	}
48  
49  	public void add(long data) {
50  		if (firstDataTime < 0) {
51  			firstDataTime = System.currentTimeMillis();
52  		}
53  		if (lastTime < 0) {
54  			lastTime = System.currentTimeMillis();
55  			return;
56  		}
57  		this.data += data;
58  		this.records += 1;
59  		this.totalData += data;
60  		this.totalRecords += 1;
61  		if (System.currentTimeMillis() - lastTime > 1000) {
62  			if (isReporting()) {
63  				System.out.println("[INFO]  " + name + ": " + this.data + " " + units + " / sec | " + this.records + " records / sec");
64  			}
65  			this.data = 0;
66  			this.records = 0;
67  			lastTime = System.currentTimeMillis();
68  		}
69  	}
70  	
71  	public long getTotalData() {
72  		return totalData;
73  	}
74  	
75  	public long getTotalRecords() {
76  		return totalRecords;
77  	}
78  	
79  	/**
80  	 * Return data / secs.
81  	 * @return
82  	 */
83  	public double getCurrentThroughput() {
84  		long time = System.currentTimeMillis() - firstDataTime;
85  		if (time < 1) return 0;
86  		return (double)totalData / ((double)time/1000);
87  	}
88  	
89  	public double getCheckThroughput() {
90  		return this.throughput;
91  	}
92  
93  	public void check() {
94  		this.throughput = getCurrentThroughput();
95  		this.data = 0;
96  		this.records = 0;
97  		this.totalData = 0;
98  		this.totalRecords = 0;
99  		this.lastTime = -1;
100 		this.firstDataTime = -1;		
101 	}
102 
103 }