使用JMeter测试加载文档功能 [英] Testing load document functionality with JMeter
问题描述
我正在测试具有加载文档"功能的服务.我需要随每个请求发送一个唯一的文档.在HTTP请求采样器配置菜单中,我看到可以将文档与请求一起发送.但是,我对如何在每次请求中发送不同的文档一无所知.有没有办法让JMeter稍微修改文档,生成指定的文档,或者甚至选择一系列外部生成的文档以根据请求提交?
I am testing a service that has "load document" functionality. I need to send a unique document with every request. In the HTTP request sampler config menu I see that I can send a document along with my request. However, I don't have a clue as to how I am going to send a different document with every request. Is there a way to get JMeter to either slightly modify a document, generate a specified document, or even perhaps select a series of externally generated documents for submission with a request?
推荐答案
您可以使用While Controller下的CSV数据集配置来循环读取和发送预先创建的测试文档名称.
You may use CSV Data Set Config under While Controller to read and send pre-created test documents names in cycle.
这看起来像:
- 创建不同测试文档的集合以与您的请求一起发送;
可选:将具有创建文档的文件夹的路径存储为jmeter变量-在脚本中使用;
- 创建测试文档列表;
您可以使用如下所示的代码在BeanShell Sampler中执行此操作;
- 添加While Controller以循环发送测试文档;
CSV数据集配置作为While Controller的子级-从列表中读取测试文档的名称.
- create collection of different test-documents to send with your request;
OPTIONALLY: store path to folder with created documents as jmeter variable - to use in script; - create list of test-documents;
you can do this in BeanShell Sampler with code as shown below; - add While Controller to send test-documents in cycle;
CSV Data Set Config to While Controller as child - to read test-documents names from list.
详细信息:
${__javaScript("${testFile}"!="<EOF>",)}
-读取列表直到文件末尾
${__javaScript("${testFile}"!="<EOF>",)}
- to read list till the end of file
BeanShell Sampler代码生成测试文件列表:
BeanShell Sampler code to generate test-files list:
import java.text.*;
import java.io.*;
import java.util.*;
String [] params = Parameters.split(",");
String contentList = params[0];
String testDataDir = params[1];
File dir = new File(System.getProperty("user.dir") + File.separator + testDataDir);
BufferedWriter out = null;
try {
if (!dir.exists()) {
throw new Exception ("Directory " + dir.getName() + " not found.");
}
File contentFile = new File(System.getProperty("user.dir") + File.separator + contentList);
if (contentFile.exists()) {
contentFile.delete();
}
FileWriter fw = new FileWriter(contentFile, true);
out = new BufferedWriter(fw);
System.out.println("\n--------------------------------------------------------------------------------------");
System.out.println("CONTENT LIST:\n");
if ((dir.exists()) && (dir.listFiles() != null) && (out != null)) {
for (File f : dir.listFiles()) {
if (contentFile.length() == 0) {
out.write(f.getName());
} else {
out.write("\n" + f.getName());
}
out.flush();
System.out.println("Content " + f.getName() + " added to " + contentFile.getName() + ".");
}
}
System.out.println("--------------------------------------------------------------------------------------\n");
}
catch (Exception ex) {
IsSuccess = false;
log.error(ex.getMessage());
System.err.println(ex.getMessage());
}
catch (Throwable thex) {
System.err.println(thex.getMessage());
}
finally {
out.close();
}
这篇关于使用JMeter测试加载文档功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!