You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/10/23 16:57:05 UTC

[commons-rng] 01/09: Add hex seed option to stress command.

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git

commit b63f5f0d8082f880cb94b9ed3dbd0249554767e9
Author: aherbert <ah...@apache.org>
AuthorDate: Wed Oct 23 11:26:28 2019 +0100

    Add hex seed option to stress command.
    
    Allows the output command and stress command to stream the same data.
    Also allows tests to be repeated.
---
 .../commons/rng/examples/stress/OutputCommand.java |  2 +-
 .../rng/examples/stress/StressTestCommand.java     | 27 +++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/OutputCommand.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/OutputCommand.java
index b27b197..305f957 100644
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/OutputCommand.java
+++ b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/OutputCommand.java
@@ -97,7 +97,7 @@ class OutputCommand implements Callable<Void> {
     /** The random seed as a byte[]. */
     @Option(names = {"-x", "--hex-seed"},
             description = {"The hex-encoded random seed.",
-                           "Bytes for other primitives use little-endian format.",
+                           "Seed conversion for multi-byte primitives use little-endian format.",
                            "Over-rides the --seed parameter."})
     private String byteSeed;
 
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java
index a963ede..53b0ec0 100644
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java
+++ b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java
@@ -160,6 +160,13 @@ class StressTestCommand implements Callable<Void> {
                            "generators sequentially, each appropriately byte reversed for the platform."})
     private boolean raw64;
 
+    /** The random seed as a byte[]. */
+    @Option(names = {"-x", "--hex-seed"},
+            description = {"The hex-encoded random seed.",
+                           "Seed conversion for multi-byte primitives use little-endian format.",
+                           "Use to repeat tests. Not recommended for batch testing."})
+    private String byteSeed;
+
     /**
      * Flag to indicate the output should be combined with a hashcode from a new object.
      * This is a method previously used in the
@@ -456,7 +463,7 @@ class StressTestCommand implements Callable<Void> {
                     }
                 }
                 // Create the generator. Explicitly create a seed so it can be recorded.
-                final byte[] seed = testData.getRandomSource().createSeed();
+                final byte[] seed = createSeed(testData.getRandomSource());
                 UniformRandomProvider rng = testData.createRNG(seed);
 
                 // Upper or lower bits from 64-bit generators must be created first.
@@ -498,6 +505,24 @@ class StressTestCommand implements Callable<Void> {
     }
 
     /**
+     * Creates the seed. This will call {@link RandomSource#createSeed()} unless a hex seed has
+     * been explicitly specified on the command line.
+     *
+     * @param randomSource Random source.
+     * @return the seed
+     */
+    private byte[] createSeed(RandomSource randomSource) {
+        if (byteSeed != null) {
+            try {
+                return Hex.decodeHex(byteSeed);
+            } catch (IllegalArgumentException ex) {
+                throw new ApplicationException("Invalid hex seed: " + ex.getMessage(), ex);
+            }
+        }
+        return randomSource.createSeed();
+    }
+
+    /**
      * Submit the tasks to the executor service.
      *
      * @param service The executor service.