O código a seguir mostra como iniciar uma transação. As transações geradas pelos serviços de execução de teste criam e gerenciam as estatísticas automaticamente.
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) {
// o nome da transação poderia ter sido transmitido por meio do mecanismo de correlação de dados.
ITransaction foo = tes.getTransaction("foo");
foo.start();
return null;
}
}
O código a seguir mostra como reunir estatísticas adicionais durante uma transação.
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; // contador para o tempo RANGE
IScalar countDataStat = null; // contador para a contagem SCALAR
ITime timer = tes.getTime();
IStatTree rootStat = tes.getStatisticsManager().getStatTree();
if (rootStat != null) {
// esses contadores configuram a hierarquia
tranStat = rootStat.getStat("Transactions", StatType.STRUCTURE);
timeStat = tranStat.getStat("Body Time", StatType.STRUCTURE);
countStat = tranStat.getStat("Bocy Count", StatType.STRUCTURE);
// o nome dos contadores poderia ter sido transmitido por meio do mecanismo de correlação de dados
timeDataStat = (IStat) timeStat.getStat("foo", StatType.RANGE);
countDataStat = (IScalar) countStat.getStat("foo", StatType.SCALAR);
}
// obter a hora de início
long startTime = timer.timeInTest();
// realizar o trabalho
// seja ele qual for
// obter o horário de encerramento
long endTime = timer.timeInTest();
// atualizar timeDataStat com o tempo decorrido
if (timeDataStat != null)
timeDataStat.submitDataPoint(endTime - startTime);
// atualizar o countDataStat
if (countDataStat != null)
countDataStat.increment();
return null;
}
}
O código a seguir mostra como parar uma transação.
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) {
// o nome da transação poderia ter sido transmitido por meio do mecanismo de correlação de dados.
ITransaction foo = tes.getTransaction("foo");
foo.stop();
return null;
}
}