注意:這篇文章適用于 IBM® Rational® Performance Tester Version 6.1.2.002
好的測試不僅僅是重復執行相同的動作。為了更好的模擬實際用戶的動作,測試人員可以利用IBM Rational Performance Tester中的數據池。這是遠超出使用靜態記錄測試的一步,因為它允許測試為每個執行來選擇不同的變量。例如,無論什么時候應用軟件要求用戶輸入搜索條件,比如一個電影的名字,一個標題或者標題的一個部分都可以從數據池中選擇。
目前Rational Performance Tester數據池僅僅是順序存儲。在這個電影搜索的例子中,每次你運行這個測試時,相同電影名稱的搜尋都是以相同的順序來進行處理的。通過對要搜索的標題進行隨機選擇,可以提高測試的穩健性。
數據池文本文件
在i5/OS系統測試環境中,IBM測試自動化小組自從2004年就一直在將Mercury LoadRunner的腳本轉換到Rational Performance Tester。為了隨機化i5/OS測試的變量選擇,我們創建了一個Rational Performance Tester自定義代碼的包,來對數據池中的元素進行隨機存儲。這個執行一點都沒有用到Rational Performance Tester數據池的特性。相反,Rational Performance Tester DataPool 類讀取的文本文件中包含要使用的數據池條目。
這個將選擇元素隨機化的數據池文件是一個每行僅包含一個元素的純文本文件。用Rational Performance Tester來實現它的一個簡單的方法,就是為測試項目的文本文件創建一個數據池文件夾。一個文件輸出包括這些文本文件,因為它們被包含在Rational Performance Tester項目中。當轉換Mercury LoadRunner腳本時,你可以通過LoadRunner數據文件來實現。
RPTDataPool類
這個文本數據的文件名傳給創建者,整個文件在首次訪問嘗試時就被讀取進入了 Java™ Vector 對象。為了從這個數據池中隨機重新找到一個條目,可以使用getaDataPoolItem 方法。(參見列表1。)
注意事項
記住整個文件在測試的開始就已被讀入存儲器是十分重要的。巨大的數據池將會用到大量的內存,這將會降低加載和Rational Performance啟動的速度。巨大的Rational Performance Tester測試數據池也會發生類似的情況。
你可以使用每行包含多個元素的數據池,但是用戶必須在這個測試的自定義代碼中增加一些功能來取出單個元素。
列表1. getaDataPoolItem 方法
import .io.*;
import .util.Vector;
public class RPTDataPool {
private boolean DataPoolIsLoaded = false;
private String DataPoolFileName;
private Vector DataPool;
private int DataPoolCount = 0;
public RPTDataPool( String fileName ) {
DataPoolFileName = fileName;
DataPool = new Vector();
DataPoolCount = fillVector( DataPoolFileName, DataPool);
DataPoolIsLoaded = true;
}
public String getaDataPoolItem( ) {
if( !DataPoolIsLoaded ) {
//System.out.println("loading:" + DataPoolFileName);
DataPoolIsLoaded = true;
}
return (String) DataPool.elementAt((int)
(Math.floor(Math.random() * (DataPoolCount))));
}
private int fillVector( String fileName, Vector FileLines) {
// take the Datapool file and read it into a Vector
// do this only once per test
int fileLineCounter = 0;
BufferedReader brInReader = null;
// read from the file
// try to setup a buffered reader and open the file
try { brInReader = new BufferedReader(new FileReader(fileName));}
catch(Exception error)
{
//System.out.println("Error Opening DataPool File: " + error);
return 0;
}
// read the file and place the lines in an array
// get the first line of the file
String sInLine = ReadLine(brInReader);
// read through the file
while (sInLine != null)
{
//System.out.println("Storing '"+ sInLine+"'");
FileLines.addElement(sInLine);
// read the next line
sInLine = ReadLine(brInReader);
fileLineCounter++;
}
// At this point, the FileLines Vector has all the lines
from the file and fileLineCounter
// indicates the max index value for the array
return fileLineCounter;
}
// ReadLine
// This method will read a line from a given file and return the string value
// ********************************************************************************
原文轉自:http://www.anti-gravitydesign.com