Share this page

# JavaScript Transaction Example

With the necessary variations, this example also applies to sub-transactions and transaction instances.

Copy code

    function Action(){
        var i;
        var iteration = 100;
        var aStr =[];
        var think_time;
        var wasteTime, actualElapsedTime;
        var MasterT, timer;

        lr.startTransaction("Stop&Start");

     /* Call web.url as part of business process.
            In this example, the call is represented
            by a loop to ensure that some
            time passes. */
       // web.url(.....);
       for (i=0; i < iteration; ++i) {
            lr.logMessage(i);
        }

       if (lr.getTransactionStatus() == lr.FAIL) {
            /* web_url has failed the transaction. No point in continuing,
             * because future calls will probably fail too */
            lr.failTransWithError("web.url call failed transaction.");
            return;
       }

        /* Output the duration to this point (0.343750 seconds)
            with transaction active. */
        lr.outputMessage("First time = "+lr.getTransactionDuration("Stop&Start"));

         // Create some waste time.
        timer = lr.startTimer();
        for (i=0; i < iteration; ++i) {
            lr.logMessage(i);
        }
        wasteTime = lr.endTimer(timer);
        /* Convert Timer in seconds to wasted time in milliseconds and add to internally generated waste time */
        wasteTime *= 1000;
        lr.wastedTime(wasteTime);
        lr.outputMessage("After lr_waste_time: Duration = "
            + lr.getTransactionDuration("Demo"),+" - Waste = "
            + lr.getTransactionWastedTime("Demo"));

        // Stop the transaction
        lr.stopTransaction("Stop&Start");
        /* Output the duration to this point (0.359375 seconds)
            with transaction stopped. */

        lr.outputMessage("Immediately after stop = "
            +lr.getTransactionDuration("Stop&Start"));

        // Ensure that some time passes.
        for (i=0; i < iteration; ++i) {
            lr.logMessage(i);
        }
        /* Output the duration to this point (0.359375 seconds)
            with transaction still stopped but
            more time passed. Note that time is the same.
            The time spent in the loop while the transaction
            was stopped is not reported. */
        lr.outputMessage("After stop and loop = "+lr.getTransactionDuration("Stop&Start"));
        // Resume the transaction
        lr.resumeTransaction("Stop&Start");
        /* Note that with the transaction resumed,
            all the time passed since the start is
            reported (0.781250 seconds), including the time spent
            in the loop while the transaction was stopped. */
        lr.outputMessage("After resume time = "+lr.getTransactionDuration("Stop&Start"));
        // Add time to duration
        for (i=0; i < iteration; ++i) {
            lr.logMessage(i);
        }

        /* Check total time reported after resume and loop */
        if ( lr.getTransactionDuration("Stop&Start") > 2)
           lr.setTransactionStatus(lr.FAIL);

        think_time=lr.getTransactionThinkTime("Stop&Start");
        if (think_time)
           lr.outputMessage("The transaction think time is "+think_time+" seconds");
        else
           lr.outputMessage("The think time cannot be determined.");

        // End transaction
        lr.endTransaction("Stop&Start",lr.AUTO);
        return 0;
    }

Tips to improve your search results... VuGen Function Reference Version 26.1-26.3 Function Reference 26.1-26.3 All View Local Help Center View Online Help Center Version: