-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ExeTime to separate file (#134)
- Loading branch information
Showing
3 changed files
with
74 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/src/test/java/org/astraea/performance/ExeTimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |