脚本开发-基于HTTP协议的流媒体视频在线播放服务器性能测试
by:授客 QQ:1033553122
目的
实现基于http协议的流媒体在线视频播放,服务器性能测试脚本,模拟用户浏览器方式在线播放视频
开发环境
Loadrunner 11.0
eclipse-standard-kepler-SR2-win32.zip
jdk-6u14-windows-i586
Win7 32
脚本
DownloadVideo.java,代码如下,主要实现流媒体文件的下载及下载相关的分析
package downloadHttpMedia;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
public class DownloadVideo {
public static int getFileByUrl(String address, int vuser_id){
Date date = new Date();
SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMddHHmmss");
String downloadTime = formatDate.format(date);
int totalDownloadSize = 0; //总下载大小
int lastDownloadSize = 0; //上次下载大小
long startDownloadTime = 0; //开始下载
long lastDownloadTime = 0; //上次下载时间
int second = 0; // 记录秒
try{
URL url = new URL(address);
URLConnection conn = url.openConnection();
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
FileOutputStream fos = new FileOutputStream("E:\testvideo" + "_" + downloadTime + vuser_id + ".mp4"); //假设文件为.mp4
int fileTotalSizeInByte = conn.getContentLength();
System.out.println("File Size:" + fileTotalSizeInByte/1024 + "KB"); //获取文件大小
startDownloadTime = System.currentTimeMillis(); //产生一个当前的毫秒 -- 自1970年1月1日0时起的毫秒数
lastDownloadTime = startDownloadTime;
while (totalDownloadSize < fileTotalSizeInByte){
int bufferSize = bis.available(); // 获取数据流中可供读取的字节数
if (bufferSize > 0){
byte[] buf = new byte[bufferSize];
int size = bis.read(buf);
fos.write(buf,0, size); //写入输出流
totalDownloadSize = totalDownloadSize + size;
}
if (System.currentTimeMillis() - lastDownloadTime > 1000){
System.out.println("download speed(KB/s): " + second + " " + (totalDownloadSize - lastDownloadSize)/1024);
lastDownloadTime = System.currentTimeMillis();
lastDownloadSize = totalDownloadSize;
second++;
}
}
System.out.println("Download Completed");
System.out.println("Average download speed: " + (totalDownloadSize/1024) /
((System.currentTimeMillis() - startDownloadTime)/1000) + "KB/s");
fos.close();
bis.close();
}catch(MalformedURLException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return totalDownloadSize/1024;
}
}
说明:如下,函数添加vuser_id参数主要是用于标识文件名称,以防止模拟并发操作时,不同用户下载的视频文件被覆盖,影响分析。
public static int getFileByUrl(String address, int vuser_id){
TestDriver.java,测试程序,如下
package downloadHttpMedia;
public class TestDriver {
public static void main(String[] args)
{
String url = "http://xx.xx-cn-hangzhou.aliyuncs.com/video/VID_20160509_141127.mp4";
DownloadVideo.getFileByUrl(url,1);
}
}
loadrunner中新建java vuser协议的脚本,并将DownloadVideo.java导出jar,添加到lr中,,编写代码如下
附:
1、现成的jar文件下载地址:http://pan.baidu.com/s/1miESwVe
2、关于eclipse导出jar文件参考文章:http://blog.sina.com.cn/s/blog_13cc013b50102v4m7.html
import lrapi.lr;
import downloadMedia.*;
public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
int downloadSize = 0; // 下载文件大小
lr.rendezvous("rendezvous");
lr.start_transaction("downloadMedia");
String url = "";
int size = Integer.valueOf("").intValue();
int vuserid = Integer.valueOf("").intValue();
downloadSize = downloadMedia.getFileByUrl(url,vuserid);
if (downloadSize == size) {
lr.end_transaction("downloadMedia", lr.PASS);
}else{
lr.end_transaction("downloadMedia", lr.FAIL);
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
其中,和参数分别为视频播放http地址,及对应视频大小,为虚拟用户id
运行结果:
如上,显示每秒下载速度及平均下载速度
以下是生成的文件