不谋万世者,不足谋一时;不谋全局者,不足谋一域。君子敏于事而慎于言 新浪微薄:土司阿哈

使用自定义代码对数据池进行随机访问

上一篇 / 下一篇  2008-06-17 13:11:27 / 个人分类:Rational

1208328177
  • 自动化测试:
  • 测试文档:

使用自定义代码对数据池进行随机访问

PO J)x%dp3P L0

为了更加稳健的测试,可以用自定义代码使Performance Tester随机化。目前,Performance Tester数据池中的元素只能按照顺序进行访问。这篇文章讨论了作者如何创建自定义的Performance Tester代码,并用随机数据访问来实现数据池。这篇文章提供的了RPTDataPool类,以及如何实现它的详细说明。51Testing软件测试网9B"B?x.fi;y&}L,Q

   注意:这篇文章适用于IBM® Rational® Performance Tester Version6.1.2.002

5f}MZv5lA!s0

   好的测试不仅仅是重复执行相同的动作。为了更好的模拟实际用户的动作,测试人员可以利用IBM Rational Performance Tester中的数据池。这是远超出使用静态记录测试的一步,因为它允许测试为每个执行来选择不同的变量。例如,无论什么时候应用软件要求用户输入搜索条件,比如一个电影的名字,一个标题或者标题的一个部分都可以从数据池中选择。

-Ohjx})~z9^ t0

   目前Rational Performance Tester数据池仅仅是顺序存储。在这个电影搜索的例子中,每次你运行这个测试时,相同电影名称的搜寻都是以相同的顺序来进行处理的。通过对要搜索的标题进行随机选择,可以提高测试的稳健性。51Testing软件测试网:Fz:}1]6u

   数据池文本文件51Testing软件测试网_-?HWCG6k+p

   i5/OS系统测试环境中,IBM测试自动化小组自从2004年就一直在将Mercury LoadRunner的脚本转换到Rational Performance Tester。为了随机化i5/OS测试的变量选择,我们创建了一个Rational Performance Tester自定义代码的包,来对数据池中的元素进行随机存储。这个执行一点都没有用到Rational Performance Tester数据池的特性。相反,Rational Performance Tester DataPool类读取的文本文件中包含要使用的数据池条目。

q%| fT mT%c0

   这个将选择元素随机化的数据池文件是一个每行仅包含一个元素的纯文本文件。用Rational Performance Tester来实现它的一个简单的方法,就是为测试项目的文本文件创建一个数据池文件夹。一个文件输出包括这些文本文件,因为它们被包含在Rational Performance Tester项目中。当转换Mercury LoadRunner脚本时,你可以通过LoadRunner数据文件来实现。51Testing软件测试网w |$Bd} Ai

RPTDataPool

F2U t*N8@ R_e0

   这个文本数据的文件名传给创建者,整个文件在首次访问尝试时就被读取进入了Java™ Vector对象。为了从这个数据池中随机重新找到一个条目,可以使用getaDataPoolItem方法。(参见列表1。)51Testing软件测试网AQ#u6E7R[e

注意事项

:J#Z}8gZ0l@{}S0

   记住整个文件在测试的开始就已被读入存储器是十分重要的。巨大的数据池将会用到大量的内存,这将会降低加载和Rational Performance启动的速度。巨大的Rational Performance Tester测试数据池也会发生类似的情况。51Testing软件测试网"c:rv#aa7Yn-v

   你可以使用每行包含多个元素的数据池,但是用户必须在这个测试的自定义代码中增加一些功能来取出单个元素。

D!_ p$D4l&R*J vW,i0

51Testing软件测试网5d;Glk7}aG
   列表1. getaDataPoolItem方法
yFp_1_7v0    
9ri-D jcU*?0import .io.*;51Testing软件测试网BDG#az"B%e#q
import .util.Vector;
S"qJ.X]T _k+uJqR0public class RPTDataPool {51Testing软件测试网R)ca/re4g-O
    private boolean DataPoolIsLoaded = false;
