不要追求绝对的公平,红尘之中没有公平而言,人活一世,难得糊涂。                                           it is no use doing what you like, you have got to like what you do.

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

上一篇 / 下一篇  2006-12-08 09:05:39 / 精华(2) / 置顶(2) / 个人分类:软件测试技术

使用自定义代码对数据池进行随机访问51Testing软件测试网#n,\&?-Soj'g9sFA*p
51Testing软件测试网#F/p&p?n+\2H.W
[关键字]代码 数据池51Testing软件测试网o0fNha"LTHkA
    为了更加稳健的测试,可以用自定义代码使 Performance Tester 随机化。目前,Performance Tester 数据池中的元素只能按照顺序进行访问。这篇文章讨论了作者如何创建自定义的 Performance Tester 代码,并用随机数据访问来实现数据池。这篇文章提供的了 RPTDataPool 类,以及如何实现它的详细说明。
Sm)gmbjq051Testing软件测试网ON@CgC ~&g#gF
    注意:这篇文章适用于 IBM® Rational® Performance Tester Version 6.1.2.00251Testing软件测试网C QDo#g9\ w
51Testing软件测试网2xp{5Q^/]s$F
     好的测试不仅仅是重复执行相同的动作。为了更好的模拟实际用户的动作,测试人员可以利用IBM Rational Performance Tester 中的数据池。这是远超出使用静态记录测试的一步,因为它允许测试为每个执行来选择不同的变量。例如,无论什么时候应用软件要求用户输入搜索条件,比如一个电影的名字,一个标题或者标题的一个部分都可以从数据池中选择。
!sv8Vs QV m,m8w051Testing软件测试网O:Q!Z1lr:@] ~k;k
    目前Rational Performance Tester数据池仅仅是顺序存储。在这个电影搜索的例子中,每次你运行这个测试时,相同电影名称的搜寻都是以相同的顺序来进行处理的。通过对要搜索的标题进行随机选择,可以提高测试的稳健性。51Testing软件测试网9qp Ag%e`a7@

yN.QG&y Y1V0    数据池文本文件
0]q.d0H*r!T4V0
'{ `(c U|1`7Ki0    在i5/OS系统测试环境中,IBM测试自动化小组自从2004年就一直在将Mercury LoadRunner的脚本转换到 Rational Performance Tester。为了随机化i5/OS测试的变量选择,我们创建了一个 Rational Performance Tester自定义代码的包,来对数据池中的元素进行随机存储。这个执行一点都没有用到 Rational Performance Tester数据池的特性。相反,Rational Performance Tester DataPool 类读取的文本文件中包含要使用的数据池条目。51Testing软件测试网.D p)_[5u
51Testing软件测试网'V1Bf#ac?(W ZE&I
     这个将选择元素随机化的数据池文件是一个每行仅包含一个元素的纯文本文件。用Rational Performance Tester来实现它的一个简单的方法,就是为测试项目的文本文件创建一个数据池文件夹。一个文件输出包括这些文本文件,因为它们被包含在 Rational Performance Tester项目中。当转换Mercury LoadRunner脚本时,你可以通过LoadRunner数据文件来实现。51Testing软件测试网`f2JTy8NV

R!ZCk!vT S'j0RPTDataPool类
*fs"Y*f1e/U0
%uRi'e [ @0    这个文本数据的文件名传给创建者,整个文件在首次访问尝试时就被读取进入了 Java™ Vector 对象。为了从这个数据池中随机重新找到一个条目,可以使用getaDataPoolItem 方法。(参见列表1。)
0ky L Ub R0
0BvMIHi4T3p'v0注意事项51Testing软件测试网f.? B.x l;i
51Testing软件测试网Ql~ cdg9~!@
    记住整个文件在测试的开始就已被读入存储器是十分重要的。巨大的数据池将会用到大量的内存,这将会降低加载和Rational Performance启动的速度。巨大的Rational Performance Tester测试数据池也会发生类似的情况。
+} L n}x+KY8Hd3e0
iI iSwW0    你可以使用每行包含多个元素的数据池,但是用户必须在这个测试的自定义代码中增加一些功能来取出单个元素。
:PQ7x3E*V'_#NO&j051Testing软件测试网my(ZtluKT

