一、环境准备
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
}
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理