| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.monitor.api; |
| 21 |
|
|
| 22 |
|
import java.net.URL; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.Iterator; |
| 25 |
|
import java.util.Map; |
| 26 |
|
|
| 27 |
|
import org.apache.commons.collections4.queue.CircularFifoQueue; |
| 28 |
|
import org.slf4j.Logger; |
| 29 |
|
|
| 30 |
|
import com.xpn.xwiki.XWikiContext; |
| 31 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
| 32 |
|
|
| |
|
| 0% |
Uncovered Elements: 197 (197) |
Complexity: 58 |
Complexity Density: 0.48 |
|
| 33 |
|
public class MonitorPlugin extends XWikiDefaultPlugin |
| 34 |
|
{ |
| 35 |
|
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MonitorPlugin.class); |
| 36 |
|
|
| 37 |
|
private boolean bActive; |
| 38 |
|
|
| 39 |
|
private long duration = 0; |
| 40 |
|
|
| 41 |
|
private long nbrequests = 0; |
| 42 |
|
|
| 43 |
|
private Map<String, MonitorTimerSummary> timerSummaries = new HashMap<>(); |
| 44 |
|
|
| 45 |
|
private CircularFifoQueue<MonitorData> lastTimerDataList = new CircularFifoQueue<>(); |
| 46 |
|
|
| 47 |
|
private CircularFifoQueue<MonitorData> lastUnfinishedTimerDataList = new CircularFifoQueue<>(); |
| 48 |
|
|
| 49 |
|
private Map<Thread, MonitorData> activeTimerDataList = new HashMap<>(); |
| 50 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 51 |
0 |
public MonitorPlugin(String name, String className, XWikiContext context)... |
| 52 |
|
{ |
| 53 |
0 |
super(name, className, context); |
| 54 |
|
} |
| 55 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 56 |
0 |
@Override... |
| 57 |
|
public void init(XWikiContext context) |
| 58 |
|
{ |
| 59 |
0 |
super.init(context); |
| 60 |
0 |
reset(context); |
| 61 |
0 |
long iActive = context.getWiki().ParamAsLong("xwiki.monitor", 0); |
| 62 |
0 |
setActive((iActive > 0)); |
| 63 |
|
} |
| 64 |
|
|
| |
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 65 |
0 |
public void reset(XWikiContext context)... |
| 66 |
|
{ |
| 67 |
0 |
this.timerSummaries = new HashMap<>(); |
| 68 |
0 |
this.activeTimerDataList = new HashMap<>(); |
| 69 |
0 |
this.duration = 0; |
| 70 |
0 |
this.nbrequests = 0; |
| 71 |
0 |
long size = context.getWiki().ParamAsLong("xwiki.monitor.lastlistsize", 20); |
| 72 |
0 |
this.lastTimerDataList = new CircularFifoQueue<>((int) size); |
| 73 |
0 |
this.lastUnfinishedTimerDataList = new CircularFifoQueue<>((int) size); |
| 74 |
|
} |
| 75 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 76 |
0 |
@Override... |
| 77 |
|
public String getName() |
| 78 |
|
{ |
| 79 |
0 |
return "monitor"; |
| 80 |
|
} |
| 81 |
|
|
| |
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 6 |
Complexity Density: 0.38 |
|
| 82 |
0 |
public void startRequest(String page, String action, URL url)... |
| 83 |
|
{ |
| 84 |
0 |
if (isActive() == false) { |
| 85 |
0 |
return; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
0 |
try { |
| 89 |
0 |
Thread cthread = Thread.currentThread(); |
| 90 |
0 |
MonitorData mdata = this.activeTimerDataList.get(cthread); |
| 91 |
0 |
if (mdata != null) { |
| 92 |
0 |
removeFromActiveTimerDataList(cthread); |
| 93 |
0 |
addToLastUnfinishedTimerDataList(mdata); |
| 94 |
0 |
if (LOGGER.isDebugEnabled()) { |
| 95 |
0 |
LOGGER.debug("MONITOR: Thread " + cthread.getName() + " for page " + mdata.getWikiPage() |
| 96 |
|
+ " did not call endRequest"); |
| 97 |
|
} |
| 98 |
0 |
mdata.endRequest(false); |
| 99 |
|
} |
| 100 |
0 |
mdata = new MonitorData(page, action, url, cthread.getName()); |
| 101 |
0 |
this.activeTimerDataList.put(cthread, mdata); |
| 102 |
|
} catch (Throwable e) { |
| 103 |
0 |
if (LOGGER.isDebugEnabled()) { |
| 104 |
0 |
LOGGER.debug("MONITOR: endRequest failed with exception " + e); |
| 105 |
0 |
e.printStackTrace(); |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 110 |
0 |
private void addToLastUnfinishedTimerDataList(MonitorData mdata)... |
| 111 |
|
{ |
| 112 |
0 |
this.lastUnfinishedTimerDataList.add(mdata); |
| 113 |
|
} |
| 114 |
|
|
| |
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 6 |
Complexity Density: 0.35 |
|
| 115 |
0 |
public void endRequest()... |
| 116 |
|
{ |
| 117 |
0 |
if (isActive() == false) { |
| 118 |
0 |
return; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
0 |
try { |
| 122 |
0 |
Thread cthread = Thread.currentThread(); |
| 123 |
0 |
MonitorData mdata = this.activeTimerDataList.get(cthread); |
| 124 |
0 |
if (mdata == null) { |
| 125 |
0 |
if (LOGGER.isDebugEnabled()) { |
| 126 |
0 |
LOGGER.debug("MONITOR: Thread " + cthread.getName() + " did not call startRequest"); |
| 127 |
|
} |
| 128 |
0 |
return; |
| 129 |
|
} |
| 130 |
0 |
mdata.endRequest(true); |
| 131 |
0 |
addDuration(mdata.getDuration()); |
| 132 |
0 |
addTimerDuration(mdata); |
| 133 |
0 |
removeFromActiveTimerDataList(cthread); |
| 134 |
0 |
addToTimerDataList(mdata); |
| 135 |
|
} catch (Throwable e) { |
| 136 |
0 |
if (LOGGER.isDebugEnabled()) { |
| 137 |
0 |
LOGGER.debug("MONITOR: endRequest failed with exception " + e); |
| 138 |
0 |
e.printStackTrace(); |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
} |
| 142 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 143 |
0 |
private void removeFromActiveTimerDataList(Thread cthread)... |
| 144 |
|
{ |
| 145 |
0 |
if (this.activeTimerDataList.containsKey(cthread)) { |
| 146 |
0 |
this.activeTimerDataList.remove(cthread); |
| 147 |
|
} |
| 148 |
|
} |
| 149 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 150 |
0 |
private void addToTimerDataList(MonitorData mdata)... |
| 151 |
|
{ |
| 152 |
0 |
this.lastTimerDataList.add(mdata); |
| 153 |
|
} |
| 154 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 155 |
0 |
public void setWikiPage(String page)... |
| 156 |
|
{ |
| 157 |
0 |
if (isActive() == false) { |
| 158 |
0 |
return; |
| 159 |
|
} |
| 160 |
|
|
| 161 |
0 |
try { |
| 162 |
0 |
Thread cthread = Thread.currentThread(); |
| 163 |
0 |
MonitorData mdata = this.activeTimerDataList.get(cthread); |
| 164 |
0 |
if (mdata != null) { |
| 165 |
0 |
mdata.setWikiPage(page); |
| 166 |
|
} |
| 167 |
|
} catch (Throwable e) { |
| 168 |
|
} |
| 169 |
|
} |
| 170 |
|
|
| |
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 171 |
0 |
private void addTimerDuration(MonitorData mdata)... |
| 172 |
|
{ |
| 173 |
0 |
Map<String, MonitorTimerSummary> map = mdata.getTimerSummaries(); |
| 174 |
0 |
Map<String, MonitorTimerSummary> gmap = getTimerSummaries(); |
| 175 |
0 |
Iterator<MonitorTimerSummary> it = map.values().iterator(); |
| 176 |
0 |
while (it.hasNext()) { |
| 177 |
0 |
MonitorTimerSummary stimer = it.next(); |
| 178 |
0 |
MonitorTimerSummary gtimer = gmap.get(stimer.getName()); |
| 179 |
0 |
if (gtimer == null) { |
| 180 |
0 |
gtimer = new MonitorTimerSummary(stimer.getName()); |
| 181 |
0 |
gmap.put(stimer.getName(), gtimer); |
| 182 |
|
} |
| 183 |
0 |
gtimer.add(stimer); |
| 184 |
|
} |
| 185 |
|
} |
| 186 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 187 |
0 |
private void addDuration(long duration)... |
| 188 |
|
{ |
| 189 |
0 |
this.duration += duration; |
| 190 |
0 |
this.nbrequests++; |
| 191 |
|
} |
| 192 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 193 |
0 |
public CircularFifoQueue<MonitorData> getLastTimerData()... |
| 194 |
|
{ |
| 195 |
0 |
return this.lastTimerDataList; |
| 196 |
|
} |
| 197 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 198 |
0 |
public CircularFifoQueue<MonitorData> getLastUnfinishedTimerData()... |
| 199 |
|
{ |
| 200 |
0 |
return this.lastUnfinishedTimerDataList; |
| 201 |
|
} |
| 202 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 203 |
0 |
public void startTimer(String timername)... |
| 204 |
|
{ |
| 205 |
0 |
startTimer(timername, null); |
| 206 |
|
} |
| 207 |
|
|
| |
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
| 208 |
0 |
public void startTimer(String timername, String desc)... |
| 209 |
|
{ |
| 210 |
0 |
if (isActive() == false) { |
| 211 |
0 |
return; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
0 |
try { |
| 215 |
0 |
Thread cthread = Thread.currentThread(); |
| 216 |
0 |
MonitorData mdata = this.activeTimerDataList.get(cthread); |
| 217 |
0 |
if (mdata != null) { |
| 218 |
0 |
mdata.startTimer(timername, desc); |
| 219 |
|
} |
| 220 |
|
} catch (Throwable e) { |
| 221 |
0 |
if (LOGGER.isDebugEnabled()) { |
| 222 |
0 |
LOGGER.debug("MONITOR: startRequest for timer " + timername + " failed with exception " + e); |
| 223 |
0 |
e.printStackTrace(); |
| 224 |
|
} |
| 225 |
|
} |
| 226 |
|
} |
| 227 |
|
|
| |
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
| 228 |
0 |
public void setTimerDesc(String timername, String desc)... |
| 229 |
|
{ |
| 230 |
0 |
if (isActive() == false) { |
| 231 |
0 |
return; |
| 232 |
|
} |
| 233 |
|
|
| 234 |
0 |
try { |
| 235 |
0 |
Thread cthread = Thread.currentThread(); |
| 236 |
0 |
MonitorData mdata = this.activeTimerDataList.get(cthread); |
| 237 |
0 |
if (mdata != null) { |
| 238 |
0 |
mdata.setTimerDetails(timername, desc); |
| 239 |
|
} |
| 240 |
|
} catch (Throwable e) { |
| 241 |
0 |
if (LOGGER.isDebugEnabled()) { |
| 242 |
0 |
LOGGER.debug("MONITOR: setTimerDesc for timer " + timername + " failed with exception " + e); |
| 243 |
0 |
e.printStackTrace(); |
| 244 |
|
} |
| 245 |
|
} |
| 246 |
|
} |
| 247 |
|
|
| |
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.5 |
|
| 248 |
0 |
public void endTimer(String timername)... |
| 249 |
|
{ |
| 250 |
0 |
if (isActive() == false) { |
| 251 |
0 |
return; |
| 252 |
|
} |
| 253 |
|
|
| 254 |
0 |
try { |
| 255 |
0 |
Thread cthread = Thread.currentThread(); |
| 256 |
0 |
MonitorData mdata = this.activeTimerDataList.get(cthread); |
| 257 |
0 |
if (mdata != null) { |
| 258 |
0 |
mdata.endTimer(timername); |
| 259 |
|
} |
| 260 |
|
} catch (Throwable e) { |
| 261 |
0 |
if (LOGGER.isDebugEnabled()) { |
| 262 |
0 |
LOGGER.debug("MONITOR: endRequest for timer " + timername + " failed with exception " + e); |
| 263 |
0 |
e.printStackTrace(); |
| 264 |
|
} |
| 265 |
|
} |
| 266 |
|
} |
| 267 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 268 |
0 |
public Map<Thread, MonitorData> getActiveTimerData()... |
| 269 |
|
{ |
| 270 |
0 |
return this.activeTimerDataList; |
| 271 |
|
} |
| 272 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 273 |
0 |
public Map<String, MonitorTimerSummary> getTimerSummaries()... |
| 274 |
|
{ |
| 275 |
0 |
return this.timerSummaries; |
| 276 |
|
} |
| 277 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 278 |
0 |
public long getDuration()... |
| 279 |
|
{ |
| 280 |
0 |
return this.duration; |
| 281 |
|
} |
| 282 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 283 |
0 |
public long getRequests()... |
| 284 |
|
{ |
| 285 |
0 |
return this.nbrequests; |
| 286 |
|
} |
| 287 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 288 |
0 |
public long getDuration(String timer)... |
| 289 |
|
{ |
| 290 |
0 |
MonitorTimerSummary tsummary = getTimerSummaries().get(timer); |
| 291 |
0 |
if (tsummary == null) { |
| 292 |
0 |
return 0; |
| 293 |
|
} else { |
| 294 |
0 |
return tsummary.getDuration(); |
| 295 |
|
} |
| 296 |
|
} |
| 297 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 298 |
0 |
public long getNbCalls(String timer)... |
| 299 |
|
{ |
| 300 |
0 |
MonitorTimerSummary tsummary = getTimerSummaries().get(timer); |
| 301 |
0 |
if (tsummary == null) { |
| 302 |
0 |
return 0; |
| 303 |
|
} else { |
| 304 |
0 |
return tsummary.getNbCalls(); |
| 305 |
|
} |
| 306 |
|
} |
| 307 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 308 |
0 |
public long getRequests(String timer)... |
| 309 |
|
{ |
| 310 |
0 |
MonitorTimerSummary tsummary = getTimerSummaries().get(timer); |
| 311 |
0 |
if (tsummary == null) { |
| 312 |
0 |
return 0; |
| 313 |
|
} else { |
| 314 |
0 |
return tsummary.getRequests(); |
| 315 |
|
} |
| 316 |
|
} |
| 317 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 318 |
0 |
public boolean isActive()... |
| 319 |
|
{ |
| 320 |
0 |
return this.bActive; |
| 321 |
|
} |
| 322 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 323 |
0 |
public void setActive(boolean bActive)... |
| 324 |
|
{ |
| 325 |
0 |
this.bActive = bActive; |
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
} |