w*O LQ ]8iu0    列表1. getaDataPoolItem 方法
q&e+Y+u0i-t0   51Testing软件测试网;X:O]TWCfT
import .io.*;51Testing软件测试网&SE.Gm&a'N%a
import .util.Vector;51Testing软件测试网1E5v`5VnO%}4K
public class RPTDataPool {
"D6mQ q'T5B#E0    private boolean DataPoolIsLoaded = false;51Testing软件测试网5X(|;O,~'aY8lDs
    private String DataPoolFileName;
I9a{ D R8xf } [ jU0    private Vector DataPool;
btC hf i0p0    private int DataPoolCount = 0;
)BQ,IO\6odXz0 51Testing软件测试网r5s1[-j m
    public RPTDataPool( String fileName )  {
O rW^D#}Tdj0        DataPoolFileName = fileName;
@)E+}1Q Kv#]0        DataPool = new Vector();
.O ~ ~9C7C&Y*u2q8\z0        DataPoolCount = fillVector( DataPoolFileName, DataPool);
tI kd3af9[@R0        DataPoolIsLoaded = true;
;uA VUhe0v;Z)k0        }51Testing软件测试网Wib:i$a9O"r.u_
 51Testing软件测试网 e3XO}&@S3z
    public String getaDataPoolItem(  )  {
QT%v*eli4LL0        if( !DataPoolIsLoaded ) {51Testing软件测试网*G I m&}-P0}
            //System.out.println("loading:" + DataPoolFileName);
/}-IL}$c\8^%w2DM0            DataPoolIsLoaded = true;51Testing软件测试网n |k"m1]1r"W-?e
        }
3Hu7b^1i_*{d0        return (String) DataPool.elementAt((int)
.d+I|L8L!y0          (Math.floor(Math.random() * (DataPoolCount))));
nUYLDn/h0    }51Testing软件测试网pH:pDQJX Z
   
3T7}!{^SxFnh0    private int fillVector( String fileName, Vector FileLines) {51Testing软件测试网u$B$Zs7A:Y:H
        // take the Datapool file and read it into a Vector
#r#[ dtg:A0x1k-i1[0        // do this only once per test51Testing软件测试网4s'JB0{BX1D
        int fileLineCounter = 0;51Testing软件测试网:f"k#ms,Z;p R;p f
       51Testing软件测试网w3ht?"W N TS
        BufferedReader brInReader = null;
e7U6y.h f%qa0       51Testing软件测试网T8Rdy4RS
        // read from the file
3a J3V5yo8j XQ+b n0        // try to setup a buffered reader and open the file
3d O4Z1m1]Y8iX rdW(^0        try  { brInReader = new BufferedReader(new FileReader(fileName));}51Testing软件测试网W8O.F&p0flr
        catch(Exception error)
[0l2d8S&P:L$r0        {
pkOK U]Y;g.y X6U0      //System.out.println("Error Opening DataPool File: " + error);
7^7`|w+@,e9}1?0      return 0;51Testing软件测试网l ~"^ cI+r,I:VJU
  }
J&R!^9E(T9M5i0 
3{ ^S @:e4T%rof yc0  // read the file and place the lines in an array
,b/eas,a/GV6h0  // get the first line of the file51Testing软件测试网$d|%v-~ qU
  String sInLine = ReadLine(brInReader);
uWr*DF#E0 
~uFepiJ0  // read through the file51Testing软件测试网xbd/b$N
  while (sInLine != null)51Testing软件测试网WLB/d EL8X1k'|*K
  {
m A{#j$oq/t%LV0      //System.out.println("Storing ’"+ sInLine+"’");51Testing软件测试网,C3^.w1}+ekU0B
      FileLines.addElement(sInLine);51Testing软件测试网S~FX)`w1Bo l
     51Testing软件测试网E E'I'w*g1p
      // read the next line
*U/] ff_8L0      sInLine = ReadLine(brInReader);
;m_;m _]0     51Testing软件测试网2~0^hP|]
      fileLineCounter++;51Testing软件测试网zzj1AWOz`;| |
  }
}u@I7k)Ac0 51Testing软件测试网?$H#r5x[/W8?*Q
  // At this point, the FileLines Vector has all the lines51Testing软件测试网R.d6b)f%j_TZ*{X
  from the file and fileLineCounter
!c5S0U0oeT0 
'L3Q3pk$[%nTSi0  // indicates the max index value for the array51Testing软件测试网?iy G_*b }
  return fileLineCounter;
g|q3Td0h.b0  }
J&w"L.A4f#CJ!IB0 
kU#EG\0  // ReadLine51Testing软件测试网.F%ewZ'^ p^ Q
  // This method will read a line from a given file and return the string value
~J_bvTDex0  // ********************************************************************************
4Tt\BG.z8G%^ V0 
JgH,tFSH0  private String ReadLine(BufferedReader brReader) {51Testing软件测试网8w;k.}.k9O O
      String sReadLine = "";
