新建LR的JAVA Vuser脚本,这里可以直接进行JAVA编码,又可以调用LR的内部方法,如事务、思考时间、集合点等等。到了这步已经没有任何难点了,开发人员只要花1个小时了解下LR的基本使用和常用方法即可,测试人员如果不会JAVA……那还是算了吧。

  Action.java内容如下:

import lrapi.lr;
 import com.test.*;
 import java.util.*;
 import java.io.*;
 
 public class Actions
 {
 
     public int init() throws Throwable {
         return 0;
     }//end of init
 
 
     public int action() throws Throwable {
       
         /*
         调用者测量
         */
         int downloadSize = 0;
         int downloadTime = 0;
         long startTime = 0;
         long endTime = 0;
         int speed = 0;
 
         int vid;
         vid = lr.get_vuser_id();
 
         /*
         从url文件生成arraylist
             */
         String url;
         ArrayList urlList = new ArrayList();
 
         try{      
                 BufferedReader br = new BufferedReader(new FileReader("url.txt"));
 
              while((url = br.readLine()) != null){
                urlList.add(url);
         }
         }catch(IOException ie){
           ie.printStackTrace();
         }
 
         lr.enable_redirection(true);
         lr.set_debug_message(lr.MSG_CLASS_JIT_LOG_ON_ERROR, lr.SWITCH_OFF);
 
         System.out.println("Total URLs: " + urlList.size());
 
         for(int i = 0; i < urlList.size(); i++){
           url = (String)urlList.get(i);
           System.out.println(url);
 
           //事务名称
           String trxName = "URL" + (i+1);
 
           startTime = System.currentTimeMillis();
 
           lr.start_transaction(trxName);
           //传入url和每个调用者的标识
           downloadSize = DownloadFile.getHttpFileByUrl(url, Integer.toString(vid));
           lr.end_transaction(trxName, lr.AUTO);
 
           endTime = System.currentTimeMillis();
           downloadTime = (int)(endTime - startTime)/1000;
           speed = downloadSize / downloadTime;
 
           lr.output_message(trxName + ": completed");
           lr.output_message("time cost: " + downloadTime + "s");
           lr.output_message("average speed: " + speed + "KB/s");
           lr.output_message("");
 
         }
 
         lr.set_debug_message(lr.MSG_CLASS_JIT_LOG_ON_ERROR, lr.SWITCH_ON);
 
         return 0;
     }//end of action
 
 
     public int end() throws Throwable {
         return 0;
     }//end of end
 }