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


The following commit(s) were added to refs/heads/master by this push:
     new e9546ca  Update examples-stress to Java 1.8.
e9546ca is described below

commit e9546ca4ddd3e0addfdd82c58d4344a37709e81b
Author: Alex Herbert <[email protected]>
AuthorDate: Fri Apr 12 22:15:25 2019 +0100

    Update examples-stress to Java 1.8.
    
    Add stop file functionality to the stress command allowing ordered
    shutdown.
---
 commons-rng-examples/examples-stress/pom.xml       |  4 +-
 .../rng/examples/stress/BridgeTestCommand.java     |  7 +-
 .../rng/examples/stress/ExamplesStressCommand.java | 14 +---
 .../rng/examples/stress/StressTestCommand.java     | 80 +++++++++++++++++++---
 4 files changed, 77 insertions(+), 28 deletions(-)

diff --git a/commons-rng-examples/examples-stress/pom.xml 
b/commons-rng-examples/examples-stress/pom.xml
index 8dc842c..f471600 100644
--- a/commons-rng-examples/examples-stress/pom.xml
+++ b/commons-rng-examples/examples-stress/pom.xml
@@ -36,8 +36,8 @@
   Code in this module is not part of the public API.</description>
 
   <properties>
-    <maven.compiler.source>1.7</maven.compiler.source>
-    <maven.compiler.target>1.7</maven.compiler.target>
+    <maven.compiler.source>1.8</maven.compiler.source>
+    <maven.compiler.target>1.8</maven.compiler.target>
 
     <!-- OSGi -->
     
<commons.osgi.symbolicName>org.apache.commons.rng.examples.stress</commons.osgi.symbolicName>
diff --git 
a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/BridgeTestCommand.java
 
b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/BridgeTestCommand.java
index b6cba94..311eff9 100644
--- 
a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/BridgeTestCommand.java
+++ 
b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/BridgeTestCommand.java
@@ -25,12 +25,10 @@ import java.io.BufferedOutputStream;
 import java.io.BufferedWriter;
 import java.io.DataOutputStream;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.nio.ByteOrder;