$^7P!XLff$_0    private String DataPoolFileName;
zVn]g;}.D*S?x I0    private Vector DataPool;
*{HnR0m0    private int DataPoolCount = 0;
)xD7k!\b0 
&b8D[^n\'Pp%?0    public RPTDataPool( String fileName )  {
]n*uUs0        DataPoolFileName = fileName;
l5G IZy.r-p W\.A!H0        DataPool = new Vector();51Testing软件测试网W7m%ln1Hyd
        DataPoolCount = fillVector( DataPoolFileName, DataPool);
dJ|^/eKk0        DataPoolIsLoaded = true;
Gv2D@ al;arb0        }
W1j*OZ:~#m+V"P p6|-j0  
hTV9U:Pcf0    public String getaDataPoolItem(  )  {
'|m2n$l"P0        if( !DataPoolIsLoaded ) {
-C[ ghR c+]D z(`0            //System.out.println("loading:" + DataPoolFileName);
U_Ag|!eqm0            DataPoolIsLoaded = true;
F%I7^,| lT#e Ae)O0        }51Testing软件测试网-\ Pf_I8St$C(f
        return (String) DataPool.elementAt((int)51Testing软件测试网 a&H5xI'G&L,ns
          (Math.floor(Math.random() * (DataPoolCount))));51Testing软件测试网!u-F\t+['T KBF$J[h
    }
R){$Yi#`1N3pCa8n0   51Testing软件测试网R`Utl\n
    private int fillVector( String fileName, Vector FileLines) {
kga^&E,qb2S0        // take the Datapool file and read it into a Vector51Testing软件测试网?(D;R1IpY ]Ou
        // do this only once per test51Testing软件测试网Yy1lc'Kn6YtfMN
        int fileLineCounter = 0;51Testing软件测试网9\t)]N x%es?
       51Testing软件测试网3G [a({E
        BufferedReader brInReader = null;51Testing软件测试网s4Pn}ODl`l
       51Testing软件测试网t @N jw
        // read from the file
!TD*K9fB0        // try to setup a buffered reader and open the file51Testing软件测试网)^Tr'M#u4D }w
        try  { brInReader = new BufferedReader(new FileReader(fileName));}
K1L L'yb!C0        catch(Exception error)
2_$M1a8P B2pB j:Q6L"zt0        {
!J3kU8Y Rg0      //System.out.println("Error Opening DataPool File: " + error);
6@"D@8f/O {Qt0      return 0;
W6kjz_i0  }
5|"V)oXR4H%S[0  51Testing软件测试网WO p8^[#xr
  // read the file and place the lines in an array51Testing软件测试网wc.be.B'w
  // get the first line of the file51Testing软件测试网xe3hdM%fk
  String sInLine = ReadLine(brInReader);51Testing软件测试网 [6hNBK&f7Df
  51Testing软件测试网}Hey*]
  // read through the file51Testing软件测试网7]L(}pRg3OSAj
  while (sInLine != null)
3th5V:?#H0  {
l!r1txcV}0      //System.out.println("Storing '"+ sInLine+"'");51Testing软件测试网3~&xl$}tI.c
      FileLines.addElement(sInLine);
2U{PDvbfY;A0     
7nAK1IY(n0      // read the next line51Testing软件测试网 w(N-ZG'f
      sInLine = ReadLine(brInReader);51Testing软件测试网"[4V6P,j d"n$B6?
     
}V yIw0      fileLineCounter++;
:H6a*Pd`,`VM0  }51Testing软件测试网9]&G7C\,^Lw-RJd
  
