码迷,mamicode.com
首页 > 编程语言 > 详细

JavaVuser脚本开发-环境搭建

时间:2014-11-11 19:24:35      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:des   http   io   ar   os   使用   java   sp   on   

众所周知,loadrunner可以使用多种协议进行性能测试。这里不得不提到JavaVuser协议。

你是否已经厌烦了在loadrunner脚本开发中,使用各种c函数进行复杂的字符串拼接,解析报文?

那么为什么不使用loadrunner提供的JavaVuser协议开发基于java的脚本呢。

1.前提

可以直接使用Java提供的逻辑代码的场景。

2.环境

loadrunner11.0

jdk1.6.32_x86_32

3.新建脚本

选择JavaVuser协议

bubuko.com,布布扣

4.设置Java环境

Vuser--RunTime Settings

bubuko.com,布布扣

5.编写JavaVuser脚本

下面这段脚本主要是模拟通过url路径访问一个应用的页面,返回首页内容的操作。


/*
 * LoadRunner Java script. (Build: _build_number_)
 * 
 * Script Description: JavaVuser访问页面
 *
 * creator:jeffsui
 *
 * Create Time:2014-04-15
 *                     
 */

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("访问GitBucket首页");

	    String serverUrl="http://localhost:8026/gitbucket";//请求页面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("访问GitBucket首页", lr.AUTO);

		return 0;
	}//end of action


	public int end() throws Throwable {
		return 0;
	}//end of end
}


6.编译


bubuko.com,布布扣

编译通过


No errors detected




7.执行脚本


Starting iteration 1.
Starting action Actions.
Notify: Transaction "访问GitBucket首页" started.
//首页内容省略
....
Notify: Transaction "访问GitBucket首页" ended with "Pass" status (Duration: 4.9907).

8.遇到问题

(1)目前loadrunner11只支持32位的jdk,切记

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

(3)JavaVuser的执行效率取决于你的Java代码,尽量减少loadrunner对他的干预。





JavaVuser脚本开发-环境搭建

标签:des   http   io   ar   os   使用   java   sp   on   

原文地址:http://my.oschina.net/dlpinghailinfeng/blog/343145

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!