loadrunner之java user脚本开发

时间:2024-10-15 08:05:25

脚本开发环境:

loadrunner11.0

jdk1.6.32_x86_32

脚本开发

1.选择JavaVuser协议

loadrunner之java user脚本开发

2.配置java环境(Vuser--RunTime Settings)

loadrunner之java user脚本开发

3.开发javavuser脚本

/*
 * LoadRunner Java script. (Build: _build_number_)
 *
 * Script Description: JavaVuser访问页面
 *
 * creator:jeffsui
 *
 * Create Time:2015-09-28
 *                    
 */
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import lrapi.lr;
public class Actions
{
    public int init() throws Throwable {
        return 0;
    }//end of init
    public int action() throws Throwable {
            /***访问首页事务*/
        lr.start_transaction("访问poptest首页");
        String serverUrl="http://www.poptest.cn/";//请求页面url路径
        OutputStreamWriter out =null;
        try {
        URL url =new URL(serverUrl);
        URLConnection conn = url.openConnection();.//建立连接
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type","UTF-8");//设置请求字符编码
        out=new OutputStreamWriter(conn.getOutputStream(),"UTF-8");//设置回应字符编码
        out.flush();
        out.close();
        InputStream in =conn.getInputStream();
        byte[] buffer= new byte[in.available()];
        in.read(buffer);
        System.out.println(new String(buffer));
        catch (Exception e  ) {
            e.printStackTrace();
        }finally{
                if(out!=null){
                    try {
                        out.close();
                    catch (Exception e  ) {
                            e.printStackTrace();
                        }
                }
        }
             
        lr.end_transaction("访问poptest首页", lr.AUTO);
        return 0;
    }//end of action
    public int end() throws Throwable {
        return 0;
    }//end of end
}
4.编译
5.执行脚本
poptest经验分享:

(1)loadrunner11只支持32位的jdk

(2)RuntimeSettings里只是加载了基本jdk,如果需要加载其他第三方的jar包请在JavaFunction中加载

(3)JavaVuser的执行效率取决于你的Java代码。