-u9U"y.|~0      try  {  sReadLine = brReader.readLine();}51Testing软件测试网2^"ep-b8l-Q
      catch (IOException error)51Testing软件测试网J(e\!V!}#I
      {//System.out.println("DataPool.test Read Error: " + error);}
C-_`)j;Kp?S}0     
Z6wS@!v]#D0      return sReadLine;
l zS&uXu#NQ,D0  }51Testing软件测试网Jo'P#`-P-Pc2d
  // end of Read51Testing软件测试网3\-GG:a @ g._7K2P9j
  }51Testing软件测试网y`M3ZE8o

O"^1y`/}U0
|M@e{0    在测试中实例化数据池对象51Testing软件测试网US0I }]

teIR4j{!_E$bx0    为Rational Performance Tester测试项目创建一个特殊的类来实例化这个数据池的对象。这里,用MovieSearchDataPools来举例说明,这个电影搜索项目需要三个不同的数据池(请看列表2)51Testing软件测试网$_~z YM|:J0_a Rd
51Testing软件测试网)[{F U$PC A)N
51Testing软件测试网] k@ ar)Y A
列表 2. MovieSearchDataPools 例子
^2Ivd@5^0   51Testing软件测试网%tsbX iB:m0Q
51Testing软件测试网,BW |uHqhF$e
public class MovieSearchDataPools {
#IKcU%V0{.S0    static RPTDataPool Titles =
-F1M4`Xb0        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieTitles.txt" );51Testing软件测试网%V~rO#Fx
       
8lRO[@QK7K0    static RPTDataPool Directors =
Aw ei+q)DY-L0        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieDirectors.txt" );
&OOW_%h_;kh Go0       51Testing软件测试网E6qc FQt0O,L'z
    static RPTDataPool Actors =
^ J1`0]eSj0        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieActors.txt" );51Testing软件测试网3YF``PK[]
    }51Testing软件测试网*y ?kZ&]v
51Testing软件测试网I-|PxP-E(WhLv'j
51Testing软件测试网 p!C%k#~ {%d:[w#O'B
    使用自定义代码从一个数据池中取回数据
_2| XW$`W)LP0
?3u8A8]$Z3e;m*\0    要取回一个元素,在一个自定义代码模块中使用getaDataPoolItem方法。用这个电影搜索的例子来取回一个任意电影的标题名称,如列表3所示。
;p,]T~&J"F&U0
Mut+z$k0
4K5f#@bn;m0列表 3: 使用 getaDataPoolItem 方法51Testing软件测试网 y'U,I Nbz0rV9P
   51Testing软件测试网 n n)A$Q6BT't QiM:N
package test;
$V:XtV_)O Wy*W8P2]0
a%mZ#h^#U0import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
G$e+jSp3qUgt1A051Testing软件测试网4L~9OF9aq4| I
public class GenerateRandomTitle implements
H&U(Vy4mG*SL k+r0    com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {
$p bN-V fl6D"W0   51Testing软件测试网{.S m b3N,C
    /**
i"i h FoK0    * Instances of this will be created using the no-arg constructor.
/^v5Eo-g0    */
D4f`J7t8^:Smv0    public GenerateRandomTitle() { }
j nMQM0   51Testing软件测试网X7g5mC*E^2B8L
    /**51Testing软件测试网+q5u nbHC2v x0o%d
    * For descrīption of ICustomCode2 and ITestExecutionServices interfaces,51Testing软件测试网.c!Y,x2wmHJ
    * see doc located at 51Testing软件测试网"r+T6{?F*DCk:?'Y"s

q"`!@$jL3]9Y0/rpt_prod/eclipse/plugins/com.ibm.rational.test.lt.kernel_/pubdoc51Testing软件测试网'? g'E s2TF
    */51Testing软件测试网%rb3O(n2^
   51Testing软件测试网!@yk R8?D8K| @(K;e
public String exec(ITestExecutionServices tes, String[] args) {
-M(@hK&z0    return MovieSearchDataPools.Titles.getaDataPoolItem();51Testing软件测试网;Xn4hVF1W
    }
l8F%?-jL0   51Testing软件测试网L-s5i(tyyT p
}
3Y.O3TD%| ?051Testing软件测试网*O#r cAA7T-^8x6\-^
51Testing软件测试网5n"n Ep,_q*C
    你可以使用来自搜索电影的HTTP请求中的自定义代码模块中的输出结果。51Testing软件测试网Nh/dn V!uf/D H

N!d(A,}#yc0

TAG: 数据池 datapool 随机化

 

评分:0

我来说两句

Open Toolbar