Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / synchronizer / TransactionRateController.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.synchronizer;
24
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.onap.aai.sparky.analytics.AveragingRingBuffer;
28 import org.onap.aai.sparky.synchronizer.config.TaskProcessorConfig;
29
30 /**
31  * TODO: Fill in description.
32  * 
33  * @author davea.
34  */
35 public class TransactionRateController {
36
37   private AveragingRingBuffer responseTimeTracker;
38   private double msPerTransaction;
39   private int numThreads;
40   private TaskProcessorConfig config;
41   private long startTimeInMs;
42   private AtomicInteger numTransactions;
43
44   /**
45    * Instantiates a new transaction rate controller.
46    *
47    * @param config the config
48    */
49   public TransactionRateController(TaskProcessorConfig config) {
50
51     this.config = config;
52     this.responseTimeTracker = new AveragingRingBuffer(
53         config.getNumSamplesPerThreadForRunningAverage() * config.getMaxConcurrentWorkers());
54     this.msPerTransaction = 1000 / config.getTargetTps();
55     this.numThreads = config.getMaxConcurrentWorkers();
56     this.startTimeInMs = System.currentTimeMillis();
57     this.numTransactions = new AtomicInteger(0);
58   }
59
60   /**
61    * Track response time.
62    *
63    * @param responseTimeInMs the response time in ms
64    */
65   public void trackResponseTime(long responseTimeInMs) {
66     this.numTransactions.incrementAndGet();
67     responseTimeTracker.addSample(responseTimeInMs);
68   }
69
70   public long getFixedDelayInMs() {
71
72     /*
73      * The math here is pretty simple:
74      * 
75      * 1. Target TPS is 10. Then the msPerTxn = 1000/10 = 100ms
76      * 
77      * 2. If the calculated avgResponseTime = 40 ms, then the proposed delay is 60ms per thread.
78      * 
79      * 3. If the calculated avgResponseTime = 200ms, then the proposed delay is -100 ms, which is
80      * not possible, we can't speed it up, so we don't propose any further delay.
81      */
82
83     double proposedDelay = 0;
84
85     if (config.isTransactionRateControllerEnabled()) {
86       proposedDelay = ((msPerTransaction - responseTimeTracker.getAvg()) * this.numThreads);
87
88       if (proposedDelay > 0) {
89         return (long) (proposedDelay);
90       }
91     }
92
93     return (long) proposedDelay;
94   }
95
96   public long getAvg() {
97     return responseTimeTracker.getAvg();
98   }
99
100   public double getCurrentTps() {
101     if (numTransactions.get() > 0) {
102       double timeDelta = System.currentTimeMillis() - startTimeInMs;
103       double numTxns = numTransactions.get();
104       return (numTxns / timeDelta) * 1000.0;
105     }
106
107     return 0.0;
108   }
109
110 }