source: trunk/org/expeditee/stats/AgentStats.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 1.8 KB
Line 
1package org.expeditee.stats;
2
3import java.util.Date;
4
5public class AgentStats {
6
7 private static int _ItemsExecuted = 0;
8
9 private static int _FramesExecuted = 0;
10
11 private static String SECONDS = "sec";
12
13 private static Date _StartTime = new Date();
14
15 private static String getTimeElapsedString() {
16 return "Time: " + getSecondsElapsed() + SECONDS;
17 }
18
19 public static long getMilliSecondsElapsed() {
20 return new Date().getTime() - _StartTime.getTime();
21 }
22
23 public static String getStats() {
24 StringBuffer stats = new StringBuffer();
25 stats.append("Run ").append(getTimeElapsedString()).append(
26 ", Processed ");
27 appendStat(stats, "frames", _FramesExecuted, ", ");
28 appendStat(stats, "items", _ItemsExecuted, "");
29 return stats.toString();
30 }
31
32 private static double getSecondsElapsed() {
33 return (new Date().getTime() - _StartTime.getTime()) / 1000.0;
34 }
35
36 /**
37 * Appends a single stats to a string buffer containing a collection of
38 * stats.
39 *
40 * @param stats
41 * The string buffer to append the stat onto
42 * @param name
43 * The name of the stat
44 * @param value
45 * The new value for the stat
46 */
47 private static void appendStat(StringBuffer stats, String name, int value,
48 String separator) {
49 // prevent divide by zero errors
50 if (value < 0)
51 value = 0;
52
53 int perSec = (int) (value / Math.max(0.001, getSecondsElapsed()));
54
55 stats.append(value).append(" ").append(name).append(" @ ").append(
56 perSec).append("/" + SECONDS);
57 stats.append(separator);
58 }
59
60 public static void reset() {
61 _StartTime = new Date();
62 _FramesExecuted = 0;
63 _ItemsExecuted = 0;
64 }
65
66 /**
67 * Called signal that a frame has been accessed.
68 *
69 */
70 public static void FrameExecuted() {
71 _FramesExecuted++;
72 }
73
74 public static void ItemExecuted() {
75 _ItemsExecuted++;
76 }
77}
Note: See TracBrowser for help on using the repository browser.