Example usage for java.lang System nanoTime

List of usage examples for java.lang System nanoTime

Introduction

In this page you can find the example usage for java.lang System nanoTime.

Prototype

@HotSpotIntrinsicCandidate
public static native long nanoTime();

Source Link

Document

Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.

Usage

From source file:org.apache.uima.examples.as.GetMetaRequest.java

/**
 * retrieve meta information for a UIMA-AS Service attached to a broker
 * It uses the port 1099 as the JMX port on the broker, unless overridden
 *   by defining the system property activemq.broker.jmx.port with a value of another port number
 * It uses the default JMX ActiveMQ Domain "org.apache.activemq", unless overridden
 *   by defining the system property activemq.broker.jmx.domain with a value of the domain to use
 *   This normally never needs to be done unless multiple brokers are run on the same node 
 *   as is sometimes done for unit tests.
 * @param args - brokerUri serviceName [-verbose]
 *///from w  w w .j a va2 s .c o  m
public static void main(String[] args) {
    if (args.length < 2) {
        System.err.println("Need arguments: brokerURI serviceName [-verbose]");
        System.exit(1);
    }
    String brokerURI = args[0];
    String queueName = args[1];
    boolean printReply = false;
    if (args.length > 2) {
        if (args[2].equalsIgnoreCase("-verbose")) {
            printReply = true;
        } else {
            System.err.println("Unknown argument: " + args[2]);
            System.exit(1);
        }
    }
    final Connection connection;
    Session producerSession = null;
    Queue producerQueue = null;
    MessageProducer producer;
    MessageConsumer consumer;
    Session consumerSession = null;
    TemporaryQueue consumerDestination = null;
    long startTime = 0;

    //  Check if JMX server port number was specified
    jmxPort = System.getProperty("activemq.broker.jmx.port");
    if (jmxPort == null || jmxPort.trim().length() == 0) {
        jmxPort = "1099"; // default
    }

    try {
        //  First create connection to a broker
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerURI);
        connection = factory.createConnection();
        connection.start();
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                    if (jmxc != null) {
                        jmxc.close();
                    }
                } catch (Exception ex) {
                }
            }
        }));

        URI target = new URI(brokerURI);
        String brokerHost = target.getHost();

        attachToRemoteBrokerJMXServer(brokerURI);
        if (isQueueAvailable(queueName) == QueueState.exists) {
            System.out.println("Queue " + queueName + " found on " + brokerURI);
            System.out.println("Sending getMeta...");
        } else if (isQueueAvailable(queueName) == QueueState.existsnot) {
            System.err.println("Queue " + queueName + " does not exist on " + brokerURI);
            System.exit(1);
        } else {
            System.out.println("Cannot see queues on JMX port " + brokerHost + ":" + jmxPort);
            System.out.println("Sending getMeta anyway...");
        }

        producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producerQueue = producerSession.createQueue(queueName);
        producer = producerSession.createProducer(producerQueue);
        consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        consumerDestination = consumerSession.createTemporaryQueue();
        //  -----------------------------------------------------------------------------
        //  Create message consumer. The consumer uses a selector to filter out messages other
        //  then GetMeta replies. Currently UIMA AS service returns two messages for each request:
        //  ServiceInfo message and GetMeta message. The ServiceInfo message is returned by the 
        //  service immediately upon receiving a message from a client. This serves dual purpose, 
        //  1) to make sure the client reply destination exists
        //  2) informs the client which service is processing its request
        //  -----------------------------------------------------------------------------
        consumer = consumerSession.createConsumer(consumerDestination, "Command=2001");
        TextMessage msg = producerSession.createTextMessage();
        msg.setStringProperty(AsynchAEMessage.MessageFrom, consumerDestination.getQueueName());
        msg.setStringProperty(UIMAMessage.ServerURI, brokerURI);
        msg.setIntProperty(AsynchAEMessage.MessageType, AsynchAEMessage.Request);
        msg.setIntProperty(AsynchAEMessage.Command, AsynchAEMessage.GetMeta);
        msg.setJMSReplyTo(consumerDestination);
        msg.setText("");
        producer.send(msg);
        startTime = System.nanoTime();

        System.out.println("Sent getMeta request to " + queueName + " at " + brokerURI);
        System.out.println("Waiting for getMeta reply...");
        ActiveMQTextMessage reply = (ActiveMQTextMessage) consumer.receive();
        long waitTime = (System.nanoTime() - startTime) / 1000000;

        System.out.println(
                "Reply from " + reply.getStringProperty("ServerIP") + " received in " + waitTime + " ms");
        if (printReply) {
            System.out.println("Reply MessageText: " + reply.getText());
        }
    } catch (Exception e) {
        System.err.println(e.toString());
    }
    System.exit(0);
}

From source file:ffx.xray.TimerTest.java

