1 | /**
|
---|
2 | * AgentStats.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.stats;
|
---|
20 |
|
---|
21 | import java.util.Date;
|
---|
22 |
|
---|
23 | public class AgentStats {
|
---|
24 |
|
---|
25 | private static int _ItemsExecuted = 0;
|
---|
26 |
|
---|
27 | private static int _FramesExecuted = 0;
|
---|
28 |
|
---|
29 | private static String SECONDS = "sec";
|
---|
30 |
|
---|
31 | private static Date _StartTime = new Date();
|
---|
32 |
|
---|
33 | private static String getTimeElapsedString() {
|
---|
34 | return "Time: " + getSecondsElapsed() + SECONDS;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static long getMilliSecondsElapsed() {
|
---|
38 | return new Date().getTime() - _StartTime.getTime();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public static String getStats() {
|
---|
42 | StringBuffer stats = new StringBuffer();
|
---|
43 | stats.append("Run ").append(getTimeElapsedString()).append(
|
---|
44 | ", Processed ");
|
---|
45 | appendStat(stats, "frames", _FramesExecuted, ", ");
|
---|
46 | appendStat(stats, "items", _ItemsExecuted, "");
|
---|
47 | return stats.toString();
|
---|
48 | }
|
---|
49 |
|
---|
50 | private static double getSecondsElapsed() {
|
---|
51 | return (new Date().getTime() - _StartTime.getTime()) / 1000.0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Appends a single stats to a string buffer containing a collection of
|
---|
56 | * stats.
|
---|
57 | *
|
---|
58 | * @param stats
|
---|
59 | * The string buffer to append the stat onto
|
---|
60 | * @param name
|
---|
61 | * The name of the stat
|
---|
62 | * @param value
|
---|
63 | * The new value for the stat
|
---|
64 | */
|
---|
65 | private static void appendStat(StringBuffer stats, String name, int value,
|
---|
66 | String separator) {
|
---|
67 | // prevent divide by zero errors
|
---|
68 | if (value < 0)
|
---|
69 | value = 0;
|
---|
70 |
|
---|
71 | int perSec = (int) (value / Math.max(0.001, getSecondsElapsed()));
|
---|
72 |
|
---|
73 | stats.append(value).append(" ").append(name).append(" @ ").append(
|
---|
74 | perSec).append("/" + SECONDS);
|
---|
75 | stats.append(separator);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static void reset() {
|
---|
79 | _StartTime = new Date();
|
---|
80 | _FramesExecuted = 0;
|
---|
81 | _ItemsExecuted = 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Called signal that a frame has been accessed.
|
---|
86 | *
|
---|
87 | */
|
---|
88 | public static void FrameExecuted() {
|
---|
89 | _FramesExecuted++;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static void ItemExecuted() {
|
---|
93 | _ItemsExecuted++;
|
---|
94 | }
|
---|
95 | }
|
---|