关闭

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

发表于:2007-4-17 15:32

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Roger Guderian    来源:IBM

为了更加稳健的测试,可以用自定义代码使 Performance Tester 随机化。目前,Performance Tester 数据池中的元素只能按照顺序进行访问。这篇文章讨论了作者如何创建自定义的 Performance Tester 代码,并用随机数据访问来实现数据池。这篇文章提供的了 RPTDataPool 类,以及如何实现它的详细说明。

    注意:这篇文章适用于 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
  // ********************************************************************************
  
  private String ReadLine(BufferedReader brReader) {
      String sReadLine = "";
      try  {  sReadLine = brReader.readLine();}
      catch (IOException error)
      {//System.out.println("DataPool.test Read Error: " + error);}
     
      return sReadLine;
  }
  // end of Read
  }


    在测试中实例化数据池对象

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


列表 2. MovieSearchDataPools 例子
    

public class MovieSearchDataPools {
    static RPTDataPool Titles =
        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieTitles.txt" );
       
    static RPTDataPool Directors =
        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieDirectors.txt" );
       
    static RPTDataPool Actors =
        new RPTDataPool("C:\\Workloads\\ MovieSearch\\ Datapools\\MovieActors.txt" );
    }


    使用自定义代码从一个数据池中取回数据

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


列表 3: 使用 getaDataPoolItem 方法
    
package test;

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

public class GenerateRandomTitle implements
    com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {
   
    /**
    * Instances of this will be created using the no-arg constructor.
    */
    public GenerateRandomTitle() { } 
   
    /**
    * For description of ICustomCode2 and ITestExecutionServices interfaces,
    * see doc located at 

/rpt_prod/eclipse/plugins/com.ibm.rational.test.lt.kernel_/pubdoc
    */
   
public String exec(ITestExecutionServices tes, String[] args) {
    return MovieSearchDataPools.Titles.getaDataPoolItem();
    }
   
}


    你可以使用来自搜索电影的HTTP请求中的自定义代码模块中的输出结果。

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号