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