public static void main(String args[]) {
    // Parameters collection from original Timer script
    String pdbname = System.getProperty("pdbFile", "1N7S.pdb");
    String mtzname = System.getProperty("mtzFile", null);
    String cifname = System.getProperty("cifFile", null);
    final boolean ciOnly = false;
    final String info = "SNARE complex";
    final double r = 19.412671496011;
    final double rfree = 21.555930987573;
    final double sigmaA = 0.9336853524690557;
    final double sigmaW = 0.13192537249786418;

    boolean ci = System.getProperty("ffx.ci", "false").equalsIgnoreCase("true");
    if (!ci && ciOnly) {
        crystalStats = null;//from  w w  w . ja  v a 2 s .c o  m
        return;
    }

    int index = pdbname.lastIndexOf(".");
    String name = pdbname.substring(0, index);

    // load the structure
    MolecularAssembly molecularAssembly;
    File structure, mtzFile, cifFile;
    structure = new File(pdbname);
    PotentialsFileOpener opener = new PotentialsFileOpener(structure);
    opener.run();
    molecularAssembly = opener.getAssembly();
    mtzFile = new File(mtzname);
    cifFile = new File(cifname);

    // load any properties associated with it
    CompositeConfiguration properties = Keyword.loadProperties(structure);

    // read in Fo/sigFo/FreeR
    MTZFilter mtzFilter = new MTZFilter();
    CIFFilter cifFilter = new CIFFilter();
    Crystal crystal = Crystal.checkProperties(properties);
    Resolution resolution = Resolution.checkProperties(properties);
    if (crystal == null || resolution == null) {
        if (mtzname != null) {
            reflectionList = mtzFilter.getReflectionList(mtzFile);
        } else {
            reflectionList = cifFilter.getReflectionList(cifFile);
        }
    } else {
        reflectionList = new ReflectionList(crystal, resolution);
    }

    refinementData = new DiffractionRefinementData(properties, reflectionList);
    if (mtzname != null) {
        //            assertTrue(info + " mtz file should be read in without errors",
        //                    mtzFilter.readFile(mtzFile, reflectionList, refinementData,
        //                            properties));
    } else {
        //            assertTrue(info + " cif file should be read in without errors",
        //                    cifFilter.readFile(cifFile, reflectionList, refinementData,
        //                            properties));
    }

    ForceFieldFilter forceFieldFilter = new ForceFieldFilter(properties);
    ForceField forceField = forceFieldFilter.parse();

    // associate molecular assembly with the structure, set up forcefield
    molecularAssembly.setForceField(forceField);
    PDBFilter pdbFile = new PDBFilter(structure, molecularAssembly, forceField, properties);
    pdbFile.readFile();
    pdbFile.applyAtomProperties();
    molecularAssembly.finalize(true, forceField);
    ForceFieldEnergy energy = new ForceFieldEnergy(molecularAssembly, pdbFile.getCoordRestraints());

    List<Atom> atomList = molecularAssembly.getAtomList();
    Atom atomArray[] = atomList.toArray(new Atom[atomList.size()]);

    // set up FFT and run it
    parallelTeam = new ParallelTeam();
    CrystalReciprocalSpace crs = new CrystalReciprocalSpace(reflectionList, atomArray, parallelTeam,
            parallelTeam, false);
    crs.computeDensity(refinementData.fc);
    refinementData.setCrystalReciprocalSpace_fc(crs);
    crs = new CrystalReciprocalSpace(reflectionList, atomArray, parallelTeam, parallelTeam, true);
    crs.computeDensity(refinementData.fs);
    refinementData.setCrystalReciprocalSpace_fs(crs);

    ScaleBulkMinimize scaleBulkMinimize = new ScaleBulkMinimize(reflectionList, refinementData, crs,
            parallelTeam);
    scaleBulkMinimize.minimize(6, 1.0e-4);

    SigmaAMinimize sigmaAMinimize = new SigmaAMinimize(reflectionList, refinementData, parallelTeam);
    sigmaAMinimize.minimize(7, 2.0e-2);

    SplineMinimize splineMinimize = new SplineMinimize(reflectionList, refinementData, refinementData.spline,
            SplineEnergy.Type.FOFC);
    splineMinimize.minimize(7, 1e-5);

    crystalStats = new CrystalStats(reflectionList, refinementData);

    scaleBulkMinimize = new ScaleBulkMinimize(reflectionList, refinementData, refinementData.crs_fs,
            parallelTeam);
    ScaleBulkEnergy scaleBulkEnergy = scaleBulkMinimize.getScaleBulkEnergy();
    int n = scaleBulkMinimize.getNumberOfVariables();
    double x[] = new double[n];
    double g[] = new double[n];
    scaleBulkMinimize.getCoordinates(x);
    scaleBulkEnergy.energyAndGradient(x, g);

    double delta = 1.0e-4;
    double tolerance = 1.0e-4;

    logger.info(String.format("SCATTER TEST"));
    for (int i = 0; i < 30; i++) {
        long time = -System.nanoTime();
        scaleBulkEnergy.energyAndGradient(x, g);
        time += System.nanoTime();
        logger.info(String.format(" Time %12.8f", time * 1.0e-9));
    }
}

From source file:com.ottogroup.bi.spqr.pipeline.statistics.MicroPipelineStatistics.java

public static void main(String[] args) {
    MicroPipelineStatistics stats = new MicroPipelineStatistics("procNodeId-1", "--", "component-123",
            System.currentTimeMillis(), 1234, 2, 432, 56, 67890, 98765, 45678);
    stats.setErrors(9383);// www . j ava  2s.  c  o m
    stats.setEndTime(System.currentTimeMillis() + 1000);

    long n1 = System.nanoTime();
    byte[] converted = stats.toByteArray();
    long n2 = System.nanoTime();

    System.out.println("length: " + converted.length + ", conversion: " + (n2 - n1) + "ns");

    long s1 = System.nanoTime();
    MicroPipelineStatistics reStats = MicroPipelineStatistics.fromByteArray(converted);
    long s2 = System.nanoTime();
    System.out.println("length: " + converted.length + ", conversion: " + (s2 - s1) + "ns");
    System.out.println(stats);
    System.out.println(reStats);

}

From source file:ca.uqac.info.monitor.BeepBeepMonitor.java