-import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Callable;
@@ -108,8 +106,7 @@ class BridgeTestCommand implements Callable<Void> {
                 DataOutputStream dataOutput = new DataOutputStream(
                     new 
BufferedOutputStream(testingProcess.getOutputStream()));
                 // Open the file for Java int data
-                BufferedWriter textOutput = new BufferedWriter(
-                    new OutputStreamWriter(new FileOutputStream(dataFile), 
StandardCharsets.UTF_8))) {
+                BufferedWriter textOutput = 
Files.newBufferedWriter(dataFile.toPath())) {
 
                 final boolean littleEndian = byteOrder == 
ByteOrder.LITTLE_ENDIAN;
                 // Write int data using a single bit in all possible positions
diff --git 
a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ExamplesStressCommand.java
 
b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ExamplesStressCommand.java
index ef4a087..3306dc6 100644
--- 
a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ExamplesStressCommand.java
+++ 
b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/ExamplesStressCommand.java
@@ -20,7 +20,6 @@ import org.apache.commons.rng.UniformRandomProvider;
 
 import picocli.CommandLine;
 import picocli.CommandLine.Command;
-import picocli.CommandLine.ITypeConverter;
 import picocli.CommandLine.Mixin;
 import picocli.CommandLine.Model.CommandSpec;
 import picocli.CommandLine.RunLast;
@@ -68,15 +67,6 @@ class ExamplesStressCommand implements Callable<Void> {
      * @param args Application's arguments.
      */
     public static void main(String[] args) {
-        // Allow the input seed to be specified using hex (starting 0x, 0X, #) 
or
-        // octal (starting with 0)
-        final ITypeConverter<Long> longConverter = new ITypeConverter<Long>() {
-            @Override
-            public Long convert(String s) {
-                return Long.decode(s);
-            }
-        };
-
         // Build the command line manually so we can configure options.
         final CommandLine cmd = new CommandLine(new ExamplesStressCommand())
                 .addSubcommand("bridge", new CommandLine(new 
BridgeTestCommand())
@@ -84,7 +74,9 @@ class ExamplesStressCommand implements Callable<Void> {
                 .addSubcommand("endian", new EndianessCommand())
                 .addSubcommand("list",   new ListCommand())
                 .addSubcommand("output", new CommandLine(new OutputCommand())
-                                                         
.registerConverter(Long.class, longConverter))
+                                                         // Allow the input 
seed using hex (0x, 0X, #)
+                                                         // or octal (starting 
with 0)
+                                                         
.registerConverter(Long.class, Long::decode))
                 .addSubcommand("stress", new CommandLine(new 
StressTestCommand())
                                                          
.setStopAtPositional(true))
                 // Call last to apply to all sub-commands
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 74da58a..975bd8a 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
@@ -29,13 +29,10 @@ import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.DataOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
 import java.nio.ByteOrder;
-import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -46,6 +43,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Specification for the "stress" command.
@@ -79,6 +77,13 @@ class StressTestCommand implements Callable<Void> {
             description = "Results file prefix (default: ${DEFAULT-VALUE}).")
     private File fileOutputPrefix = new File("test_");
 
+    /** The file output prefix. */
+    @Option(names = {"--stop-file"},
+            description = {"Stop file (default: <Results file prefix>.stop).",
+                           "When created it will prevent new tasks from 
starting " +
+                           "but running tasks will complete."})
+    private File stopFile;
+
     /** The output mode for existing files. */
     @Option(names = {"-o", "--output-mode"},
             description = {"Output mode for existing files (default: 
${DEFAULT-VALUE}).",
@@ -123,6 +128,11 @@ class StressTestCommand implements Callable<Void> {
                           "but the stress test is not executed.")
     private boolean dryRun = false;
 
+    /** The stop file exists flag. This is set in a synchronised method. */
+    private boolean stopFileExists;
+    /** The timestamp when the stop file was last checked. */
+    private long stopFileTimestamp;
+
     /**
      * The output mode for existing files.
      */
@@ -146,6 +156,7 @@ class StressTestCommand implements Callable<Void> {
         LogUtils.setLogLevel(reusableOptions.logLevel);
         ProcessUtils.checkExecutable(executable);
         ProcessUtils.checkOutputDirectory(fileOutputPrefix);
+        checkStopFileDoesNotExist();
         final Iterable<StressTestData> stressTestData = createStressTestData();
         printStressTestData(stressTestData);
         runStressTest(stressTestData);
@@ -153,6 +164,46 @@ class StressTestCommand implements Callable<Void> {
     }
 
     /**
+     * Initialise the stop file to a default unless specified by the user, 
then check it
+     * does not currently exist.
+     *
+     * @throws ApplicationException If the stop file exists
+     */
+    private void checkStopFileDoesNotExist() {
+        if (stopFile == null) {
+            stopFile = new File(fileOutputPrefix + ".stop");
+        }
+        if (stopFile.exists()) {
+            throw new ApplicationException("Stop file exists: " + stopFile);
+        }
+    }
+
+    /**
+     * Check if the stop file exists.
+     *
+     * <p>This method is synchronized. It will log a message if the file 
exists one time only.
+     *
+     * @return true if the stop file exists
+     */
+    private synchronized boolean stopFileExists() {
+        if (!stopFileExists) {
+            // This should hit the filesystem each time it is called.
+            // To prevent this happening a lot when all the first set of tasks 
run use
+            // a timestamp to limit the check to 1 time each interval.
+            final long timestamp = System.currentTimeMillis();
+            if (timestamp > stopFileTimestamp) {
+                stopFileTimestamp = timestamp + TimeUnit.SECONDS.toMillis(2);
+                stopFileExists = stopFile.exists();
+                if (stopFileExists) {
+                    LogUtils.info("Stop file detected: " + stopFile);
+                    LogUtils.info("No further tasks will start");
+                }
+            }
+        }
+        return stopFileExists;
+    }
+
+    /**
      * Creates the test data.
      *
      * <p>If the input file is null then a default list is created.
@@ -165,8 +216,7 @@ class StressTestCommand implements Callable<Void> {
             return new StressTestDataList("", trials);
         }
         // Read data into a list
-        try (BufferedReader reader = new BufferedReader(
-                new InputStreamReader(new FileInputStream(generatorsListFile), 
StandardCharsets.UTF_8))) {
+        try (BufferedReader reader = 
Files.newBufferedReader(generatorsListFile.toPath())) {
             return ListCommand.readStressTestData(reader);
         } catch (final IOException ex) {
             throw new ApplicationException("Failed to read generators list: " 
+ generatorsListFile, ex);
@@ -205,6 +255,7 @@ class StressTestCommand implements Callable<Void> {
         checkExistingOutputFiles(basePath, stressTestData);
 
         LogUtils.info("Running stress test ...");
+        LogUtils.info("Shutdown by creating stop file: " + stopFile);
         final ProgressTracker progressTracker = new 
ProgressTracker(countTrials(stressTestData));
 
         // Run tasks with parallel execution.
@@ -233,6 +284,8 @@ class StressTestCommand implements Callable<Void> {
             // Terminate all threads.
             service.shutdown();
         }
+
+        LogUtils.info("Finished stress test");
     }
 
     /**
@@ -324,6 +377,7 @@ class StressTestCommand implements Callable<Void> {
                 // Log the decision
                 LogUtils.info("%s existing output file: %s", outputMode, 
output);
                 if (outputMode == StressTestCommand.OutputMode.SKIP) {
+                    progressTracker.incrementProgress();
                     continue;
                 }
             }
@@ -347,7 +401,7 @@ class StressTestCommand implements Callable<Void> {
      */
     static class ProgressTracker {
         /** The reporting interval. */
-        private static final long REPORT_INTERVAL= 100;
+        private static final long REPORT_INTERVAL = 100;
         /** The total. */
         private final int total;
         /** The count. */
@@ -449,6 +503,11 @@ class StressTestCommand implements Callable<Void> {
         /** {@inheritDoc} */
         @Override
         public void run() {
+            if (cmd.stopFileExists()) {
+                // Do nothing
+                return;
+            }
+
             try {
                 printHeader();
 
@@ -581,8 +640,9 @@ class StressTestCommand implements Callable<Void> {
         private static void write(StringBuilder sb,
                                   File output,
                                   boolean append) throws IOException {
-            try (BufferedWriter w = new BufferedWriter(
-                    new OutputStreamWriter(new FileOutputStream(output, 
append), StandardCharsets.UTF_8))) {
+            try (BufferedWriter w = append ?
+                    Files.newBufferedWriter(output.toPath(), 
StandardOpenOption.APPEND) :
+                    Files.newBufferedWriter(output.toPath())) {
                 w.write(sb.toString());
             }
         }

Reply via email to