1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.debug;
35
36 import java.util.Iterator;
37 import java.util.Map;
38
39 import org.apache.commons.collections.Factory;
40 import org.apache.commons.collections.MapUtils;
41 import org.apache.commons.collections.map.LinkedMap;
42
43 import info.magnolia.context.MgnlContext;
44
45
46
47
48
49
50
51
52 public class PerformanceTestStatus {
53
54 private static final String ATTRIBUTE_NAME = PerformanceTestStatus.class.getName();
55
56 class TestStatus{
57 long start = -1;
58 long total = -1;
59 }
60
61 Map states = MapUtils.lazyMap(new LinkedMap(), new Factory() {
62 public Object create() {
63 return new TestStatus();
64 }
65 });
66
67 public void start(String key){
68 getTestStatus(key).start = System.currentTimeMillis();
69 }
70
71 private TestStatus getTestStatus(String key) {
72 return ((TestStatus)states.get(key));
73 }
74
75 public void stop(String key){
76 TestStatus status = getTestStatus(key);
77 status.total += System.currentTimeMillis() - status.start;
78 }
79
80 public static PerformanceTestStatus getInstance() {
81 if(!MgnlContext.hasAttribute(ATTRIBUTE_NAME)) {
82 MgnlContext.setAttribute(ATTRIBUTE_NAME, new PerformanceTestStatus());
83 }
84 return (PerformanceTestStatus) MgnlContext.getAttribute(ATTRIBUTE_NAME);
85 }
86
87 public String toString() {
88 StringBuffer str = new StringBuffer();
89 final Iterator it = states.entrySet().iterator();
90 while(it.hasNext()) {
91 Map.Entry entry = (Map.Entry) it.next();
92 final TestStatus testStatus = (TestStatus) entry.getValue();
93 final String value = testStatus.total >= 0 ? String.valueOf(testStatus.total) : "(not stopped yet)";
94 str.append(entry.getKey());
95 str.append(": ");
96 str.append(value);
97 if (it.hasNext()) {
98 str.append(", ");
99 }
100 }
101 return str.toString();
102 }
103
104 }