トランザクションおよび統計の使用

カスタム・コードを使用して、トランザクションの開始、トランザクション中の追加統計の収集、およびトランザクションの停止を行うことができます。

以下のコードは、トランザクションの開始方法を示しています。 テスト実行サービスによって生成されるトランザクションでは、統計が自動的に作成され、管理されます。

package customcode;

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

/** 
 * @作成者 IBM カスタム・コード・サンプル
 */ 
public class BeginTransaction implements
		com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

	/** 	
	 * no-arg コンストラクターを使用してこのインスタンスを作成する。 
	 */ 
	public BeginTransaction() { 	
	}  	

	/** 	
	 * ICustomCode2 および ITestExecutionServices インターフェースに関する Javadoc 情報については、 	
	 * 「テスト実行サービス・インターフェースおよびクラス」のヘルプ・トピックを参照。 	
	 */ 	
	public String exec(ITestExecutionServices tes, String[] args) { 		
		// トランザクションの名前はデータ相関メカニズムによって渡された可能性がある。 
		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;

/**
 * @作成者 IBM カスタム・コード・サンプル
 */
public class BodyTransaction implements
		com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

	/**
	 * no-arg コンストラクターを使用してこのインスタンスを作成する。
	 */
	public BodyTransaction() {
	}

	/** 	
	 * ICustomCode2 および ITestExecutionServices インターフェースに関する Javadoc 情報については、 	
	 * 「テスト実行サービス・インターフェースおよびクラス」のヘルプ・トピックを参照。 	
	 */ 	
	public String exec(ITestExecutionServices tes, String[] args) {
		IStatTree tranStat;
		IStatTree timeStat;
		IStatTree countStat;
		
		IStat timeDataStat = null;		// 時刻 RANGE のカウンター
		IScalar countDataStat = null;	// カウント SCALAR のカウンター
		
		ITime timer = tes.getTime();
		
        IStatTree rootStat = tes.getStatisticsManager().getStatTree();
        if (rootStat != null) {
        	// これらのカウンターは階層をセットアップする
        	tranStat = rootStat.getStat("Transactions", StatType.STRUCTURE);
        	timeStat = tranStat.getStat("Body Time", StatType.STRUCTURE);
        	countStat = tranStat.getStat("Bocy Count", StatType.STRUCTURE);
        
        	// カウンターの名前はデータ相関メカニズムによって渡された可能性がある
        	timeDataStat = (IStat) timeStat.getStat("foo", StatType.RANGE);
        	countDataStat = (IScalar) countStat.getStat("foo", StatType.SCALAR);
        }

        // 開始時刻を取得する
        long startTime = timer.timeInTest();
        
        // 作業を実行する
        // 作業は何でもよい
        
        // 終了時刻を取得する
        long endTime = timer.timeInTest();
        
        // timeDataStat を経過時間で更新する
        if (timeDataStat != null)
        	timeDataStat.submitDataPoint(endTime - startTime);
        
        // countDataStat を更新する
        if (countDataStat != null)
        	countDataStat.increment();
        
		return null;
	}

}

以下のコードは、トランザクションの停止方法を示しています。

package customcode;

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

/**
 * @作成者 IBM カスタム・コード・サンプル
 */
public class EndTransaction implements
		com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

	/**
	 * no-arg コンストラクターを使用してこのインスタンスを作成する。
	 */
	public EndTransaction() {
	}

	/** 	
	 * ICustomCode2 および ITestExecutionServices インターフェースに関する Javadoc 情報については、 	
	 * 「テスト実行サービス・インターフェースおよびクラス」のヘルプ・トピックを参照。 	
	 */ 	
	public String exec(ITestExecutionServices tes, String[] args) {
		// トランザクションの名前はデータ相関メカニズムによって渡された可能性がある。 
		ITransaction foo = tes.getTransaction("foo");
		foo.stop();
		return null;
	}

}

フィードバック