如何从JMeter中的文件中获取随机行 [英] How to get random row from file in JMeter

查看:121
本文介绍了如何从JMeter中的文件中获取随机行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何从JMeter中的文件中获取随机行。
我会很感激任何建议。

解决方案

不确定关于groovy,也许有一个更简单的方法,但例如,您可以使用以下代码通过 Beanshell Sampler 执行此操作:

  import org.apache.commons.io.FileUtils; //需要导入

列表行= FileUtils.readLines(新建文件(/ path / to / your / file)); //将文件读入行数组
int random = new Random()。nextInt(lines.size()); //获得随机行号
字符串randomLine = lines.get(random); //获得随机行

vars.put(randomLine,randomLine); //将随机行存入$ {randomLine}变量




  • substitute / path / to / your / file 具有相对或绝对路径,您需要从随机文件中获取随机行

  • $ {randomLine} 的形式访问随机行



请参阅如何使用BeanShell:JMeter最喜欢的内置组件指南了解更多信息在JMeter测试中使用来自Beanshell测试元素的JMeter和Java API

NB上面的代码是有效的groovy代码以及

I'm looking for way how to get random row from file in JMeter. I would be appreciate for any suggestions.

解决方案

Not sure regarding groovy, maybe there is an easier way, but for instance you can do it with Beanshell Sampler using the following code:

import org.apache.commons.io.FileUtils; //necessary import

List lines = FileUtils.readLines(new File("/path/to/your/file"));  // read file into lines array   
int random = new Random().nextInt(lines.size()); // get random line number
String randomLine = lines.get(random); // get random line

vars.put("randomLine", randomLine); // store the random line into ${randomLine} variable

  • substitute /path/to/your/file with relative or absolute path to the file you want the random line from
  • you will be able to access the random line as ${randomLine} where required

See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using JMeter and Java APIs from Beanshell test elements in your JMeter test

N.B. The above code is valid groovy code as well

这篇关于如何从JMeter中的文件中获取随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