Jmeter 如何将BSF采样器的输出作为输入传递给Beanshell脚本
BSF取样器:Jmeter 如何将BSF采样器的输出作为输入传递给Beanshell脚本,jmeter,Jmeter,BSF取样器: Hi I need to pass the output of BSF sampler as input to the Bean shell pre-processor below are my programs Bean外壳程序: function makeid() { var ts = new Date().getTime(); var digits = 10e10; //
Hi I need to pass the output of BSF sampler as input to the Bean shell pre-processor below are my programs
Bean外壳程序:
function makeid()
{
var ts = new Date().getTime();
var digits = 10e10;
//var timestamp = ts.toString() + Math.floor(Math.random() *digits ).toString();
var timestamp = new Date().getTime().toString() + Math.floor(Math.random() *digits ).toString();
return timestamp;
}
function test()
{
var uniqueId=makeid();
var numCopies = 20,status = 'done printing',timestampDonePrintingAttr = "timestamp done printing",Title='demo.jpg',Username='keshavka'
date='1429036296',printstatus='OK',time='1429036296';
var joblog = {};
joblog[uniqueId] = {};
joblog[uniqueId]['NumCopies'] = numCopies;
joblog[uniqueId]['Status'] = status;
joblog[uniqueId]['Title'] = Title;
joblog[uniqueId]['Username'] = Username;
joblog[uniqueId]['date'] = date;
joblog[uniqueId]['print status'] = printstatus;
joblog[uniqueId]['time'] = time;
joblog[uniqueId]['timestampDonePrintingAttr'] = new Date().getTime();
//console.log(i);
var json = JSON.stringify(joblog);
return json;
}
BSF sampler给了我一个JSON,我需要使用BeanShell脚本创建一个文件并将内容写入其中。
请帮助我解决这个问题。根据BSF采样器和Beanshell预处理器所在的位置,方法可能会有所不同 如果Beanshell预处理器是BSF Sampler的子级,那么它将不起作用,因为预处理器是在Sampler之前执行的 对于其他情况: 如果查看“脚本”输入的顶部,您将看到一些预定义的变量名称,如
ctx
、vars
、props
,等等
例如,vars
代表类实例,所以您可以在BSF采样器中设置变量,并在Beanshell预处理器中访问它,如下所示:
在BSF中:
FileWriter fstream = new FileWriter("C:\\apache-jmeter-2.13\\detail_log7.txt",false);
BufferedWriter out = new BufferedWriter(fstream);
out.write(${test});
out.close();
在比恩希尔:
vars.put('json', json);
如果上述方法无法帮助用测试计划的屏幕截图更新问题
注意:
- 您不需要仅仅为了将JSON写入文件而切换到Beanshell。请参阅指南,了解如何从JavaScript访问Java类,反之亦然
- 指南中详细介绍了预定义变量,如ctx、VAR、props等李>
- JavaScript和Beanshell都有性能问题,因此如果此代码使用超过10个线程执行,建议在需要时用于编写脚本李>
out.write(vars.get("json"));