2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.synchronizer;
25 import java.util.concurrent.atomic.AtomicInteger;
27 import org.onap.aai.sparky.analytics.AveragingRingBuffer;
28 import org.onap.aai.sparky.synchronizer.config.TaskProcessorConfig;
31 * TODO: Fill in description.
35 public class TransactionRateController {
37 private AveragingRingBuffer responseTimeTracker;
38 private double msPerTransaction;
39 private int numThreads;
40 private TaskProcessorConfig config;
41 private long startTimeInMs;
42 private AtomicInteger numTransactions;
45 * Instantiates a new transaction rate controller.
47 * @param config the config
49 public TransactionRateController(TaskProcessorConfig 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);
61 * Track response time.
63 * @param responseTimeInMs the response time in ms
65 public void trackResponseTime(long responseTimeInMs) {
66 this.numTransactions.incrementAndGet();
67 responseTimeTracker.addSample(responseTimeInMs);
70 public long getFixedDelayInMs() {
73 * The math here is pretty simple:
75 * 1. Target TPS is 10. Then the msPerTxn = 1000/10 = 100ms
77 * 2. If the calculated avgResponseTime = 40 ms, then the proposed delay is 60ms per thread.
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.
83 double proposedDelay = 0;
85 if (config.isTransactionRateControllerEnabled()) {
86 proposedDelay = ((msPerTransaction - responseTimeTracker.getAvg()) * this.numThreads);
88 if (proposedDelay > 0) {
89 return (long) (proposedDelay);
93 return (long) proposedDelay;
96 public long getAvg() {
97 return responseTimeTracker.getAvg();
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;