场景:需要直观的查看请求的响应时间

在statistics模块里面能够看到某个请求的具体响应时间


但是这样显得不直观

通过在网上查阅资料得到, 往script里面添加一段代码即可直观查看


public static BindUIColumn("耗时/s", 120)
function TimeTaken(oS: Session):String{
var sResult = "0";
var t1_h = oS.Timers.ClientDoneResponse.Hour;
var t1_m = oS.Timers.ClientBeginResponse.ToUniversalTime().Minute;
var t1_s = oS.Timers.ClientBeginResponse.ToUniversalTime().Second;
var t1_ms = oS.Timers.ClientBeginResponse.ToUniversalTime().Millisecond;
var t1 = t1_m*60*1000 + t1_s*1000 + t1_ms ;
var t2_m = oS.Timers.ClientDoneRequest.ToUniversalTime().Minute;
var t2_s = oS.Timers.ClientDoneRequest.ToUniversalTime().Second;
var t2_ms = oS.Timers.ClientDoneRequest.ToUniversalTime().Millisecond;
var t2 = t2_m*60*1000 + t2_s*1000 + t2_ms ;

if(t1 >= t2){
var t3 = (t1 - t2)/1000;
sResult = t3.toString();

//>5秒的
if (t3>3) {
var riqi=new Date(oS.Timers.ClientDoneResponse.Date).toLocaleDateString();

var fso;
var file;
fso = new ActiveXObject("Scripting.FileSystemObject");
//文件保存路径,可自定义
file = fso.OpenTextFile("d:\chaoshi"+riqi+".txt",8 ,true, true);
file.writeLine(t1_h +":" +t1_m +":" +t1_s +" " + oS.url+" duration: " + sResult);
file.close();
}
}
return sResult;
}

把代码添加好以后保存,这样就可以在左侧列表直接看到响应时间了,效果如下图所示: