使用事务和统计信息

可使用定制代码来启动事务,收集事务期间的其他统计信息,以及停止事务。

以下代码显示了如何启动事务。测试执行服务生成的事务将自动创建和管理统计信息。

package customcode;

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

/** 
 * @author IBM Custom Code Samples
 */ 
public class BeginTransaction implements
		com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

	/** 	
	 * Instances of this will be created using the no-arg constructor.
	 */ 
		public BeginTransaction() { 	
	}  	

	/** 	
	 	 * For Javadoc information on the ICustomCode2 and ITestExecutionServices interfaces, 	
	 	 * see the 'Test execution services interfaces and classes' help topic. 	
	 */ 	
	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;
	}  
}

以下代码显示了如何收集事务期间的其他统计信息。

package customcode;

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;

/**
 * @author IBM Custom Code Samples
 */
public class BodyTransaction implements
		com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

	/**
	 * Instances of this will be created using the no-arg constructor.
	 */
		public BodyTransaction() {
	}

	/** 	
	 	 * For Javadoc information on the ICustomCode2 and ITestExecutionServices interfaces, 	
	 	 * see the 'Test execution services interfaces and classes' help topic. 	
	 */ 	
	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)
        			relationshipRange.setLowestVersionType(RelationshipRange.GREATER_THAN_OR_EQUAL_TO);
        
		return null;
	}

}

以下代码显示了如何停止事务。

package customcode;

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

/**
 * @author IBM Custom Code Samples
 */
public class EndTransaction implements
		com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

	/**
	 * Instances of this will be created using the no-arg constructor.
	 */
		public EndTransaction() {
	}

	/** 	
	 	 * For Javadoc information on the ICustomCode2 and ITestExecutionServices interfaces, 	
	 	 * see the 'Test execution services interfaces and classes' help topic. 	
	 */ 	
	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"); 		
				relationshipRange.setType(RelationshipRange.HIGHEST_VERSION);
		return null;
	}

}

反馈