Use transactions and statistics

+

Search Tips   |   Advanced Search

 

Overview

You can use custom code to...


Start a transaction

Transactions that are generated by test execution services automatically create and manage statistics.

package test;

import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.ITransaction;

public class BeginTransaction implements com.ibm.rational.test.lt.kernel.custom.ICustomCode2 
{
    public BeginTransaction() 
    {     
    }      

    public String exec(ITestExecutionServices tes, String[] args) 
    {         
        // the name of the transaction could have been passed in via data correlation mechanism.         
        ITransaction foo = tes.getTransaction("foo");         
        foo.start();         
        return null; 
    }  
}


Gather additional statistics during a transaction

package test;

import com.ibm.rational.test.lt.kernel.ITime;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.statistics.IScalar;
import com.ibm.rational.test.lt.kernel.statistics.IStat;
import com.ibm.rational.test.lt.kernel.statistics.IStatTree;
import com.ibm.rational.test.lt.kernel.statistics.impl.StatType;


public class BodyTransaction implements
        com.ibm.rational.test.lt.kernel.custom.ICustomCode2 
{

    public BodyTransaction() 
    {
    }


    public String exec(ITestExecutionServices tes, String[] args) 
    {
        IStatTree tranStat;
        IStatTree timeStat;
        IStatTree countStat;
        
        IStat timeDataStat = null;        // counter for the time RANGE
        IScalar countDataStat = null;     // counter for the count SCALAR
        
        ITime timer = tes.getTime();
        
        IStatTree rootStat = tes.getStatisticsManager().getStatTree();

        if (rootStat != null) 
        {
            // these counters set up the hierarchy
            tranStat = rootStat.getStat("Transactions", StatType.STRUCTURE);
            timeStat = tranStat.getStat("Body Time", StatType.STRUCTURE);
            countStat = tranStat.getStat("Bocy Count", StatType.STRUCTURE);
        
            // the name of the counters could have been passed in via data correlation mechanism
            timeDataStat = (IStat) timeStat.getStat("foo", StatType.RANGE);
            countDataStat = (IScalar) countStat.getStat("foo", StatType.SCALAR);
        }

        // get the start time
        long startTime = timer.timeInTest();
        
        // do the work
        // whatever that work might be
        
        // get the end time
        long endTime = timer.timeInTest();
        
        // update timeDataStat with the elapsed time
        if (timeDataStat != null)
            timeDataStat.submitDataPoint(endTime - startTime);
        
        // update the countDataStat
        if (countDataStat != null)
            countDataStat.increment();
        
        return null;
    }

}


stop a transaction

package test;

import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.ITransaction;


public class EndTransaction implements
        com.ibm.rational.test.lt.kernel.custom.ICustomCode2 
{

    public EndTransaction() 
    {
    }

    public String exec(ITestExecutionServices tes, String[] args) 
    {
        // the name of the transaction could have been passed in via data correlation mechanism.
        ITransaction foo = tes.getTransaction("foo");
        foo.stop();
        return null;
    }

}