2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.sli.resource.dblib.pm;
23 import java.util.Iterator;
24 import java.util.Properties;
26 import java.util.Timer;
27 import java.util.TimerTask;
28 import java.util.TreeSet;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.concurrent.LinkedBlockingQueue;
34 import java.util.concurrent.atomic.AtomicLong;
36 public class PollingWorker implements Runnable {
38 private Logger LOGGER = LoggerFactory.getLogger(PollingWorker.class);
40 private static PollingWorker self = null;
42 private LinkedBlockingQueue tasks = new LinkedBlockingQueue(100);
43 private long interval = 1000L;
44 private Thread worker = null;
45 private AtomicLong[] counters = null;
46 private int[] bucketUnit = null;
47 private static boolean enabled = false;
48 private Timer timer = null;
50 public static void post(long starttime){
51 PollingWorker temp = self;
52 if(temp != null && enabled) {
53 temp.register(new TestSample(starttime));
57 public static void createInistance(Properties props){
58 self = new PollingWorker(props);
61 private PollingWorker(Properties ctxprops){
62 if(ctxprops==null || ctxprops.getProperty("org.openecomp.sdnc.dblib.pm") == null){
65 if("true".equalsIgnoreCase((String)ctxprops.getProperty("org.openecomp.sdnc.dblib.pm"))){
72 interval = Long.parseLong(( ctxprops == null || ctxprops.getProperty("org.openecomp.sdnc.dblib.pm.interval") == null) ? "60" : (String)ctxprops.getProperty("org.openecomp.sdnc.dblib.pm.interval"));
73 // '0' bucket is to count exceptions
74 String sampling[] = ((ctxprops == null || ctxprops.getProperty("org.openecomp.sdnc.dblib.pm.sampling")==null) ? "0,2,5,10,20,50,100" : (String)ctxprops.getProperty("org.openecomp.sdnc.dblib.pm.sampling")).split(",");
77 bucketUnit = new int[sampling.length];
78 for(int i=0, max = bucketUnit.length; i<max; i++){
79 bucketUnit[i] = Integer.parseInt(sampling[i].trim());
81 counters = new AtomicLong[bucketUnit.length+1];
82 for(int i=0, max = counters.length; i<max; i++){
83 counters[i] = new AtomicLong();
85 worker = new Thread(this);
86 worker.setDaemon(true);
88 timer = new Timer(true);
89 timer.schedule(new MyTimerTask(), interval*1000L, interval*1000L);
93 private void register(TestSample object){
96 } catch(Throwable exc) {
97 // if cannot add an object to the queue, do nothing
101 private void deRegister(TestSample object){
102 tasks.remove(object);
107 Set data = new TreeSet();
109 for(Iterator it = data.iterator(); it.hasNext(); ){
110 Object next = it.next();
111 if(next instanceof TestSample){
112 consume((TestSample)next);
114 System.out.println(next.getClass().getName());
119 } catch (InterruptedException e) {
125 public void clearReqister(){
126 AtomicLong[] tmp = new AtomicLong[counters.length];
127 for(int i=0, max = tmp.length; i<max; i++){
128 tmp[i] = new AtomicLong();
130 AtomicLong[] tmp2 = counters;
134 StringBuffer sb = new StringBuffer("CPM: ");
135 for(int i=0, max = tmp2.length; i < max; i++){
136 if(i==0 && bucketUnit[0]==0){
140 if(i==bucketUnit.length){
141 sb.append("Other]=");
143 sb.append(bucketUnit[i]).append(" ms]=");
146 sb.append(tmp2[i].get()).append("\t");
148 LOGGER.info(sb.toString());
151 class MyTimerTask extends TimerTask{
160 private void consume(TestSample probe) {
161 AtomicLong[] tmp = counters;
163 counters[getBucket(probe.getDuration())].incrementAndGet();
168 * This method is used to find the offset of the bucket in
169 * counters. 'counters' array is 1 size longer than bucketUnit,
170 * hence by default it returns 'bucketUnit.length'
172 private int getBucket(long difftime){
173 for(int i=0; i<bucketUnit.length; i++){
174 if(difftime < bucketUnit[i]){
178 return bucketUnit.length;
181 private static boolean isEnabled() {
185 * @author Rich Tabedzki
186 * A helper class to pass measured parameter to the counter.
188 static class TestSample implements Comparable{
189 private long starttime;
190 private long endtime;
192 public TestSample(long starttime) {
193 this.endtime = System.currentTimeMillis();
194 this.starttime = starttime;
197 public long getDuration(){
198 return endtime - starttime;
201 public int compareTo(Object o) {
202 if(o instanceof TestSample){
203 TestSample x = (TestSample)o;
204 if(starttime < x.starttime)
206 if(endtime < x.endtime)
208 if(starttime > x.starttime)
210 if(endtime > x.endtime)