public static void main(String[] args) {
    int verbosity = 1, slowdown = 0, tcp_port = 0;
    boolean show_stats = false, to_stdout = false;
    String trace_filename = "", pipe_filename = "", event_name = "message";
    final MonitorFactory mf = new MonitorFactory();

    // In case we open a socket
    ServerSocket m_serverSocket = null;
    Socket m_connection = null;/*from w  ww  .  jav  a 2s .co m*/

    // Parse command line arguments
    Options options = setupOptions();
    CommandLine c_line = setupCommandLine(args, options);
    assert c_line != null;
    if (c_line.hasOption("verbosity")) {
        verbosity = Integer.parseInt(c_line.getOptionValue("verbosity"));
    }
    if (verbosity > 0) {
        showHeader();
    }
    if (c_line.hasOption("version")) {
        System.err.println("(C) 2008-2013 Sylvain Hall et al., Universit du Qubec  Chicoutimi");
        System.err.println("This program comes with ABSOLUTELY NO WARRANTY.");
        System.err.println("This is a free software, and you are welcome to redistribute it");
        System.err.println("under certain conditions. See the file COPYING for details.\n");
        System.exit(ERR_OK);
    }
    if (c_line.hasOption("h")) {
        showUsage(options);
        System.exit(ERR_OK);
    }
    if (c_line.hasOption("version")) {
        System.exit(ERR_OK);
    }
    if (c_line.hasOption("slowdown")) {
        slowdown = Integer.parseInt(c_line.getOptionValue("slowdown"));
        if (verbosity > 0)
            System.err.println("Slowdown factor: " + slowdown + " ms");
    }
    if (c_line.hasOption("stats")) {
        show_stats = true;
    }
    if (c_line.hasOption("csv")) {
        // Will output data in CSV format to stdout
        to_stdout = true;
    }
    if (c_line.hasOption("eventname")) {
        // Set event name
        event_name = c_line.getOptionValue("eventname");
    }
    if (c_line.hasOption("t")) {
        // Read events from a trace
        trace_filename = c_line.getOptionValue("t");
    }
    if (c_line.hasOption("p")) {
        // Read events from a pipe
        pipe_filename = c_line.getOptionValue("p");
    }
    if (c_line.hasOption("k")) {
        // Read events from a TCP port
        tcp_port = Integer.parseInt(c_line.getOptionValue("k"));
    }
    if (!trace_filename.isEmpty() && !pipe_filename.isEmpty()) {
        System.err.println("ERROR: you must specify at most one of trace file or named pipe");
        showUsage(options);
        System.exit(ERR_ARGUMENTS);
    }
    @SuppressWarnings("unchecked")
    List<String> remaining_args = c_line.getArgList();
    if (remaining_args.isEmpty()) {
        System.err.println("ERROR: no input formula specified");
        showUsage(options);
        System.exit(ERR_ARGUMENTS);
    }
    // Instantiate the event notifier
    boolean notify = (verbosity > 0);
    EventNotifier en = new EventNotifier(notify);
    en.m_slowdown = slowdown;
    en.m_csvToStdout = to_stdout;
    // Create one monitor for each input file and add it to the notifier 
    for (String formula_filename : remaining_args) {
        try {
            String formula_contents = FileReadWrite.readFile(formula_filename);
            Operator op = Operator.parseFromString(formula_contents);
            op.accept(mf);
            Monitor mon = mf.getMonitor();
            Map<String, String> metadata = getMetadata(formula_contents);
            metadata.put("Filename", formula_filename);
            en.addMonitor(mon, metadata);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(ERR_IO);
        } catch (Operator.ParseException e) {
            System.err.println("Error parsing input formula");
            System.exit(ERR_PARSE);
        }
    }

    // Read trace and iterate
    // Opens file
    PipeReader pr = null;
    try {
        if (!pipe_filename.isEmpty()) {
            // We tell the pipe reader we read a pipe
            File f = new File(pipe_filename);
            if (verbosity > 0)
                System.err.println("Reading from pipe named " + f.getName());
            pr = new PipeReader(new FileInputStream(f), en, false);
        } else if (!trace_filename.isEmpty()) {
            // We tell the pipe reader we read a regular file
            File f = new File(trace_filename);
            if (verbosity > 0)
                System.err.println("Reading from file " + f.getName());
            pr = new PipeReader(new FileInputStream(f), en, true);
        } else if (tcp_port > 0) {
            // We tell the pipe reader we read from a socket
            if (verbosity > 0)
                System.err.println("Reading from TCP port " + tcp_port);
            m_serverSocket = new ServerSocket(tcp_port);
            m_connection = m_serverSocket.accept();
            pr = new PipeReader(m_connection.getInputStream(), en, false);
        } else {
            // We tell the pipe reader we read from standard input
            if (verbosity > 0)
                System.err.println("Reading from standard input");
            pr = new PipeReader(System.in, en, false);
        }
    } catch (FileNotFoundException ex) {
        // We print both trace and pipe since one of them must be empty
        System.err.println("ERROR: file not found " + trace_filename + pipe_filename);
        System.exit(ERR_IO);
    } catch (IOException e) {
        // Caused by socket error
        e.printStackTrace();
        System.exit(ERR_IO);
    }
    pr.setSeparator("<" + event_name + ">", "</" + event_name + ">");

    // Check parameters for the event notifier
    if (c_line.hasOption("no-trigger")) {
        en.m_notifyOnVerdict = false;
    } else {
        en.m_notifyOnVerdict = true;
    }
    if (c_line.hasOption("mirror")) {
        en.m_mirrorEventsOnStdout = true;
    }

    // Start event notifier
    en.reset();
    Thread th = new Thread(pr);
    long clock_start = System.nanoTime();
    th.start();
    try {
        th.join(); // Wait for thread to finish
    } catch (InterruptedException e1) {
        // Thread is finished
    }
    if (tcp_port > 0 && m_serverSocket != null) {
        // We opened a socket; now we close it
        try {
            m_serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    long clock_end = System.nanoTime();
    int ret_code = pr.getReturnCode();
    switch (ret_code) {
    case PipeReader.ERR_EOF:
        if (verbosity > 0)
            System.err.println("\nEnd of file reached");
        break;
    case PipeReader.ERR_EOT:
        if (verbosity > 0)
            System.err.println("\nEOT received on pipe: closing");
        break;
    case PipeReader.ERR_OK:
        // Do nothing
        break;
    default:
        // An error
        System.err.println("Runtime error");
        System.exit(ERR_RUNTIME);
        break;
    }
    if (show_stats) {
        if (verbosity > 0) {
            System.out.println("Messages:   " + en.m_numEvents);
            System.out.println("Time:       " + (int) (en.m_totalTime / 1000000f) + " ms");
            System.out.println("Clock time: " + (int) ((clock_end - clock_start) / 1000000f) + " ms");
            System.out.println("Max heap:   " + (int) (en.heapSize / 1048576f) + " MB");
        } else {
            // If stats are asked but verbosity = 0, only show time value
            // (both monitor and wall clock) 
            System.out.print((int) (en.m_totalTime / 1000000f));
            System.out.print(",");
            System.out.print((int) ((clock_end - clock_start) / 1000000f));
        }
    }
    System.exit(ERR_OK);
}

From source file:eu.amidst.core.inference.MPEInferenceExperiments_Deliv1.java

/**
 * The class constructor.//  w  ww  . ja  va 2s.co m
 * @param args Array of options: "filename variable a b N useVMP" if variable is continuous or "filename variable w N useVMP" for discrete
 */
public static void main(String[] args) throws Exception { // args: seedNetwork numberGaussians numberDiscrete seedAlgorithms

    int seedNetwork = 23423523;
    int numberOfGaussians = 20;
    int numberOfMultinomials = 20;

    int seed = 634634534;

    int parallelSamples = 100;
    int samplingMethodSize = 10000;

    int repetitions = 10;

    int numberOfIterations = 200;

    if (args.length != 8) {
        if (Main.VERBOSE)
            System.out.println("Invalid number of parameters. Using default values");
    } else {
        try {
            seedNetwork = Integer.parseInt(args[0]);
            numberOfGaussians = Integer.parseInt(args[1]);
            numberOfMultinomials = Integer.parseInt(args[2]);

            seed = Integer.parseInt(args[3]);

            parallelSamples = Integer.parseInt(args[4]);
            samplingMethodSize = Integer.parseInt(args[5]);

            repetitions = Integer.parseInt(args[6]);

            numberOfIterations = Integer.parseInt(args[7]);

        } catch (NumberFormatException ex) {
            if (Main.VERBOSE)
                System.out.println(
                        "Invalid parameters. Provide integers: seedNetwork numberGaussians numberDiscrete seedAlgorithms parallelSamples sampleSize repetitions");
            if (Main.VERBOSE)
                System.out.println("Using default parameters");
            if (Main.VERBOSE)
                System.out.println(ex.toString());
            System.exit(20);
        }
    }
    int numberOfLinks = (int) 1.3 * (numberOfGaussians + numberOfMultinomials);

    BayesianNetworkGenerator.setSeed(seedNetwork);
    BayesianNetworkGenerator.setNumberOfGaussianVars(numberOfGaussians);
    BayesianNetworkGenerator.setNumberOfMultinomialVars(numberOfMultinomials, 2);
    BayesianNetworkGenerator.setNumberOfLinks(numberOfLinks);

    String filename = "./networks/simulated/RandomBN_" + Integer.toString(numberOfMultinomials) + "D_"
            + Integer.toString(numberOfGaussians) + "C_" + Integer.toString(seedNetwork) + "_Seed.bn";
    //BayesianNetworkGenerator.generateBNtoFile(numberOfMultinomials,2,numberOfGaussians,numberOfLinks,seedNetwork,filename);
    BayesianNetwork bn = BayesianNetworkGenerator.generateBayesianNetwork();

    //if (Main.VERBOSE) System.out.println(bn.getDAG());
    //if (Main.VERBOSE) System.out.println(bn.toString());

    MPEInference mpeInference = new MPEInference();
    mpeInference.setModel(bn);
    mpeInference.setParallelMode(true);

    //if (Main.VERBOSE) System.out.println("CausalOrder: " + Arrays.toString(Utils.getCausalOrder(mpeInference.getOriginalModel().getDAG()).stream().map(Variable::getName).toArray()));
    List<Variable> modelVariables = Utils.getTopologicalOrder(bn.getDAG());
    if (Main.VERBOSE)
        System.out.println();

    // Including evidence:
    //double observedVariablesRate = 0.00;
    //Assignment evidence = randomEvidence(seed, observedVariablesRate, bn);
    //mpeInference.setEvidence(evidence);

    mpeInference.setSampleSize(parallelSamples);
    mpeInference.setSeed(seed);
    mpeInference.setNumberOfIterations(numberOfIterations);

    double[] SA_All_prob = new double[repetitions];
    double[] SA_Some_prob = new double[repetitions];
    double[] HC_All_prob = new double[repetitions];
    double[] HC_Some_prob = new double[repetitions];
    double[] sampling_prob = new double[repetitions];

    double[] SA_All_time = new double[repetitions];
    double[] SA_Some_time = new double[repetitions];
    double[] HC_All_time = new double[repetitions];
    double[] HC_Some_time = new double[repetitions];
    double[] sampling_time = new double[repetitions];

    long timeStart;
    long timeStop;
    double execTime;

    Assignment mpeEstimate;

    mpeInference.setParallelMode(true);

    for (int k = 0; k < repetitions; k++) {

        mpeInference.setSampleSize(parallelSamples);

        /***********************************************
         *        SIMULATED ANNEALING
         ************************************************/

        // MPE INFERENCE WITH SIMULATED ANNEALING, ALL VARIABLES
        //if (Main.VERBOSE) System.out.println();
        timeStart = System.nanoTime();
        mpeInference.runInference(MPEInference.SearchAlgorithm.SA_GLOBAL);

        //mpeEstimate = mpeInference.getEstimate();
        //if (Main.VERBOSE) System.out.println("MPE estimate (SA.All): " + mpeEstimate.outputString(modelVariables));   //toString(modelVariables)
        //if (Main.VERBOSE) System.out.println("with probability: " + Math.exp(mpeInference.getLogProbabilityOfEstimate()) + ", logProb: " + mpeInference.getLogProbabilityOfEstimate());
        timeStop = System.nanoTime();
        execTime = (double) (timeStop - timeStart) / 1000000000.0;
        //if (Main.VERBOSE) System.out.println("computed in: " + Double.toString(execTime) + " seconds");
        //if (Main.VERBOSE) System.out.println(.toString(mapInference.getOriginalModel().getStaticVariables().iterator().));
        //if (Main.VERBOSE) System.out.println();
        SA_All_prob[k] = mpeInference.getLogProbabilityOfEstimate();
        SA_All_time[k] = execTime;

        // MPE INFERENCE WITH SIMULATED ANNEALING, SOME VARIABLES AT EACH TIME
        timeStart = System.nanoTime();
        mpeInference.runInference(MPEInference.SearchAlgorithm.SA_LOCAL);

        //mpeEstimate = mpeInference.getEstimate();
        //if (Main.VERBOSE) System.out.println("MPE estimate  (SA.Some): " + mpeEstimate.outputString(modelVariables));   //toString(modelVariables)
        //if (Main.VERBOSE) System.out.println("with probability: "+ Math.exp(mpeInference.getLogProbabilityOfEstimate()) + ", logProb: " + mpeInference.getLogProbabilityOfEstimate());
        timeStop = System.nanoTime();
        execTime = (double) (timeStop - timeStart) / 1000000000.0;
        //if (Main.VERBOSE) System.out.println("computed in: " + Double.toString(execTime) + " seconds");
        //if (Main.VERBOSE) System.out.println(.toString(mapInference.getOriginalModel().getStaticVariables().iterator().));
        //if (Main.VERBOSE) System.out.println();
        SA_Some_prob[k] = mpeInference.getLogProbabilityOfEstimate();
        SA_Some_time[k] = execTime;

        /***********************************************
         *        HILL CLIMBING
         ************************************************/

        // MPE INFERENCE WITH HILL CLIMBING, ALL VARIABLES
        timeStart = System.nanoTime();
        mpeInference.runInference(MPEInference.SearchAlgorithm.HC_GLOBAL);

        //mpeEstimate = mpeInference.getEstimate();
        //modelVariables = mpeInference.getOriginalModel().getVariables().getListOfVariables();
        //if (Main.VERBOSE) System.out.println("MPE estimate (HC.All): " + mpeEstimate.outputString(modelVariables));
        //if (Main.VERBOSE) System.out.println("with probability: " + Math.exp(mpeInference.getLogProbabilityOfEstimate()) + ", logProb: " + mpeInference.getLogProbabilityOfEstimate());
        timeStop = System.nanoTime();
        execTime = (double) (timeStop - timeStart) / 1000000000.0;
        //if (Main.VERBOSE) System.out.println("computed in: " + Double.toString(execTime) + " seconds");
        //if (Main.VERBOSE) System.out.println();
        HC_All_prob[k] = mpeInference.getLogProbabilityOfEstimate();
        HC_All_time[k] = execTime;

        //  MPE INFERENCE WITH HILL CLIMBING, ONE VARIABLE AT EACH TIME
        timeStart = System.nanoTime();
        mpeInference.runInference(MPEInference.SearchAlgorithm.HC_LOCAL);

        //mpeEstimate = mpeInference.getEstimate();
        //if (Main.VERBOSE) System.out.println("MPE estimate  (HC.Some): " + mpeEstimate.outputString(modelVariables));   //toString(modelVariables)
        //if (Main.VERBOSE) System.out.println("with probability: " + Math.exp(mpeInference.getLogProbabilityOfEstimate()) + ", logProb: " + mpeInference.getLogProbabilityOfEstimate());
        timeStop = System.nanoTime();
        execTime = (double) (timeStop - timeStart) / 1000000000.0;
        //if (Main.VERBOSE) System.out.println("computed in: " + Double.toString(execTime) + " seconds");
        //if (Main.VERBOSE) System.out.println();
        HC_Some_prob[k] = mpeInference.getLogProbabilityOfEstimate();
        HC_Some_time[k] = execTime;

        /***********************************************
         *        SAMPLING AND DETERMINISTIC
         ************************************************/

        // MPE INFERENCE WITH SIMULATION AND PICKING MAX

        mpeInference.setSampleSize(samplingMethodSize);

        timeStart = System.nanoTime();
        mpeInference.runInference(MPEInference.SearchAlgorithm.SAMPLING);

        //mpeEstimate = mpeInference.getEstimate();
        //modelVariables = mpeInference.getOriginalModel().getVariables().getListOfVariables();
        //if (Main.VERBOSE) System.out.println("MPE estimate (SAMPLING): " + mpeEstimate.outputString(modelVariables));
        //if (Main.VERBOSE) System.out.println("with probability: " + Math.exp(mpeInference.getLogProbabilityOfEstimate()) + ", logProb: " + mpeInference.getLogProbabilityOfEstimate());
        timeStop = System.nanoTime();
        execTime = (double) (timeStop - timeStart) / 1000000000.0;
        //if (Main.VERBOSE) System.out.println("computed in: " + Double.toString(execTime) + " seconds");
        //if (Main.VERBOSE) System.out.println();
        sampling_prob[k] = mpeInference.getLogProbabilityOfEstimate();
        sampling_time[k] = execTime;
    }

    double determ_prob = 0;
    double determ_time = 0;

    if (bn.getNumberOfVars() <= 50) {

        // MPE INFERENCE, DETERMINISTIC
        timeStart = System.nanoTime();
        //mpeInference.runInference(-2);

        //mpeEstimate = mpeInference.getEstimate();
        //modelVariables = mpeInference.getOriginalModel().getVariables().getListOfVariables();
        //if (Main.VERBOSE) System.out.println("MPE estimate (DETERM.): " + mpeEstimate.outputString(modelVariables));
        //if (Main.VERBOSE) System.out.println("with probability: " + Math.exp(mpeInference.getLogProbabilityOfEstimate()) + ", logProb: " + mpeInference.getLogProbabilityOfEstimate());
        timeStop = System.nanoTime();
        execTime = (double) (timeStop - timeStart) / 1000000000.0;
        //if (Main.VERBOSE) System.out.println("computed in: " + Double.toString(execTime) + " seconds");
        //if (Main.VERBOSE) System.out.println();

        determ_prob = mpeInference.getLogProbabilityOfEstimate();
        determ_time = execTime;
        determ_prob = -34.64743236365595;
        determ_time = 0;

    } else {
        if (Main.VERBOSE)
            System.out.println("Too many variables for deterministic method");
    }

    /***********************************************
     *        DISPLAY OF RESULTS
     ************************************************/

    if (Main.VERBOSE)
        System.out.println("*** RESULTS ***");

    //        if (Main.VERBOSE) System.out.println("SA_All log-probabilities");
    //        if (Main.VERBOSE) System.out.println(Arrays.toString(SA_All_prob));
    //        if (Main.VERBOSE) System.out.println("SA_Some log-probabilities");
    //        if (Main.VERBOSE) System.out.println(Arrays.toString(SA_Some_prob));
    //        if (Main.VERBOSE) System.out.println("HC_All log-probabilities");
    //        if (Main.VERBOSE) System.out.println(Arrays.toString(HC_All_prob));
    //        if (Main.VERBOSE) System.out.println("HC_Some log-probabilities");
    //        if (Main.VERBOSE) System.out.println(Arrays.toString(HC_Some_prob));
    //        if (Main.VERBOSE) System.out.println("Sampling log-probabilities");
    //        if (Main.VERBOSE) System.out.println(Arrays.toString(sampling_prob));
    //
    //        if(bn.getNumberOfVars()<=50) {
    //            if (Main.VERBOSE) System.out.println("Deterministic log-probability");
    //            if (Main.VERBOSE) System.out.println(Double.toString(determ_prob));
    //        }
    //        if (Main.VERBOSE) System.out.println();

    final double determ_prob_FINAL = determ_prob;

    //        int SA_All_success = (int) Arrays.stream(SA_All_prob).filter(db -> (db <= determ_prob_FINAL+0.001 && db >=determ_prob_FINAL-0.001)).count();
    //        int SA_Some_success = (int) Arrays.stream(SA_Some_prob).filter(db -> (db <= determ_prob_FINAL+0.001 && db >=determ_prob_FINAL-0.001)).count();
    //        int HC_All_success = (int) Arrays.stream(HC_All_prob).filter(db -> (db <= determ_prob_FINAL+0.001 && db >=determ_prob_FINAL-0.001)).count();
    //        int HC_Some_success = (int) Arrays.stream(HC_Some_prob).filter(db -> (db <= determ_prob_FINAL+0.001 && db >=determ_prob_FINAL-0.001)).count();
    //        int sampling_success = (int) Arrays.stream(sampling_prob).filter(db -> (db <= determ_prob_FINAL+0.001 && db >=determ_prob_FINAL-0.001)).count();
    //
    //        if (Main.VERBOSE) System.out.println("SA_All % success");
    //        if (Main.VERBOSE) System.out.println(Double.toString( 100 * SA_All_success/repetitions ));
    //        if (Main.VERBOSE) System.out.println("SA_Some % success");
    //        if (Main.VERBOSE) System.out.println(Double.toString( 100 * SA_Some_success/repetitions ));
    //        if (Main.VERBOSE) System.out.println("HC_All % success");
    //        if (Main.VERBOSE) System.out.println(Double.toString( 100 * HC_All_success/repetitions ));
    //        if (Main.VERBOSE) System.out.println("HC_Some % success");
    //        if (Main.VERBOSE) System.out.println(Double.toString( 100 * HC_Some_success/repetitions ));
    //        if (Main.VERBOSE) System.out.println("Sampling % success");
    //        if (Main.VERBOSE) System.out.println(Double.toString( 100 * sampling_success/repetitions ));
    //        if (Main.VERBOSE) System.out.println();

    if (Main.VERBOSE)
        System.out.println("SA_All RMS probabilities");
    if (Main.VERBOSE)
        System.out.println(Double.toString(Arrays.stream(SA_All_prob)
                .map(value -> Math.pow(value - determ_prob_FINAL, 2)).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("SA_Some RMS probabilities");
    if (Main.VERBOSE)
        System.out.println(Double.toString(Arrays.stream(SA_Some_prob)
                .map(value -> Math.pow(value - determ_prob_FINAL, 2)).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("HC_All RMS probabilities");
    if (Main.VERBOSE)
        System.out.println(Double.toString(Arrays.stream(HC_All_prob)
                .map(value -> Math.pow(value - determ_prob_FINAL, 2)).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("HC_Some RMS probabilities");
    if (Main.VERBOSE)
        System.out.println(Double.toString(Arrays.stream(HC_Some_prob)
                .map(value -> Math.pow(value - determ_prob_FINAL, 2)).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("Sampling RMS probabilities");
    if (Main.VERBOSE)
        System.out.println(Double.toString(Arrays.stream(sampling_prob)
                .map(value -> Math.pow(value - determ_prob_FINAL, 2)).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println();

    if (Main.VERBOSE)
        System.out.println("SA_All times");
    //if (Main.VERBOSE) System.out.println(Arrays.toString(SA_All_time));
    if (Main.VERBOSE)
        System.out.println("Mean time: " + Double.toString(Arrays.stream(SA_All_time).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("SA_Some times");
    //if (Main.VERBOSE) System.out.println(Arrays.toString(SA_Some_time));
    if (Main.VERBOSE)
        System.out
                .println("Mean time: " + Double.toString(Arrays.stream(SA_Some_time).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("HC_All times");
    //if (Main.VERBOSE) System.out.println(Arrays.toString(HC_All_time));
    if (Main.VERBOSE)
        System.out.println("Mean time: " + Double.toString(Arrays.stream(HC_All_time).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("HC_Some times");
    //if (Main.VERBOSE) System.out.println(Arrays.toString(HC_Some_time));
    if (Main.VERBOSE)
        System.out
                .println("Mean time: " + Double.toString(Arrays.stream(HC_Some_time).average().getAsDouble()));
    if (Main.VERBOSE)
        System.out.println("Sampling times");
    //if (Main.VERBOSE) System.out.println(Arrays.toString(sampling_time));
    if (Main.VERBOSE)
        System.out
                .println("Mean time: " + Double.toString(Arrays.stream(sampling_time).average().getAsDouble()));

    if (bn.getNumberOfVars() <= 50) {
        if (Main.VERBOSE)
            System.out.println("Deterministic time");
        if (Main.VERBOSE)
            System.out.println(Double.toString(determ_time));
        if (Main.VERBOSE)
            System.out.println("and probability");
        if (Main.VERBOSE)
            System.out.println(determ_prob);
    }

}

From source file:com.yahoo.pulsar.testclient.PerformanceReader.java

public static void main(String[] args) throws Exception {
    final Arguments arguments = new Arguments();
    JCommander jc = new JCommander(arguments);
    jc.setProgramName("pulsar-perf-reader");

    try {/* ww  w  .j  av  a2s. c  o m*/
        jc.parse(args);
    } catch (ParameterException e) {
        System.out.println(e.getMessage());
        jc.usage();
        System.exit(-1);
    }

    if (arguments.help) {
        jc.usage();
        System.exit(-1);
    }

    if (arguments.topic.size() != 1) {
        System.out.println("Only one topic name is allowed");
        jc.usage();
        System.exit(-1);
    }

    if (arguments.confFile != null) {
        Properties prop = new Properties(System.getProperties());
        prop.load(new FileInputStream(arguments.confFile));

        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("brokerServiceUrl");
        }

        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("webServiceUrl");
        }

        // fallback to previous-version serviceUrl property to maintain backward-compatibility
        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("serviceUrl", "http://localhost:8080/");
        }

        if (arguments.authPluginClassName == null) {
            arguments.authPluginClassName = prop.getProperty("authPlugin", null);
        }

        if (arguments.authParams == null) {
            arguments.authParams = prop.getProperty("authParams", null);
        }
    }

    // Dump config variables
    ObjectMapper m = new ObjectMapper();
    ObjectWriter w = m.writerWithDefaultPrettyPrinter();
    log.info("Starting Pulsar performance reader with config: {}", w.writeValueAsString(arguments));

    final DestinationName prefixTopicName = DestinationName.get(arguments.topic.get(0));

    final RateLimiter limiter = arguments.rate > 0 ? RateLimiter.create(arguments.rate) : null;

    ReaderListener listener = (reader, msg) -> {
        messagesReceived.increment();
        bytesReceived.add(msg.getData().length);

        if (limiter != null) {
            limiter.acquire();
        }
    };

    EventLoopGroup eventLoopGroup;
    if (SystemUtils.IS_OS_LINUX) {
        eventLoopGroup = new EpollEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2,
                new DefaultThreadFactory("pulsar-perf-reader"));
    } else {
        eventLoopGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(),
                new DefaultThreadFactory("pulsar-perf-reader"));
    }

    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setConnectionsPerBroker(arguments.maxConnections);
    clientConf.setStatsInterval(arguments.statsIntervalSeconds, TimeUnit.SECONDS);
    if (isNotBlank(arguments.authPluginClassName)) {
        clientConf.setAuthentication(arguments.authPluginClassName, arguments.authParams);
    }
    PulsarClient pulsarClient = new PulsarClientImpl(arguments.serviceURL, clientConf, eventLoopGroup);

    List<CompletableFuture<Reader>> futures = Lists.newArrayList();
    ReaderConfiguration readerConfig = new ReaderConfiguration();
    readerConfig.setReaderListener(listener);
    readerConfig.setReceiverQueueSize(arguments.receiverQueueSize);

    MessageId startMessageId;
    if ("earliest".equals(arguments.startMessageId)) {
        startMessageId = MessageId.earliest;
    } else if ("latest".equals(arguments.startMessageId)) {
        startMessageId = MessageId.latest;
    } else {
        String[] parts = arguments.startMessageId.split(":");
        startMessageId = new MessageIdImpl(Long.parseLong(parts[0]), Long.parseLong(parts[1]), -1);
    }

    for (int i = 0; i < arguments.numDestinations; i++) {
        final DestinationName destinationName = (arguments.numDestinations == 1) ? prefixTopicName
                : DestinationName.get(String.format("%s-%d", prefixTopicName, i));

        futures.add(pulsarClient.createReaderAsync(destinationName.toString(), startMessageId, readerConfig));
    }

    FutureUtil.waitForAll(futures).get();

    log.info("Start reading from {} topics", arguments.numDestinations);

    long oldTime = System.nanoTime();

    while (true) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            break;
        }

        long now = System.nanoTime();
        double elapsed = (now - oldTime) / 1e9;
        double rate = messagesReceived.sumThenReset() / elapsed;
        double throughput = bytesReceived.sumThenReset() / elapsed * 8 / 1024 / 1024;

        log.info("Read throughput: {}  msg/s -- {} Mbit/s", dec.format(rate), dec.format(throughput));
        oldTime = now;
    }

    pulsarClient.close();
}

From source file:com.yahoo.pulsar.testclient.PerformanceConsumer.java

public static void main(String[] args) throws Exception {
    final Arguments arguments = new Arguments();
    JCommander jc = new JCommander(arguments);
    jc.setProgramName("pulsar-perf-consumer");

    try {//  w  w w. j a v a  2  s .c o m
        jc.parse(args);
    } catch (ParameterException e) {
        System.out.println(e.getMessage());
        jc.usage();
        System.exit(-1);
    }

    if (arguments.help) {
        jc.usage();
        System.exit(-1);
    }

    if (arguments.topic.size() != 1) {
        System.out.println("Only one destination name is allowed");
        jc.usage();
        System.exit(-1);
    }

    if (arguments.confFile != null) {
        Properties prop = new Properties(System.getProperties());
        prop.load(new FileInputStream(arguments.confFile));

        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("brokerServiceUrl");
        }

        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("webServiceUrl");
        }

        // fallback to previous-version serviceUrl property to maintain backward-compatibility
        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("serviceUrl", "http://localhost:8080/");
        }

        if (arguments.authPluginClassName == null) {
            arguments.authPluginClassName = prop.getProperty("authPlugin", null);
        }

        if (arguments.authParams == null) {
            arguments.authParams = prop.getProperty("authParams", null);
        }
    }

    // Dump config variables
    ObjectMapper m = new ObjectMapper();
    ObjectWriter w = m.writerWithDefaultPrettyPrinter();
    log.info("Starting Pulsar performance consumer with config: {}", w.writeValueAsString(arguments));

    final DestinationName prefixDestinationName = DestinationName.get(arguments.topic.get(0));

    final RateLimiter limiter = arguments.rate > 0 ? RateLimiter.create(arguments.rate) : null;

    MessageListener listener = new MessageListener() {
        public void received(Consumer consumer, Message msg) {
            messagesReceived.increment();
            bytesReceived.add(msg.getData().length);

            if (limiter != null) {
                limiter.acquire();
            }

            consumer.acknowledgeAsync(msg);
        }
    };

    EventLoopGroup eventLoopGroup;
    if (SystemUtils.IS_OS_LINUX) {
        eventLoopGroup = new EpollEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2,
                new DefaultThreadFactory("pulsar-perf-consumer"));
    } else {
        eventLoopGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(),
                new DefaultThreadFactory("pulsar-perf-consumer"));
    }

    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setConnectionsPerBroker(arguments.maxConnections);
    clientConf.setStatsInterval(arguments.statsIntervalSeconds, TimeUnit.SECONDS);
    if (isNotBlank(arguments.authPluginClassName)) {
        clientConf.setAuthentication(arguments.authPluginClassName, arguments.authParams);
    }
    PulsarClient pulsarClient = new PulsarClientImpl(arguments.serviceURL, clientConf, eventLoopGroup);

    List<Future<Consumer>> futures = Lists.newArrayList();
    ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
    consumerConfig.setMessageListener(listener);
    consumerConfig.setReceiverQueueSize(arguments.receiverQueueSize);

    for (int i = 0; i < arguments.numDestinations; i++) {
        final DestinationName destinationName = (arguments.numDestinations == 1) ? prefixDestinationName
                : DestinationName.get(String.format("%s-%d", prefixDestinationName, i));
        log.info("Adding {} consumers on destination {}", arguments.numConsumers, destinationName);

        for (int j = 0; j < arguments.numConsumers; j++) {
            String subscriberName;
            if (arguments.numConsumers > 1) {
                subscriberName = String.format("%s-%d", arguments.subscriberName, j);
            } else {
                subscriberName = arguments.subscriberName;
            }

            futures.add(
                    pulsarClient.subscribeAsync(destinationName.toString(), subscriberName, consumerConfig));
        }
    }

    for (Future<Consumer> future : futures) {
        future.get();
    }

    log.info("Start receiving from {} consumers on {} destinations", arguments.numConsumers,
            arguments.numDestinations);

    long oldTime = System.nanoTime();

    while (true) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            break;
        }

        long now = System.nanoTime();
        double elapsed = (now - oldTime) / 1e9;
        double rate = messagesReceived.sumThenReset() / elapsed;
        double throughput = bytesReceived.sumThenReset() / elapsed * 8 / 1024 / 1024;

        log.info("Throughput received: {}  msg/s -- {} Mbit/s", dec.format(rate), dec.format(throughput));
        oldTime = now;
    }

    pulsarClient.close();
}

From source file:co.paralleluniverse.photon.Photon.java

public static void main(final String[] args) throws InterruptedException, IOException {

    final Options options = new Options();
    options.addOption("rate", true, "Requests per second (default " + rateDefault + ")");
    options.addOption("duration", true,
            "Minimum test duration in seconds: will wait for <duration> * <rate> requests to terminate or, if progress check enabled, no progress after <duration> (default "
                    + durationDefault + ")");
    options.addOption("maxconnections", true,
            "Maximum number of open connections (default " + maxConnectionsDefault + ")");
    options.addOption("timeout", true,
            "Connection and read timeout in millis (default " + timeoutDefault + ")");
    options.addOption("print", true,
            "Print cycle in millis, 0 to disable intermediate statistics (default " + printCycleDefault + ")");
    options.addOption("check", true,
            "Progress check cycle in millis, 0 to disable progress check (default " + checkCycleDefault + ")");
    options.addOption("stats", false, "Print full statistics when finish (default false)");
    options.addOption("minmax", false, "Print min/mean/stddev/max stats when finish (default false)");
    options.addOption("name", true, "Test name to print in the statistics (default '" + testNameDefault + "')");
    options.addOption("help", false, "Print help");

    try {/*from w w  w  .jav  a  2  s .  c  o m*/
        final CommandLine cmd = new BasicParser().parse(options, args);
        final String[] ar = cmd.getArgs();
        if (cmd.hasOption("help") || ar.length != 1)
            printUsageAndExit(options);

        final String url = ar[0];

        final int timeout = Integer.parseInt(cmd.getOptionValue("timeout", timeoutDefault));
        final int maxConnections = Integer
                .parseInt(cmd.getOptionValue("maxconnections", maxConnectionsDefault));
        final int duration = Integer.parseInt(cmd.getOptionValue("duration", durationDefault));
        final int printCycle = Integer.parseInt(cmd.getOptionValue("print", printCycleDefault));
        final int checkCycle = Integer.parseInt(cmd.getOptionValue("check", checkCycleDefault));
        final String testName = cmd.getOptionValue("name", testNameDefault);
        final int rate = Integer.parseInt(cmd.getOptionValue("rate", rateDefault));

        final MetricRegistry metrics = new MetricRegistry();
        final Meter requestMeter = metrics.meter("request");
        final Meter responseMeter = metrics.meter("response");
        final Meter errorsMeter = metrics.meter("errors");
        final Logger log = LoggerFactory.getLogger(Photon.class);
        final ConcurrentHashMap<String, AtomicInteger> errors = new ConcurrentHashMap<>();
        final HttpGet request = new HttpGet(url);
        final StripedTimeSeries<Long> sts = new StripedTimeSeries<>(30000, false);
        final StripedHistogram sh = new StripedHistogram(60000, 5);

        log.info("name: " + testName + " url:" + url + " rate:" + rate + " duration:" + duration
                + " maxconnections:" + maxConnections + ", " + "timeout:" + timeout);
        final DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(IOReactorConfig.custom()
                .setConnectTimeout(timeout).setIoThreadCount(10).setSoTimeout(timeout).build());

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            final List<ExceptionEvent> events = ioreactor.getAuditLog();
            if (events != null)
                events.stream().filter(event -> event != null).forEach(event -> {
                    System.err.println(
                            "Apache Async HTTP Client I/O Reactor Error Time: " + event.getTimestamp());
                    //noinspection ThrowableResultOfMethodCallIgnored
                    if (event.getCause() != null)
                        //noinspection ThrowableResultOfMethodCallIgnored
                        event.getCause().printStackTrace();
                });
            if (cmd.hasOption("stats"))
                printFinishStatistics(errorsMeter, sts, sh, testName);
            if (!errors.keySet().isEmpty())
                errors.entrySet().stream()
                        .forEach(p -> log.info(testName + " " + p.getKey() + " " + p.getValue() + "ms"));
            System.out.println(
                    testName + " responseTime(90%): " + sh.getHistogramData().getValueAtPercentile(90) + "ms");
            if (cmd.hasOption("minmax")) {
                final HistogramData hd = sh.getHistogramData();
                System.out.format("%s %8s%8s%8s%8s\n", testName, "min", "mean", "sd", "max");
                System.out.format("%s %8d%8.2f%8.2f%8d\n", testName, hd.getMinValue(), hd.getMean(),
                        hd.getStdDeviation(), hd.getMaxValue());
            }
        }));

        final PoolingNHttpClientConnectionManager mngr = new PoolingNHttpClientConnectionManager(ioreactor);
        mngr.setDefaultMaxPerRoute(maxConnections);
        mngr.setMaxTotal(maxConnections);
        final CloseableHttpAsyncClient ahc = HttpAsyncClientBuilder.create().setConnectionManager(mngr)
                .setDefaultRequestConfig(RequestConfig.custom().setLocalAddress(null).build()).build();
        try (final CloseableHttpClient client = new FiberHttpClient(ahc)) {
            final int num = duration * rate;

            final CountDownLatch cdl = new CountDownLatch(num);
            final Semaphore sem = new Semaphore(maxConnections);
            final RateLimiter rl = RateLimiter.create(rate);

            spawnStatisticsThread(printCycle, cdl, log, requestMeter, responseMeter, errorsMeter, testName);

            for (int i = 0; i < num; i++) {
                rl.acquire();
                if (sem.availablePermits() == 0)
                    log.debug("Maximum connections count reached, waiting...");
                sem.acquireUninterruptibly();

                new Fiber<Void>(() -> {
                    requestMeter.mark();
                    final long start = System.nanoTime();
                    try {
                        try (final CloseableHttpResponse ignored = client.execute(request)) {
                            responseMeter.mark();
                        } catch (final Throwable t) {
                            markError(errorsMeter, errors, t);
                        }
                    } catch (final Throwable t) {
                        markError(errorsMeter, errors, t);
                    } finally {
                        final long now = System.nanoTime();
                        final long millis = TimeUnit.NANOSECONDS.toMillis(now - start);
                        sts.record(start, millis);
                        sh.recordValue(millis);
                        sem.release();
                        cdl.countDown();
                    }
                }).start();
            }
            spawnProgressCheckThread(log, duration, checkCycle, cdl);
            cdl.await();
        }
    } catch (final ParseException ex) {
        System.err.println("Parsing failed.  Reason: " + ex.getMessage());
    }
}

From source file:edu.oregonstate.eecs.mcplan.domains.fuelworld.FuelWorldMDP.java

public static void main(final String[] argv) {
    final RandomGenerator rng = new MersenneTwister(42);
    final double discount = 0.99;
    final boolean choices = true;
    final FuelWorldState template;
    if (choices) {
        template = FuelWorldState.createDefaultWithChoices(rng);
    } else {//from   w w  w  .  j a va 2s.  c  om
        template = FuelWorldState.createDefault(rng);
    }

    for (int i = 0; i < template.adjacency.size(); ++i) {
        System.out.print(i);
        System.out.print(" -> {");
        final TIntList succ = template.adjacency.get(i);
        for (int j = 0; j < succ.size(); ++j) {
            System.out.print(" " + succ.get(j));
        }
        System.out.println(" }");
    }

    final FuelWorldMDP mdp = new FuelWorldMDP(template);
    final int Nfeatures = new PrimitiveFuelWorldRepresentation(template).phi().length;
    final SparseValueIterationSolver<FuelWorldState, FuelWorldAction> vi = new SparseValueIterationSolver<FuelWorldState, FuelWorldAction>(
            mdp, discount);
    vi.run();

    final PrimitiveFuelWorldRepresenter repr = new PrimitiveFuelWorldRepresenter();
    final ArrayList<Attribute> attr = new ArrayList<Attribute>();
    attr.addAll(repr.attributes());
    attr.add(WekaUtil.createNominalAttribute("__label__", mdp.A().cardinality()));
    final Instances instances = WekaUtil
            .createEmptyInstances("fuelworld" + (choices ? "_choices" : "") + "_pistar", attr);
    final Policy<FuelWorldState, FuelWorldAction> pistar = vi.pistar();
    final Generator<FuelWorldState> g = mdp.S().generator();
    while (g.hasNext()) {
        final FuelWorldState s = g.next();
        if (s.location == s.goal) {
            continue;
        }
        pistar.setState(s, 0L);
        final FuelWorldAction astar = pistar.getAction();
        System.out.println("" + s + " -> " + astar);
        final double[] phi = new double[Nfeatures + 1];
        Fn.memcpy_as_double(phi, new PrimitiveFuelWorldRepresentation(s).phi(), Nfeatures);
        phi[Nfeatures] = mdp.A().index(astar);
        WekaUtil.addInstance(instances, new DenseInstance(1.0, phi));
    }

    WekaUtil.writeDataset(new File("."), instances);

    final MeanVarianceAccumulator ret = new MeanVarianceAccumulator();
    final MeanVarianceAccumulator steps = new MeanVarianceAccumulator();
    final int Ngames = 100000;
    for (int i = 0; i < Ngames; ++i) {
        final FuelWorldState s0;
        if (choices) {
            s0 = FuelWorldState.createDefaultWithChoices(rng);
        } else {
            s0 = FuelWorldState.createDefault(rng);
        }
        final FuelWorldSimulator sim = new FuelWorldSimulator(s0);

        final Episode<FuelWorldState, FuelWorldAction> episode = new Episode<FuelWorldState, FuelWorldAction>(
                sim, JointPolicy.create(pistar));
        final RewardAccumulator<FuelWorldState, FuelWorldAction> racc = new RewardAccumulator<FuelWorldState, FuelWorldAction>(
                sim.nagents(), discount);
        episode.addListener(racc);

        final long tstart = System.nanoTime();
        episode.run();
        final long tend = System.nanoTime();
        final double elapsed_ms = (tend - tstart) * 1e-6;

        ret.add(racc.v()[0]);
        steps.add(racc.steps());
    }

    System.out.println("****************************************");
    System.out.println("Average return: " + ret.mean());
    System.out.println("Return variance: " + ret.variance());
    System.out.println("Confidence: " + ret.confidence());
    System.out.println("Steps (mean): " + steps.mean());
    System.out.println("Steps (var): " + steps.variance());
}

From source file:io.anserini.search.SearchWebCollection.java

public static void main(String[] args) throws Exception {

    SearchArgs searchArgs = new SearchArgs();
    CmdLineParser parser = new CmdLineParser(searchArgs, ParserProperties.defaults().withUsageWidth(90));

    try {//from ww  w  .ja  v  a  2s . c om
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        System.err.println("Example: SearchWebCollection" + parser.printExample(OptionHandlerFilter.REQUIRED));
        return;
    }

    LOG.info("Reading index at " + searchArgs.index);
    Directory dir;
    if (searchArgs.inmem) {
        LOG.info("Using MMapDirectory with preload");
        dir = new MMapDirectory(Paths.get(searchArgs.index));
        ((MMapDirectory) dir).setPreload(true);
    } else {
        LOG.info("Using default FSDirectory");
        dir = FSDirectory.open(Paths.get(searchArgs.index));
    }

    Similarity similarity = null;

    if (searchArgs.ql) {
        LOG.info("Using QL scoring model");
        similarity = new LMDirichletSimilarity(searchArgs.mu);
    } else if (searchArgs.bm25) {
        LOG.info("Using BM25 scoring model");
        similarity = new BM25Similarity(searchArgs.k1, searchArgs.b);
    } else {
        LOG.error("Error: Must specify scoring model!");
        System.exit(-1);
    }

    RerankerCascade cascade = new RerankerCascade();
    boolean useQueryParser = false;
    if (searchArgs.rm3) {
        cascade.add(new Rm3Reranker(new EnglishAnalyzer(), FIELD_BODY,
                "src/main/resources/io/anserini/rerank/rm3/rm3-stoplist.gov2.txt"));
        useQueryParser = true;
    } else {
        cascade.add(new IdentityReranker());
    }
    FeatureExtractors extractors = null;
    if (searchArgs.extractors != null) {
        extractors = FeatureExtractors.loadExtractor(searchArgs.extractors);
    }

    if (searchArgs.dumpFeatures) {
        PrintStream out = new PrintStream(searchArgs.featureFile);
        Qrels qrels = new Qrels(searchArgs.qrels);
        cascade.add(new WebCollectionLtrDataGenerator(out, qrels, extractors));
    }

    Path topicsFile = Paths.get(searchArgs.topics);

    if (!Files.exists(topicsFile) || !Files.isRegularFile(topicsFile) || !Files.isReadable(topicsFile)) {
        throw new IllegalArgumentException(
                "Topics file : " + topicsFile + " does not exist or is not a (readable) file.");
    }

    TopicReader tr = (TopicReader) Class
            .forName("io.anserini.search.query." + searchArgs.topicReader + "TopicReader")
            .getConstructor(Path.class).newInstance(topicsFile);
    SortedMap<Integer, String> topics = tr.read();

    final long start = System.nanoTime();
    SearchWebCollection searcher = new SearchWebCollection(searchArgs.index);
    searcher.search(topics, searchArgs.output, similarity, searchArgs.hits, cascade, useQueryParser,
            searchArgs.keepstop);
    searcher.close();
    final long durationMillis = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    LOG.info("Total " + topics.size() + " topics searched in "
            + DurationFormatUtils.formatDuration(durationMillis, "HH:mm:ss"));
}