Example: lr_stop_transaction and lr_resume_transaction

In the following example, a transaction is stopped and resumed to show the effect on the reported duration.

Action() {
    int i;
    int iteration = 100;
    char aStr[100];
    lr_start_transaction("Stop&Start");
   
 /* Do part of business process. Represented
        here by loop to ensure that some 
        time passes. */
   
 for (i=0; i < iteration; ++i) {
        sprintf(aStr, "%d", i);    
        lr_log_message("%d",i);
    }
    /* Output the duration to this point (0.343750 seconds)
        with transaction active. */
    lr_output_message("First time = %f",lr_get_transaction_duration("Stop&Start"));
    // Stop the transaction
    lr_stop_transaction("Stop&Start");
    /* Output the duration to this point (0.359375 seconds)
        with transaction stopped. */

    lr_output_message("Immediately after stop = %f", lr_get_transaction_duration("Stop&Start"));
    // Ensure that some time passes
    for (i=0; i < iteration; ++i) {
        sprintf(aStr, "%d", i);
        lr_log_message("%d",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_output_message("After stop and loop = %f", lr_get_transaction_duration("Stop&Start"));
    // Resume the transaction
    lr_resume_transaction("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_output_message("After resume time = %f", lr_get_transaction_duration("Stop&Start"));
    // Add time to duration
    for (i=0; i < iteration; ++i) {
        sprintf(aStr, "%d", i);
        lr_log_message("%d",i);
        }
    /* Total time reported (1.140625 seconds) */
    lr_output_message("After resume and loop = %f", lr_get_transaction_duration("Stop&Start"));    
    // End transaction
    lr_end_transaction("Stop&Start", LR_AUTO);
    return 0;
}
Output:
Action.c(11): Notify: Transaction Stop&Start started.
Action.c(23): First time = 0.343750
Action.c(27): Notify: Transaction Stop&Start stopped.
Action.c(31): Immediately after stop = 0.359375
Action.c(45): After stop and loop = 0.359375
Action.c(49): Notify: Transaction Stop&Start resumed.
Action.c(55): After resume time = 0.781250
Action.c(65): After resume and loop = 1.140625
Action.c(70): Notify: Transaction Stop&Start ended with Pass status (Duration: 1.1406).