%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.agent.ChildTimeTrackerImpl |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
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 | * |
|
17 | * |
|
18 | * Original Author: kamal.govindraj (Tavant Technologies) |
|
19 | * Contributor(s): -; |
|
20 | * |
|
21 | */ |
|
22 | package net.sf.infrared.agent; |
|
23 | ||
24 | import net.sf.infrared.base.util.LoggingFactory; |
|
25 | ||
26 | import org.apache.log4j.Logger; |
|
27 | ||
28 | /** |
|
29 | * An array based implementation, this approach is more complex but better than |
|
30 | * the stack based approach as it creates less Objects for the normal cases. The |
|
31 | * stack based approach has to work with immutable Long objects every update |
|
32 | * results in throwing away the existing value and creating a new object. |
|
33 | * |
|
34 | * @author kamal.govindraj |
|
35 | */ |
|
36 | 2 | public class ChildTimeTrackerImpl implements ChildTimeTracker { |
37 | private static final int INITIAL_SIZE = 25; |
|
38 | ||
39 | private static final int GROW_BY = INITIAL_SIZE / 4; |
|
40 | ||
41 | 2 | private static final Logger log = LoggingFactory.getLogger(ChildTimeTrackerImpl.class); |
42 | ||
43 | 2 | private long[] childTimes = null; |
44 | ||
45 | 2 | private int capacity = 0; |
46 | ||
47 | 2 | private int top = 0; |
48 | ||
49 | public void begin() { |
|
50 | 104 | if (top >= capacity) { |
51 | 15 | growArray(); |
52 | } |
|
53 | ||
54 | 104 | childTimes[top++] = 0; |
55 | 104 | } |
56 | ||
57 | public void recordChildExecutionTime(long time) { |
|
58 | 104 | if (top > 1) { |
59 | 102 | childTimes[top - 2] += time; |
60 | } |
|
61 | 104 | } |
62 | ||
63 | public long getChildExecutionTime() { |
|
64 | // Just a defensive check |
|
65 | // There might be cases where monitoring is turned on |
|
66 | // mid request, this might lead to mismatched begin/ end calls |
|
67 | 102 | if (top > 0) { |
68 | 102 | return childTimes[top - 1]; |
69 | } |
|
70 | 0 | if (log.isDebugEnabled()) { |
71 | 0 | log.debug("Mismatch in being/end calls"); |
72 | } |
|
73 | 0 | return 0; |
74 | } |
|
75 | ||
76 | public void end() { |
|
77 | 104 | if (top > 0) { |
78 | 104 | top--; |
79 | } |
|
80 | ||
81 | 104 | shrinkIfRequired(); |
82 | 104 | } |
83 | ||
84 | public void reset() { |
|
85 | 0 | top = 0; |
86 | 0 | shrinkIfRequired(); |
87 | 0 | } |
88 | ||
89 | private void shrinkIfRequired() { |
|
90 | 104 | if ((capacity > INITIAL_SIZE) && ((capacity - top) > (GROW_BY * 2))) { |
91 | 13 | if (log.isInfoEnabled()) { |
92 | 0 | log.info("Shrinking array capacity " + capacity + " top " + top); |
93 | } |
|
94 | ||
95 | 13 | long[] temp = childTimes; |
96 | 13 | capacity = capacity - GROW_BY; |
97 | 13 | childTimes = new long[capacity]; |
98 | ||
99 | 715 | for (int i = 0; i < top; i++) { |
100 | 702 | childTimes[i] = temp[i]; |
101 | } |
|
102 | } |
|
103 | 104 | } |
104 | ||
105 | private void growArray() { |
|
106 | 15 | if (capacity > 0) { |
107 | 13 | long[] temp = childTimes; |
108 | 13 | childTimes = new long[capacity + GROW_BY]; |
109 | ||
110 | 806 | for (int i = 0; i < temp.length; i++) { |
111 | 793 | childTimes[i] = temp[i]; |
112 | } |
|
113 | ||
114 | 13 | capacity = capacity + GROW_BY; |
115 | ||
116 | 13 | if (log.isInfoEnabled()) { |
117 | 0 | log.info("Growing array by " + GROW_BY + " capacity " + capacity + " top " + top); |
118 | } |
|
119 | } else { |
|
120 | 2 | childTimes = new long[INITIAL_SIZE]; |
121 | 2 | capacity = INITIAL_SIZE; |
122 | ||
123 | 2 | if (log.isInfoEnabled()) { |
124 | 0 | log.info("Initizliaing array with initial size " + INITIAL_SIZE); |
125 | } |
|
126 | } |
|
127 | 15 | } |
128 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |