LR实现流媒体性能测试方法

一、环境准备
1.LoadRunner 11(最高支持Jdk 1.6 32位)
2.Jdk 1.6 32bit
3.Eclipse

二、Eclipse 具体实现代码如下:

package com.appium.test;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Apache 流媒体下载测试Demo
 * @author liutao
 * 循环读取URL,并向URL发出下载Request
 */
public class DownloadFile {

    public static void main(String []args){

        String url;
        ArrayList urlList = new ArrayList();

        /*
        *读取待下载的URL地址
        *保存到list中
        */
        try {
            BufferedReader br =new BufferedReader(new FileReader("url.txt"));
            while((url = br.readLine())!=null){
                urlList.add(url);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Total URLs: " + urlList.size());

        /*
         * 依次读取url信息
         */
        for(int i=0;i<urlList.size();i++){
            url=(String)urlList.get(i);
            System.out.println(url);
            getHttpFileByUrl(url,"1");
        }
    }

    public static int getHttpFileByUrl(String address, String userid) {
        // TODO Auto-generated method stub

        int size=0;
        byte [] buf= new byte[1024];
        String downloadTime=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());

        /*
         * 计算下载速度相关
         */
        int totalDownloadSize = 0;
        int lastDownloadSize = 0;
        long lastCheckTime = 0;
        long startDownloadTime = 0;
        int sec = 0;

        try {
            URL url=new URL(address);
            URLConnection conn=url.openConnection();
            BufferedInputStream bis=new BufferedInputStream(conn.getInputStream());
            FileOutputStream fos=new FileOutputStream("e:\\test\\testvideo" + "_" + downloadTime + "_" + userid);

            System.out.println("File Size:" + conn.getContentLength()/1024 + "KB");
            startDownloadTime = System.currentTimeMillis();
            lastCheckTime = startDownloadTime;

            while((size=bis.read(buf)) !=-1){
                fos.write(buf,0,size);
                totalDownloadSize += size;

                 /*
                 *计算每秒下载速度
                 */
                if (System.currentTimeMillis() - lastCheckTime > 1000) {
                    System.out.println(sec + ": " + (totalDownloadSize - lastDownloadSize)/1024 + "KB");
                    lastCheckTime = System.currentTimeMillis();
                    lastDownloadSize = totalDownloadSize;
                    sec++;
                 }
            }
             System.out.println("Vuser " + userid + " Download Completed!");
             System.out.println("Average Download Speed: " + (totalDownloadSize/1024)/((System.currentTimeMillis() - startDownloadTime)/1000) + "KB/s");
             fos.close();
             bis.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return totalDownloadSize/1024;
    }
}

三、LoadRunner
1.将com包放入LoadRunner脚本目录中
这里写图片描述

2、检查环境变量
这里写图片描述

3.LoadRunner具体实现代码如下:

/*
 * LoadRunner Java script. (Build: _build_number_)
 * 
 * Script Description: 
 *                     
 */

import java.io.*;
import java.util.*;
import com.appium.test.*;
import lrapi.lr;

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 列表参数
        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 e){
        e.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.print(url);

        String trName="URL"+(i+1);

        startTime=System.currentTimeMillis();
        lr.start_transaction(trName);

        //文件大小
        downloadSize=DownloadFile.getHttpFileByUrl(url,Integer.toString(vid));

        lr.end_transaction(trName,lr.AUTO);

        endTime=System.currentTimeMillis();
        downloadTime=(int)(endTime-startTime)/1000;
        speed=downloadSize/downloadTime;

        lr.output_message(trName+":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
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值