2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.synchronizer;
28 import java.util.concurrent.atomic.AtomicInteger;
30 import org.openecomp.sparky.analytics.AveragingRingBuffer;
31 import org.openecomp.sparky.synchronizer.config.TaskProcessorConfig;
34 * TODO: Fill in description.
38 public class TransactionRateController {
40 private AveragingRingBuffer responseTimeTracker;
41 private double msPerTransaction;
42 private int numThreads;
43 private TaskProcessorConfig config;
44 private long startTimeInMs;
45 private AtomicInteger numTransactions;
48 * Instantiates a new transaction rate controller.
50 * @param config the config
52 public TransactionRateController(TaskProcessorConfig 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);
64 * Track response time.
66 * @param responseTimeInMs the response time in ms
68 public void trackResponseTime(long responseTimeInMs) {
69 this.numTransactions.incrementAndGet();
70 responseTimeTracker.addSample(responseTimeInMs);
73 public long getFixedDelayInMs() {
76 * The math here is pretty simple:
78 * 1. Target TPS is 10. Then the msPerTxn = 1000/10 = 100ms
80 * 2. If the calculated avgResponseTime = 40 ms, then the proposed delay is 60ms per thread.
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.
86 double proposedDelay = 0;
88 if (config.isTransactionRateControllerEnabled()) {
89 proposedDelay = ((msPerTransaction - responseTimeTracker.getAvg()) * this.numThreads);
91 if (proposedDelay > 0) {
92 return (long) (proposedDelay);
96 return (long) proposedDelay;
99 public long getAvg() {
100 return responseTimeTracker.getAvg();
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;