Skip to content

Commit

Permalink
Move ExeTime to separate file (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
chia7712 authored Dec 1, 2021
1 parent 21c67c1 commit e106f17
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 41 deletions.
45 changes: 45 additions & 0 deletions app/src/main/java/org/astraea/performance/ExeTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.astraea.performance;

import com.beust.jcommander.IStringConverter;
import java.time.Duration;
import java.util.function.BiFunction;
import org.astraea.argument.ArgumentUtil;

/**
* Two kind of running modes. One runs for a duration of time. The other runs for a number of
* records.
*/
interface ExeTime {

double percentage(long records, long elapsedTime);

static ExeTime of(String exeTime) {
if (exeTime.endsWith("records")) {
final long records = Long.parseLong(exeTime.replace("records", ""));
return ExeTime.of((completeRecords, ignore) -> 100D * completeRecords / records, exeTime);
}
final Duration duration = new ArgumentUtil.DurationConverter().convert(exeTime);
return ExeTime.of((ignore, elapsedTime) -> 100D * elapsedTime / duration.toMillis(), exeTime);
}

static ExeTime of(BiFunction<Long, Long, Double> function, String toString) {
return new ExeTime() {
@Override
public double percentage(long records, long duration) {
return function.apply(records, duration);
}

@Override
public String toString() {
return toString;
}
};
}

class Converter implements IStringConverter<ExeTime> {
@Override
public ExeTime convert(String value) {
return ExeTime.of(value);
}
}
}
42 changes: 1 addition & 41 deletions app/src/main/java/org/astraea/performance/Performance.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
Expand Down Expand Up @@ -230,7 +229,7 @@ static class Argument extends BasicArgumentWithPropFile {
+ " The duration formats accepted are (a number) + (a time unit)."
+ " The time units can be \"days\", \"day\", \"h\", \"m\", \"s, \"ms\","
+ " \"us\", \"ns\"",
converter = ExeTimeArgument.class)
converter = ExeTime.Converter.class)
ExeTime exeTime = ExeTime.of("1000records");

@Parameter(
Expand Down Expand Up @@ -289,43 +288,4 @@ public CompressionType convert(String value) {
}
}
}

static class ExeTimeArgument implements IStringConverter<ExeTime> {
@Override
public ExeTime convert(String value) {
return ExeTime.of(value);
}
}
}

/**
* Two kind of running modes. One runs for a duration of time. The other runs for a number of
* records.
*/
interface ExeTime {

double percentage(long records, long elapsedTime);

static ExeTime of(String exeTime) {
if (exeTime.endsWith("records")) {
final long records = Long.parseLong(exeTime.replace("records", ""));
return ExeTime.of((completeRecords, ignore) -> 100D * completeRecords / records, exeTime);
}
final Duration duration = new ArgumentUtil.DurationConverter().convert(exeTime);
return ExeTime.of((ignore, elapsedTime) -> 100D * elapsedTime / duration.toMillis(), exeTime);
}

static ExeTime of(BiFunction<Long, Long, Double> function, String toString) {
return new ExeTime() {
@Override
public double percentage(long records, long duration) {
return function.apply(records, duration);
}

@Override
public String toString() {
return toString;
}
};
}
}
28 changes: 28 additions & 0 deletions app/src/test/java/org/astraea/performance/ExeTimeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.astraea.performance;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ExeTimeTest {
private final ExeTime.Converter converter = new ExeTime.Converter();

@Test
void testRecords() {
var exeTime = converter.convert("1000records");
Assertions.assertEquals(0, exeTime.percentage(0, 10));
Assertions.assertEquals(100D, exeTime.percentage(1000, 10));
}

@Test
void testDuration() {
var exeTime = converter.convert("100ms");
Assertions.assertEquals(0, exeTime.percentage(1000, 0));
Assertions.assertEquals(100D, exeTime.percentage(1000, 100));
}

@Test
void testUnknownArgument() {
Assertions.assertThrows(IllegalArgumentException.class, () -> converter.convert("aaa"));
Assertions.assertThrows(IllegalArgumentException.class, () -> converter.convert("10record"));
}
}

0 comments on commit e106f17

Please sign in to comment.