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/05/16 19:15:19 UTC

[commons-rng] branch master updated: Removed old stress test application code.

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 2e38645  Removed old stress test application code.
2e38645 is described below

commit 2e38645a534702f07b11461055b641f1ff4f912e
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Thu May 16 20:15:15 2019 +0100

    Removed old stress test application code.
---
 .../rng/examples/stress/GeneratorsList.java        |  76 -----
 .../rng/examples/stress/IntGeneratorsList.java     |  49 ---
 .../rng/examples/stress/LongGeneratorsList.java    |  49 ---
 .../rng/examples/stress/RandomStressTester.java    | 366 ---------------------
 4 files changed, 540 deletions(-)

diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/GeneratorsList.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/GeneratorsList.java
deleted file mode 100644
index 57bdf50..0000000
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/GeneratorsList.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rng.examples.stress;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.EnumSet;
-import java.util.Iterator;
-
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.simple.RandomSource;
-
-/**
- * List of generators.
- */
-public class GeneratorsList implements Iterable<UniformRandomProvider> {
-    /**
-     * The RandomSource values to ignore when auto generating the list
-     * from the enumeration.
-     */
-    private static final EnumSet<RandomSource> TO_IGNORE =
-            EnumSet.of(RandomSource.TWO_CMRES_SELECT);
-
-    /** List of generators. */
-    private final List<UniformRandomProvider> list = new ArrayList<>();
-
-    /**
-     * Creates the list.
-     */
-    public GeneratorsList() {
-        // Auto-generate using the order of the RandomSource enum
-        for (final RandomSource source : RandomSource.values()) {
-            // Ignore those generators known to take arguments
-            if (TO_IGNORE.contains(source)) {
-                continue;
-            }
-            // Currently we cannot detect if the source requires arguments,
-            // e.g. RandomSource.TWO_CMRES_SELECT. So try and create
-            // using no arguments and allow this to throw
-            // IllegalStateException if it cannot be created.
-            //
-            // Implementation note:
-            // Handle such generators by adding to the ignore set and
-            // if they must be included add as a special case below.
-            list.add(RandomSource.create(source));
-        }
-
-        // --------------------------------------------------------------------
-        // Note:
-        // Add any special cases that cannot be created without arguments here.
-        // --------------------------------------------------------------------
-        // The list must then be sorted using the order defined by
-        // RandomSource.ordinal(). Currently this is not required so
-        // has not been implemented.
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public Iterator<UniformRandomProvider> iterator() {
-        return list.iterator();
-    }
-}
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/IntGeneratorsList.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/IntGeneratorsList.java
deleted file mode 100644
index 72d15d1..0000000
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/IntGeneratorsList.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rng.examples.stress;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Iterator;
-
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.core.source32.IntProvider;
-
-/**
- * List of generators that use {@link IntProvider} as a base class.
- */
-public class IntGeneratorsList implements Iterable<UniformRandomProvider> {
-    /** List of generators. */
-    private final List<UniformRandomProvider> list = new ArrayList<>();
-
-    /**
-     * Creates list.
-     */
-    public IntGeneratorsList() {
-        for (UniformRandomProvider rng : new GeneratorsList()) {
-            if (rng instanceof IntProvider) {
-                list.add(rng);
-            }
-        }
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public Iterator<UniformRandomProvider> iterator() {
-        return list.iterator();
-    }
-}
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/LongGeneratorsList.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/LongGeneratorsList.java
deleted file mode 100644
index 259b1bd..0000000
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/LongGeneratorsList.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rng.examples.stress;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Iterator;
-
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.core.source64.LongProvider;
-
-/**
- * List of generators that use {@link LongProvider} as a base class.
- */
-public class LongGeneratorsList implements Iterable<UniformRandomProvider> {
-    /** List of generators. */
-    private final List<UniformRandomProvider> list = new ArrayList<>();
-
-    /**
-     * Creates list.
-     */
-    public LongGeneratorsList() {
-        for (UniformRandomProvider rng : new GeneratorsList()) {
-            if (rng instanceof LongProvider) {
-                list.add(rng);
-            }
-        }
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public Iterator<UniformRandomProvider> iterator() {
-        return list.iterator();
-    }
-}
diff --git a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/RandomStressTester.java b/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/RandomStressTester.java
deleted file mode 100644
index c994e33..0000000
--- a/commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/RandomStressTester.java
+++ /dev/null
@@ -1,366 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rng.examples.stress;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.io.IOException;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.BufferedOutputStream;
-import java.io.BufferedWriter;
-import java.io.DataOutputStream;
-import java.text.SimpleDateFormat;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-
-import org.apache.commons.rng.UniformRandomProvider;
-
-/**
- * Class that can be used for testing a generator by piping the values
- * returned by its {@link UniformRandomProvider#nextInt()} method to a
- * program that reads {@code int} values from its standard input and
- * writes an analysis report to standard output.
- *
- * The <a href="http://www.phy.duke.edu/~rgb/General/dieharder.php">
- * "dieharder"</a> test suite is such a software.
- *
- * Example of command line, assuming that "examples.jar" specifies this
- * class as the "main" class (see {@link #main(String[]) main} method):
- * <pre><code>
- *  $ java -jar examples.jar \
- *    report/dh_ \
- *    4 \
- *    org.apache.commons.rng.examples.stress.GeneratorsList \
- *    /usr/bin/dieharder -a -g 200 -Y 1 -k 2
- * </code></pre>
- */
-public class RandomStressTester {
-    /** Comment prefix. */
-    private static final String C = "# ";
-    /** New line. */
-    private static final String N = System.lineSeparator();
-    /** The date format. */
-    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
-    /** Command line. */
-    private final List<String> cmdLine;
-    /** Output prefix. */
-    private final String fileOutputPrefix;
-
-    /**
-     * Creates the application.
-     *
-     * @param cmd Command line.
-     * @param outputPrefix Output prefix for file reports.
-     */
-    private RandomStressTester(List<String> cmd,
-                               String outputPrefix) {
-        final File exec = new File(cmd.get(0));
-        if (!exec.exists() ||
-            !exec.canExecute()) {
-            throw new IllegalArgumentException("Program is not executable: " + exec);
-        }
-
-        cmdLine = new ArrayList<String>(cmd);
-        fileOutputPrefix = outputPrefix;
-
-        final File reportDir = new File(fileOutputPrefix).getAbsoluteFile().getParentFile();
-        if (!reportDir.exists() ||
-            !reportDir.isDirectory() ||
-            !reportDir.canWrite()) {
-            throw new IllegalArgumentException("Invalid output directory: " + reportDir);
-        }
-    }
-
-    /**
-     * Program's entry point.
-     *
-     * @param args Application's arguments.
-     * The order is as follows:
-     * <ol>
-     *  <li>Output prefix: Filename prefix where the output of the analysis will
-     *   written to.  The appended suffix is the index of the instance within the
-     *   list of generators to be tested.</li>
-     *  <li>Number of threads to use concurrently: One thread will process one of
-     *    the generators to be tested.</li>
-     *  <li>Name of a class that implements {@code Iterable<UniformRandomProvider>}
-     *   (and defines a default constructor): Each generator of the list will be
-     *   tested by one instance of the analyzer program</li>
-     *  <li>Path to the executable: this is the analyzer software that reads 32-bits
-     *   integers from stdin.</li>
-     *  <li>All remaining arguments are passed to the executable.</li>
-     * </ol>
-     */
-    public static void main(String[] args) {
-        final String output = args[0];
-        final int numThreads = Integer.parseInt(args[1]);
-
-        final Iterable<UniformRandomProvider> rngList = createGeneratorsList(args[2]);
-
-        final List<String> cmdLine = new ArrayList<String>();
-        cmdLine.addAll(Arrays.asList(Arrays.copyOfRange(args, 3, args.length)));
-
-        final RandomStressTester app = new RandomStressTester(cmdLine, output);
-
-        // Throws runtime exceptions
-        app.run(rngList, numThreads);
-    }
-
-    /**
-     * Creates the tasks and starts the processes.
-     *
-     * @param generators List of generators to be analyzed.
-     * @param numConcurrentTasks Number of concurrent tasks.
-     * Twice as many threads will be started: one thread for the RNG and one
-     * for the analyzer.
-     */
-    private void run(Iterable<UniformRandomProvider> generators,
-                     int numConcurrentTasks) {
-        // Parallel execution.
-        final ExecutorService service = Executors.newFixedThreadPool(numConcurrentTasks);
-
-        // Placeholder (output will be "null").
-        final List<Future<?>> execOutput = new ArrayList<Future<?>>();
-
-        // Run tasks.
-        int count = 0;
-        for (UniformRandomProvider rng : generators) {
-            final File output = new File(fileOutputPrefix + (++count));
-            final Runnable r = new Task(rng, output);
-            execOutput.add(service.submit(r));
-        }
-
-        // Wait for completion (ignoring return value).
-        try {
-            for (Future<?> f : execOutput) {
-                try {
-                    f.get();
-                } catch (ExecutionException e) {
-                    System.err.println(e.getCause().getMessage());
-                }
-            }
-        } catch (InterruptedException ignored) {}
-
-        // Terminate all threads.
-        service.shutdown();
-    }
-
-    /**
-     * Creates the list of generators to be tested.
-     *
-     * @param name Name of the class that contains the generators to be
-     * analyzed.
-     * @return the list of generators.
-     * @throws IllegalStateException if an error occurs during instantiation.
-     */
-    @SuppressWarnings("unchecked")
-    private static Iterable<UniformRandomProvider> createGeneratorsList(String name) {
-        try {
-            return (Iterable<UniformRandomProvider>) Class.forName(name).newInstance();
-        } catch (ClassNotFoundException|
-                 InstantiationException|
-                 IllegalAccessException e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    /**
-     * Pipes random numbers to the standard input of an analyzer.
-     */
-    private class Task implements Runnable {
-        /** The timeout to wait for the process exit value in milliseconds. */
-        private final long TIMEOUT = 1000000L;
-        /** The default exit value to use when the process has not terminated. */
-        private final int DEFAULT_EXIT_VALUE = -808080;
-        /** Directory for reports of the tester processes. */
-        private final File output;
-        /** RNG to be tested. */
-        private final UniformRandomProvider rng;
-
-        /**
-         * Creates the task.
-         *
-         * @param random RNG to be tested.
-         * @param report Report file.
-         */
-        Task(UniformRandomProvider random,
-             File report) {
-            rng = random;
-            output = report;
-        }
-
-        /** {@inheritDoc} */
-        @Override
-        public void run() {
-            try {
-                // Write header.
-                printHeader(output, rng);
-
-                // Start test suite.
-                final ProcessBuilder builder = new ProcessBuilder(cmdLine);
-                builder.redirectOutput(ProcessBuilder.Redirect.appendTo(output));
-                builder.redirectErrorStream(true);
-                final Process testingProcess = builder.start();
-                final DataOutputStream sink = new DataOutputStream(
-                    new BufferedOutputStream(testingProcess.getOutputStream()));
-
-                final long startTime = System.nanoTime();
-
-                try {
-                    while (true) {
-                        sink.writeInt(rng.nextInt());
-                    }
-                } catch (IOException e) {
-                    // Hopefully getting here when the analyzing software terminates.
-                }
-
-                final long endTime = System.nanoTime();
-
-                // Get the exit value
-                final int exitValue = getExitValue(testingProcess);
-
-                // Write footer.
-                printFooter(output, endTime - startTime, exitValue);
-
-            } catch (IOException e) {
-                throw new RuntimeException("Failed to start task: " + e.getMessage());
-            }
-        }
-
-        /**
-         * Get the exit value from the process, waiting at most for 1 second, otherwise kill the process
-         * and return a dummy value.
-         *
-         * @param process the process.
-         * @return the exit value.
-         * @see Process#destroy()
-         */
-        private int getExitValue(Process process) {
-            long startTime = System.currentTimeMillis();
-            long remaining = TIMEOUT;
-
-            while (remaining > 0) {
-                try {
-                    return process.exitValue();
-                } catch (IllegalThreadStateException ex) {
-                    try {
-                        Thread.sleep(Math.min(remaining + 1, 100));
-                    } catch (InterruptedException e) {
-                        // Reset interrupted status and stop waiting
-                        Thread.currentThread().interrupt();
-                        break;
-                    }
-                }
-                remaining = TIMEOUT - (System.currentTimeMillis() - startTime);
-            }
-
-            // Not finished so kill it
-            process.destroy();
-
-            return DEFAULT_EXIT_VALUE;
-        }
-    }
-
-    /**
-     * @param output File.
-     * @param rng Generator being tested.
-     * @param cmdLine
-     * @throws IOException if there was a problem opening or writing to
-     * the {@code output} file.
-     */
-    private void printHeader(File output,
-                             UniformRandomProvider rng)
-        throws IOException {
-        final StringBuilder sb = new StringBuilder();
-        sb.append(C).append(N);
-        sb.append(C).append("RNG: ").append(rng.toString()).append(N);
-        sb.append(C).append(N);
-        sb.append(C).append("Java: ").append(System.getProperty("java.version")).append(N);
-        sb.append(C).append("Runtime: ").append(System.getProperty("java.runtime.version", "?")).append(N);
-        sb.append(C).append("JVM: ").append(System.getProperty("java.vm.name"))
-            .append(" ").append(System.getProperty("java.vm.version")).append(N);
-        sb.append(C).append("OS: ").append(System.getProperty("os.name"))
-            .append(" ").append(System.getProperty("os.version"))
-            .append(" ").append(System.getProperty("os.arch")).append(N);
-        sb.append(C).append(N);
-
-        sb.append(C).append("Analyzer: ");
-        for (String s : cmdLine) {
-            sb.append(s).append(" ");
-        }
-        sb.append(N);
-        sb.append(C).append(N);
-
-        appendDate(sb, "Start");
-
-        try (BufferedWriter w = new BufferedWriter(new FileWriter(output, true))) {
-            w.write(sb.toString());
-        }
-    }
-
-    /**
-     * @param output File.
-     * @param nanoTime Duration of the run.
-     * @param exitValue The process exit value.
-     * @throws IOException if there was a problem opening or writing to
-     * the {@code output} file.
-     */
-    private void printFooter(File output,
-                             long nanoTime,
-                             int exitValue)
-        throws IOException {
-        final StringBuilder sb = new StringBuilder();
-        sb.append(C).append(N);
-
-        appendDate(sb, "End");
-
-        sb.append(C).append("Exit value: ").append(exitValue).append(N);
-        sb.append(C).append(N);
-
-        final double duration = nanoTime * 1e-9 / 60;
-        sb.append(C).append("Test duration: ").append(duration).append(" minutes").append(N);
-
-        sb.append(C).append(N);
-
-        try (BufferedWriter w = new BufferedWriter(new FileWriter(output, true))) {
-            w.write(sb.toString());
-        }
-    }
-
-    /**
-     * Append a comment with the current date to the {@link StringBuilder}.
-     *
-     * <pre>
-     * # [prefix]: yyyy-MM-dd HH:mm:ss
-     * #
-     * </pre>
-     *
-     * @param sb the StringBuilder.
-     * @param prefix the prefix used before the formatted date, e.g. "Start".
-     */
-    private void appendDate(StringBuilder sb,
-                            String prefix) {
-        // Use local date format. It is not thread safe.
-        final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
-        sb.append(C).append(prefix).append(": ").append(dateFormat.format(new Date())).append(N);
-        sb.append(C).append(N);
-    }
-}