TestNG中线程间共享数据
我在testNG中有一个testmethod,threadPoolSize=10,它在数据库中创建记录 我需要计算所有线程创建所有记录所花费的总时间 请帮我找到同样的TestNG中线程间共享数据,testng,Testng,我在testNG中有一个testmethod,threadPoolSize=10,它在数据库中创建记录 我需要计算所有线程创建所有记录所花费的总时间 请帮我找到同样的 @Test(threadPoolSize = 10, invocationCount = 10) public void testCreate(){ long StartTime = new Date().getTime() ; //operation to create records
@Test(threadPoolSize = 10, invocationCount = 10)
public void testCreate(){
long StartTime = new Date().getTime() ;
//operation to create records
long EndTime = new Date().getTime() ;
}
如何计算上述代码中所有线程所花费的时间
上面的代码一次只给我一个线程占用的时间。我会调用一个外部函数,比如startTick,它会跟踪startTime,然后一旦设置,它就不会被覆盖,而endTick会收集所有的endtimes。在后面的测试中,我可以做endTick-startTick..类似于:
startTick(){
if(startTime == null)
startTime = new Date().getTime();
}
testCreate() {
startTick();
//operation to create records
endTick();
}
endTick(){
endTime = new Date().getTime();
}
@AfterTest
afterTest(){
endTime - startTime;
}
听起来您需要发布关于测试时间的数据,以便可以在报告中使用,而不是在同一测试的其他线程中使用 为此,您可以将每个测试的结果存储在某个中心位置,稍后再查找总数。以下内容对你有用吗
public class TimeTest {
Map<Long, Long> timeRecord = new ConcurrentHashMap<Long, Long>();
@Test(threadPoolSize = 10, invocationCount = 10)
public void testCreate(){
long threadID = Thread.currentThread().getId();
long startTime = new Date().getTime() ;
//operation to create records
long endTime = new Date().getTime();
long diffTime = endTime - startTime;
timeRecord.put(threadID, diffTime);
// Log time taken
System.out.println("TestMethod[" + threadID + "] TestTime: " + diffTime);
}
@AfterTest
public void displayTime() {
long runtime = 0;
for (Long threadID : timeRecord.keySet()) {
runtime += timeRecord.get(threadID);
}
System.out.println("AfterSuite TotalTime: " + runtime);
}
}
但是,时间可能与上面的时间不同,因为可能会有额外的开销。我不熟悉TestNG,但如果可能的话,我会设置一个测试前和测试后挂钩,比较开始和结束时间。使用静态volatile字段计算我的总工作时间。。谢谢
@AfterMethod
public void recordTime(ITestResult testResult){
long threadID = Thread.currentThread().getId();
long timeDiff = testResult.getEndMillis() - testResult.getStartMillis();
// Log time taken
System.out.println("AfterMethod[" + threadID + "] TestTime: " + timeDiff);
}