Skip to content

Commit

Permalink
Move the Time constants from PerlConfig to Time interface
Browse files Browse the repository at this point in the history
Signed-off-by: Keshava Munegowda <keshava.gowda@gmail.com>
  • Loading branch information
kmgowda committed Jan 2, 2022
1 parent cc6a875 commit 0a07e49
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 52 deletions.
3 changes: 1 addition & 2 deletions sbk-api/src/main/java/io/sbk/api/AbstractCallbackReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package io.sbk.api;

import io.sbk.config.PerlConfig;
import io.sbk.data.DataType;
import io.sbk.time.Time;

Expand Down Expand Up @@ -90,7 +89,7 @@ public void initialize(Worker reader, long secondsToRun, long recordsCount, Data
this.time = time;
this.readCnt = new AtomicLong(0);
this.beginTime = time.getCurrentTime();
this.msToRun = secondsToRun * PerlConfig.MS_PER_SEC;
this.msToRun = secondsToRun * Time.MS_PER_SEC;
this.recordsCount = recordsCount;
this.ret = new CompletableFuture<>();
start(callback);
Expand Down
5 changes: 2 additions & 3 deletions sbk-api/src/main/java/io/sbk/api/DataRecordsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package io.sbk.api;

import io.sbk.config.PerlConfig;
import io.sbk.data.DataType;
import io.sbk.perl.SendChannel;
import io.sbk.time.Time;
Expand Down Expand Up @@ -106,7 +105,7 @@ default void genericRecordsTimeReader(Worker reader, long secondsToRun, DataType
final long startTime = time.getCurrentTime();
final int size = reader.params.getRecordSize();
final Status status = new Status();
final long msToRun = secondsToRun * PerlConfig.MS_PER_SEC;
final long msToRun = secondsToRun * Time.MS_PER_SEC;
int id = reader.id % reader.recordIDMax;
while (time.elapsedMilliSeconds(status.endTime, startTime) < msToRun) {
recordTime.recordRead(dType, size, time, status, reader.sendChannel, id++);
Expand Down Expand Up @@ -207,7 +206,7 @@ default void genericRecordsTimeReaderRateControl(Worker reader, long secondsToRu
final long startTime = time.getCurrentTime();
final int size = reader.params.getRecordSize();
final Status status = new Status();
final long msToRun = secondsToRun * PerlConfig.MS_PER_SEC;
final long msToRun = secondsToRun * Time.MS_PER_SEC;
int id = reader.id % reader.recordIDMax;
final long loopStartTime = time.getCurrentTime();
double secondsElapsed = 0;
Expand Down
3 changes: 1 addition & 2 deletions sbk-api/src/main/java/io/sbk/api/DataRecordsWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package io.sbk.api;

import io.sbk.config.PerlConfig;
import io.sbk.data.DataType;
import io.sbk.perl.SendChannel;
import io.sbk.time.Time;
Expand Down Expand Up @@ -141,7 +140,7 @@ default void RecordsWriterSync(Worker writer, long recordsCount, DataType<T> dTy
default void RecordsWriterTime(Worker writer, long secondsToRun, DataType<T> dType, T data, int size,
Time time) throws IOException {
final Status status = new Status();
final long msToRun = secondsToRun * PerlConfig.MS_PER_SEC;
final long msToRun = secondsToRun * Time.MS_PER_SEC;
long startTime = time.getCurrentTime();
int id = writer.id % writer.recordIDMax;
status.startTime = startTime;
Expand Down
8 changes: 4 additions & 4 deletions sbk-api/src/main/java/io/sbk/api/impl/SbkBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public SbkBenchmark(Action action, PerlConfig perlConfig,
executor = perlConfig.fork ? new ForkJoinPool(threadCount) : Executors.newFixedThreadPool(threadCount);
writeStats = params.getWritersCount() > 0 && !params.isWriteAndRead() ?
new CQueuePerformance(perlConfig, params.getWritersCount(), createLatencyRecorder(),
logger.getReportingIntervalSeconds() * PerlConfig.MS_PER_SEC, params.getTimeoutMS(),
logger.getReportingIntervalSeconds() * Time.MS_PER_SEC, params.getTimeoutMS(),
this.time, executor) : null;

readStats = params.getReadersCount() > 0 ?
new CQueuePerformance(perlConfig, params.getReadersCount(), createLatencyRecorder(),
logger.getReportingIntervalSeconds() * PerlConfig.MS_PER_SEC, params.getTimeoutMS(),
logger.getReportingIntervalSeconds() * Time.MS_PER_SEC, params.getTimeoutMS(),
this.time, executor) : null;
timeoutExecutor = Executors.newScheduledThreadPool(1);
retFuture = new CompletableFuture<>();
Expand Down Expand Up @@ -303,7 +303,7 @@ public CompletableFuture<Void> start() throws IOException, InterruptedException,
i += params.getWritersStep();
if (params.getWritersStepSeconds() > 0 && i < params.getWritersCount()) {
try {
Thread.sleep((long) params.getWritersStepSeconds() * PerlConfig.MS_PER_SEC);
Thread.sleep((long) params.getWritersStepSeconds() * Time.MS_PER_SEC);
if (params.getTotalSecondsToRun() > 0) {
secondsToRun -= params.getWritersStepSeconds();
if (secondsToRun <= 0) {
Expand Down Expand Up @@ -355,7 +355,7 @@ public CompletableFuture<Void> start() throws IOException, InterruptedException,
i += params.getReadersStep();
if (params.getReadersStepSeconds() > 0 && i < params.getReadersCount()) {
try {
Thread.sleep((long) params.getReadersStepSeconds() * PerlConfig.MS_PER_SEC);
Thread.sleep((long) params.getReadersStepSeconds() * Time.MS_PER_SEC);
if (params.getTotalSecondsToRun() > 0) {
secondsToRun -= params.getReadersStepSeconds();
if (secondsToRun <= 0) {
Expand Down
3 changes: 1 addition & 2 deletions sbk-api/src/main/java/io/sbk/api/impl/SbkCallbackReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.sbk.api.Callback;
import io.sbk.api.ParameterOptions;
import io.sbk.api.Worker;
import io.sbk.config.PerlConfig;
import io.sbk.data.DataType;
import io.sbk.perl.SendChannel;
import io.sbk.time.Time;
Expand Down Expand Up @@ -44,7 +43,7 @@ public SbkCallbackReader(int readerId, int idMax, ParameterOptions params, SendC
this.ret = new CompletableFuture<>();
this.readCnt = new AtomicLong(0);
this.beginTime = 0;
this.msToRun = params.getTotalSecondsToRun() * PerlConfig.MS_PER_SEC;
this.msToRun = params.getTotalSecondsToRun() * Time.MS_PER_SEC;
this.totalRecords = params.getTotalRecords();

if (params.isWriteAndRead()) {
Expand Down
10 changes: 5 additions & 5 deletions sbk-api/src/main/java/io/sbk/api/impl/SbkRateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
package io.sbk.api.impl;

import io.sbk.api.RateController;
import io.sbk.config.PerlConfig;
import io.sbk.time.Time;

final public class SbkRateController implements RateController {
private static final long MIN_SLEEP_NS = 2 * PerlConfig.NS_PER_MS;
private static final long MIN_SLEEP_NS = 2 * Time.NS_PER_MS;
private long sleepTimeNs;
private int recordsPerSec;
private long toSleepNs;
Expand All @@ -33,7 +33,7 @@ public SbkRateController() {
public void start(final int recordsPerSec) {
this.recordsPerSec = recordsPerSec;
this.sleepTimeNs = this.recordsPerSec > 0 ?
PerlConfig.NS_PER_SEC / this.recordsPerSec : 0;
Time.NS_PER_SEC / this.recordsPerSec : 0;
}

/**
Expand Down Expand Up @@ -61,8 +61,8 @@ private void needSleep(final long events, final double elapsedSec) {
if (toSleepNs >= MIN_SLEEP_NS) {
long sleepStart = System.nanoTime();
try {
final long sleepMs = toSleepNs / PerlConfig.NS_PER_MS;
final long sleepNs = toSleepNs - (sleepMs * PerlConfig.NS_PER_MS);
final long sleepMs = toSleepNs / Time.NS_PER_MS;
final long sleepNs = toSleepNs - (sleepMs * Time.NS_PER_MS);
Thread.sleep(sleepMs, (int) sleepNs);
} catch (InterruptedException e) {
// will be taken care in finally block
Expand Down
16 changes: 5 additions & 11 deletions sbk-api/src/main/java/io/sbk/config/PerlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package io.sbk.config;

import io.sbk.time.Time;
import io.sbk.time.TimeUnit;

final public class PerlConfig {
Expand All @@ -21,23 +22,16 @@ final public class PerlConfig {
final public static long BYTES_PER_GB = ((long) BYTES_PER_MB) * BYTES_PER_MB;

final public static int DEFAULT_REPORTING_INTERVAL_SECONDS = 5;
final public static int NS_PER_MICRO = 1000;
final public static int MICROS_PER_MS = 1000;
final public static int MS_PER_SEC = 1000;
final public static int MICROS_PER_SEC = MICROS_PER_MS * MS_PER_SEC;
final public static int DEFAULT_TIMEOUT_MS = MS_PER_SEC;
final public static long DEFAULT_RUNTIME_SECONDS = Long.MAX_VALUE / MS_PER_SEC;
final public static int DEFAULT_TIMEOUT_MS = Time.MS_PER_SEC;
final public static long DEFAULT_RUNTIME_SECONDS = Long.MAX_VALUE / Time.MS_PER_SEC;

final public static long LONG_MAX = Long.MAX_VALUE >> 2;
final public static long TOTAL_LATENCY_MAX = Long.MAX_VALUE >> 1;

final public static int MIN_Q_PER_WORKER = 1;

final public static int MS_PER_MIN = MS_PER_SEC * 60;
final public static int DEFAULT_MAX_LATENCY = MS_PER_MIN * 3;
final public static int NS_PER_MS = NS_PER_MICRO * MICROS_PER_MS;
final public static long NS_PER_SEC = MS_PER_SEC * NS_PER_MS;
final public static int MIN_IDLE_NS = NS_PER_MICRO;
final public static int DEFAULT_MAX_LATENCY = Time.MS_PER_MIN * 3;
final public static int MIN_IDLE_NS = Time.NS_PER_MICRO;
final public static int DEFAULT_MIN_LATENCY = 0;
final public static double[] PERCENTILES = {10, 25, 50, 75, 95, 99, 99.9, 99.99};

Expand Down
4 changes: 2 additions & 2 deletions sbk-api/src/main/java/io/sbk/perl/impl/CQueuePerformance.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public CQueuePerformance(@NotNull PerlConfig perlConfig, int workers, PeriodicRe


private void runPerformance(final long secondsToRun, final long totalRecords) {
final long msToRun = secondsToRun * PerlConfig.MS_PER_SEC;
final long msToRun = secondsToRun * Time.MS_PER_SEC;
final ElasticWaitCounter idleCounter = new ElasticWaitCounter(windowIntervalMS, timeoutMS, idleNS);
final long startTime = time.getCurrentTime();
boolean doWork = true;
Expand Down Expand Up @@ -223,7 +223,7 @@ final static private class ElasticWaitCounter {
public ElasticWaitCounter(int windowInterval, int timeoutMS, int idleNS) {
this.windowInterval = windowInterval;
this.idleNS = idleNS;
countRatio = (PerlConfig.NS_PER_MS * 1.0) / this.idleNS;
countRatio = (Time.NS_PER_MS * 1.0) / this.idleNS;
minIdleCount = (long) (countRatio * timeoutMS);
elasticCount = minIdleCount;
idleCount = 0;
Expand Down
12 changes: 5 additions & 7 deletions sbk-api/src/main/java/io/sbk/time/MicroSeconds.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

package io.sbk.time;

import io.sbk.config.PerlConfig;

final public class MicroSeconds implements Time {

/**
Expand All @@ -30,7 +28,7 @@ public TimeUnit getTimeUnit() {
* @return current Time
*/
public long getCurrentTime() {
return System.nanoTime() / PerlConfig.NS_PER_MICRO;
return System.nanoTime() / Time.NS_PER_MICRO;
}

/**
Expand All @@ -41,7 +39,7 @@ public long getCurrentTime() {
* @return elapsed time in milliseconds
*/
public double elapsedMilliSeconds(long h, long l) {
return elapsed(h, l) / (PerlConfig.MICROS_PER_MS * 1.0);
return elapsed(h, l) / (Time.MICROS_PER_MS * 1.0);
}

/**
Expand All @@ -52,7 +50,7 @@ public double elapsedMilliSeconds(long h, long l) {
* @return elapsed time in seconds
*/
public double elapsedSeconds(long h, long l) {
return elapsed(h, l) / (PerlConfig.MICROS_PER_SEC * 1.0);
return elapsed(h, l) / (Time.MICROS_PER_SEC * 1.0);
}

/**
Expand All @@ -62,7 +60,7 @@ public double elapsedSeconds(long h, long l) {
* @return converted time in nanoseconds
*/
public double convertToNanoSeconds(double t) {
return t * PerlConfig.NS_PER_MICRO;
return t * Time.NS_PER_MICRO;
}

/**
Expand All @@ -82,7 +80,7 @@ public double convertToMicroSeconds(double t) {
* @return converted time in Milliseconds
*/
public double convertToMilliSeconds(double t) {
return t / PerlConfig.MICROS_PER_MS;
return t / Time.MICROS_PER_MS;
}

}
8 changes: 3 additions & 5 deletions sbk-api/src/main/java/io/sbk/time/MilliSeconds.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

package io.sbk.time;

import io.sbk.config.PerlConfig;

final public class MilliSeconds implements Time {

/**
Expand Down Expand Up @@ -52,7 +50,7 @@ public double elapsedMilliSeconds(long h, long l) {
* @return elapsed time in seconds
*/
public double elapsedSeconds(long h, long l) {
return elapsed(h, l) / (PerlConfig.MS_PER_SEC * 1.0);
return elapsed(h, l) / (Time.MS_PER_SEC * 1.0);
}

/**
Expand All @@ -62,7 +60,7 @@ public double elapsedSeconds(long h, long l) {
* @return converted time in nanoseconds
*/
public double convertToNanoSeconds(double t) {
return t * PerlConfig.NS_PER_MS;
return t * Time.NS_PER_MS;
}

/**
Expand All @@ -72,7 +70,7 @@ public double convertToNanoSeconds(double t) {
* @return converted time in microseconds
*/
public double convertToMicroSeconds(double t) {
return t * PerlConfig.MICROS_PER_MS;
return t * Time.MICROS_PER_MS;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions sbk-api/src/main/java/io/sbk/time/NanoSeconds.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

package io.sbk.time;

import io.sbk.config.PerlConfig;

final public class NanoSeconds implements Time {

/**
Expand Down Expand Up @@ -41,7 +39,7 @@ public long getCurrentTime() {
* @return elapsed time in milliseconds
*/
public double elapsedMilliSeconds(long h, long l) {
return elapsed(h, l) / (PerlConfig.NS_PER_MS * 1.0);
return elapsed(h, l) / (Time.NS_PER_MS * 1.0);
}

/**
Expand All @@ -52,7 +50,7 @@ public double elapsedMilliSeconds(long h, long l) {
* @return elapsed time in seconds
*/
public double elapsedSeconds(long h, long l) {
return elapsed(h, l) / (PerlConfig.NS_PER_SEC * 1.0);
return elapsed(h, l) / (Time.NS_PER_SEC * 1.0);
}

/**
Expand All @@ -72,7 +70,7 @@ public double convertToNanoSeconds(double t) {
* @return converted time in microseconds
*/
public double convertToMicroSeconds(double t) {
return t / PerlConfig.NS_PER_MICRO;
return t / Time.NS_PER_MICRO;
}

/**
Expand All @@ -82,7 +80,7 @@ public double convertToMicroSeconds(double t) {
* @return converted time in Milliseconds
*/
public double convertToMilliSeconds(double t) {
return t / PerlConfig.NS_PER_MS;
return t / Time.NS_PER_MS;
}

}
9 changes: 9 additions & 0 deletions sbk-api/src/main/java/io/sbk/time/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

public sealed interface Time permits MilliSeconds, MicroSeconds, NanoSeconds {

int NS_PER_MICRO = 1000;
int MICROS_PER_MS = 1000;
int MS_PER_SEC = 1000;

int NS_PER_MS = NS_PER_MICRO * MICROS_PER_MS;
long NS_PER_SEC = MS_PER_SEC * NS_PER_MS;
int MS_PER_MIN = MS_PER_SEC * 60;
int MICROS_PER_SEC = MICROS_PER_MS * MS_PER_SEC;

/**
* get the Time Unit.
*
Expand Down
4 changes: 2 additions & 2 deletions sbk-gem/src/main/java/io/sbk/gem/impl/SbkGemParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

import io.sbk.api.impl.SbkDriversParameters;
import io.sbk.config.GemConfig;
import io.sbk.config.PerlConfig;
import io.sbk.exception.HelpException;
import io.sbk.gem.GemParameterOptions;
import io.sbk.gem.SshConnection;
import io.sbk.system.Printer;
import io.sbk.time.Time;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.ParseException;
Expand Down Expand Up @@ -56,7 +56,7 @@ public final class SbkGemParameters extends SbkDriversParameters implements GemP
public SbkGemParameters(String name, String[] drivers, @NotNull GemConfig config, int ramport) {
super(name, GemConfig.DESC, drivers);
this.config = config;
this.timeoutMS = config.timeoutSeconds * PerlConfig.MS_PER_SEC;
this.timeoutMS = config.timeoutSeconds * Time.MS_PER_SEC;
this.ramPort = ramport;
try {
this.localHost = InetAddress.getLocalHost().getHostName();
Expand Down
2 changes: 1 addition & 1 deletion sbk-ram/src/main/java/io/sbk/ram/impl/SbkRamBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public SbkRamBenchmark(RamConfig ramConfig, RamParameterOptions params,
queue = new LinkedBlockingQueue<>();
latencyRecorder = createLatencyRecorder();
benchmark = new RamBenchmark(ramConfig.maxQueues, ramConfig.idleMS, time, latencyRecorder,
logger.getReportingIntervalSeconds() * PerlConfig.MS_PER_SEC);
logger.getReportingIntervalSeconds() * Time.MS_PER_SEC);
service = new SbkGrpcService(params, time, logger.getMinLatency(), logger.getMaxLatency(), logger, benchmark);
server = ServerBuilder.forPort(params.getRamPort()).addService(service).directExecutor().build();
retFuture = new CompletableFuture<>();
Expand Down

0 comments on commit 0a07e49

Please sign in to comment.