1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.monitor.api

File MonitorPlugin.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

50
120
27
1
328
264
58
0.48
4.44
27
2.15

Classes

Class Line # Actions
MonitorPlugin 33 120 0% 58 197
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software 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 GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   
 
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   
 
51  0 toggle public MonitorPlugin(String name, String className, XWikiContext context)
52    {
53  0 super(name, className, context);
54    }
55   
 
56  0 toggle @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   
 
65  0 toggle 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   
 
76  0 toggle @Override
77    public String getName()
78    {
79  0 return "monitor";
80    }
81   
 
82  0 toggle 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   
 
110  0 toggle private void addToLastUnfinishedTimerDataList(MonitorData mdata)
111    {
112  0 this.lastUnfinishedTimerDataList.add(mdata);
113    }
114   
 
115  0 toggle 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   
 
143  0 toggle private void removeFromActiveTimerDataList(Thread cthread)
144    {
145  0 if (this.activeTimerDataList.containsKey(cthread)) {
146  0 this.activeTimerDataList.remove(cthread);
147    }
148    }
149   
 
150  0 toggle private void addToTimerDataList(MonitorData mdata)
151    {
152  0 this.lastTimerDataList.add(mdata);
153    }
154   
 
155  0 toggle 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   
 
171  0 toggle 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   
 
187  0 toggle private void addDuration(long duration)
188    {
189  0 this.duration += duration;
190  0 this.nbrequests++;
191    }
192   
 
193  0 toggle public CircularFifoQueue<MonitorData> getLastTimerData()
194    {
195  0 return this.lastTimerDataList;
196    }
197   
 
198  0 toggle public CircularFifoQueue<MonitorData> getLastUnfinishedTimerData()
199    {
200  0 return this.lastUnfinishedTimerDataList;
201    }
202   
 
203  0 toggle public void startTimer(String timername)
204    {
205  0 startTimer(timername, null);
206    }
207   
 
208  0 toggle 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   
 
228  0 toggle 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   
 
248  0 toggle 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   
 
268  0 toggle public Map<Thread, MonitorData> getActiveTimerData()
269    {
270  0 return this.activeTimerDataList;
271    }
272   
 
273  0 toggle public Map<String, MonitorTimerSummary> getTimerSummaries()
274    {
275  0 return this.timerSummaries;
276    }
277   
 
278  0 toggle public long getDuration()
279    {
280  0 return this.duration;
281    }
282   
 
283  0 toggle public long getRequests()
284    {
285  0 return this.nbrequests;
286    }
287   
 
288  0 toggle 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   
 
298  0 toggle 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   
 
308  0 toggle 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   
 
318  0 toggle public boolean isActive()
319    {
320  0 return this.bActive;
321    }
322   
 
323  0 toggle public void setActive(boolean bActive)
324    {
325  0 this.bActive = bActive;
326    }
327   
328    }