Repository: skiptests/astraea
Pull request #134 (2021-11-30 18:33:53)

Move ExeTime to separate file
1. 把`ExeTime`移到獨立的檔案
2. 添加測試


Patch:
diff --git a/app/src/main/java/org/astraea/performance/ExeTime.java b/app/src/main/java/org/astraea/performance/ExeTime.java
new file mode 100644
index 00000000..225e255c
--- /dev/null
+++ b/app/src/main/java/org/astraea/performance/ExeTime.java
@@ -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);
+    }
+  }
+}
diff --git a/app/src/main/java/org/astraea/performance/Performance.java b/app/src/main/java/org/astraea/performance/Performance.java
index 4af2460e..24c34d82 100644
--- a/app/src/main/java/org/astraea/performance/Performance.java
+++ b/app/src/main/java/org/astraea/performance/Performance.java
@@ -9,7 +9,6 @@ import java.util.Map;
 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;
@@ -230,7 +229,7 @@ public class Performance {
                 + " 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(
@@ -289,43 +288,4 @@ public class Performance {
       }
     }
   }
-
-  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;
-      }
-    };
-  }
 }


Test patch:
diff --git a/app/src/test/java/org/astraea/performance/ExeTimeTest.java b/app/src/test/java/org/astraea/performance/ExeTimeTest.java
new file mode 100644
index 00000000..6446d0c9
--- /dev/null
+++ b/app/src/test/java/org/astraea/performance/ExeTimeTest.java
@@ -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"));
+  }
+}