N{um*i&|`0  // At this point, the FileLines Vector has all the lines
^s!yqk0  from the file and fileLineCounter
6\Lr-N,X.E0  
%u~ TCe0  // indicates the max index value for the array51Testing软件测试网B@/?qP&D
  return fileLineCounter;
)w3V"Ip i7ScK0  }
zS"|Pq0  51Testing软件测试网.f-[ iZV$W8]6MN
  // ReadLine
AN`o'L9X,i0  // This method will read a line from a given file and return the string value
ue,eN(V0  // ********************************************************************************
1r5t'T&W2^L0  51Testing软件测试网TgJWA J%T9xit.s
  private String ReadLine(BufferedReader brReader) {
Rl$MK!yr2Lzy0      String sReadLine = "";51Testing软件测试网L+w-eVl.k Z@C
      try  {  sReadLine = brReader.readLine();}
d T_N$?l8fT4O0      catch (IOException error)
L%P5m5Y3tp"P0      {//System.out.println("DataPool.test Read Error: " + error);}
ljh2\s0     
Q$B |c,l&nZ+{g3k0      return sReadLine;51Testing软件测试网6M(hL K:U \+x
  }
1wC7~J3m$m h0  // end of Read
a/TJd;@0  }
51Testing软件测试网@GG1NB$n,A'B(u4G


[)Q S.zL y:J/FE0
   在测试中实例化数据池对象51Testing软件测试网+ln1OSl_|+vze

   Rational Performance Tester测试项目创建一个特殊的类来实例化这个数据池的对象。这里,用MovieSearchDataPools来举例说明,这个电影搜索项目需要三个不同的数据池(请看列表2

R x1O X1h4S HV3C0


l]+E6mB9H/bi0
列表2. MovieSearchDataPools例子
]-g5h:^8X!c:]1cn0    

6FTKR |J0

public class MovieSearchDataPools {
xz g LFR ]5Bq)w0    static RPTDataPool Titles =
G9I K^Bw!pT0        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieTitles.txt" );
$T#R#vm!YDx1Lu'\0       
+I ` HK8W8u0    static RPTDataPool Directors =51Testing软件测试网eC sX8U;`i3}U
        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieDirectors.txt" );
d9vj"sr4W3D0       51Testing软件测试网:@(D5^S-h qw!BpU
    static RPTDataPool Actors =
In?9v9j.yqN v7A0        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieActors.txt" );
5?~;N%f kfd0    }
51Testing软件测试网c:V-IDt'G-k-m|$U


:_p4N%m:Fx"Vog+F0
   使用自定义代码从一个数据池中取回数据51Testing软件测试网iv/? BQT/J(fAG

   要取回一个元素,在一个自定义代码模块中使用getaDataPoolItem方法。用这个电影搜索的例子来取回一个任意电影的标题名称,如列表3所示。

9rN-c-nF(tP5h1i.`0


G d7O8Vi TI0
列表3:使用getaDataPoolItem方法51Testing软件测试网 Z6cu@ ^ CD]x
    51Testing软件测试网+RFZZ'?
package test;
51Testing软件测试网wI$NZ lIS

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

)[Ta'["qv0

public class GenerateRandomTitle implements51Testing软件测试网.Z/G@zK`
    com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {51Testing软件测试网/l.h?.y%o'NG
   
9I leua Rj{0    /**51Testing软件测试网5X'{0R)Ly k
    * Instances of this will be created using the no-arg constructor.51Testing软件测试网#isXtO
    */
?7w!y:UZ Px0    public GenerateRandomTitle() { } 
~.q+KR)A9v R-g;F ru0   51Testing软件测试网KF/Ws*d)V7U ~
    /**51Testing软件测试网+y&{+T*Z;`&m-t
    * For descrīption of ICustomCode2 and ITestExecutionServices interfaces,51Testing软件测试网T x[9q2Yq V Y
    * see doc located at 
51Testing软件测试网)S3S4^'x8GY7\

/rpt_prod/eclipse/plugins/com.ibm.rational.test.lt.kernel_/pubdoc51Testing软件测试网"A2uw$X4G Gufne
    */
PBc SJ0   51Testing软件测试网$xg YXnD{rY
public String exec(ITestExecutionServices tes, String[] args) {
U&RKNf4_|B0    return MovieSearchDataPools.Titles.getaDataPoolItem();51Testing软件测试网"?9J(H:oT|
    }
1sa:S)H6hT3XdH0   
%EO~!jM0}

G:d5W'SmS6ZTQ0k`0
   你可以使用来自搜索电影的HTTP请求中的自定义代码模块中的输出结果。

2N\ uo%_0

 51Testing软件测试网K3sD(A/Cq4qVp

 

P7}.T `~/zO APS&ebI0

TAG: Rational

 

评分:0

我来说两句

Open Toolbar