一、Postprocessor概述
Postprocessor是JMeter中的一个组件,它是在测试执行完成后,对JMeter运行结果进行处理的模块。它的作用是将运行结果处理、分析,生成各种报告或者其他的结果输出。
在JMeter的测试中,通常需要对采集到的数据进行处理、分析。Postprocessor的作用就是对测试数据进行处理,让我们能够对数据有更深入的认识,进而对测试结果进行分析和优化。
二、Postprocessor的类型
JMeter中的Postprocessor包括以下几个类型:
- 正则表达式提取器(Regular Expression Extractor)
- Json提取器(JSON Extractor)
- CSS/JQuery提取器(CSS/JQuery Extractor)
- Beanshell Postprocessor
- JSR223 Postprocessor
每个类型都有其特定的用途和使用场景。正则表达式提取器主要用来提取HTML代码中的某些特定内容;Json提取器适用于处理Json格式的数据;CSS/JQuery提取器专门用来抓取HTML页面中对应的属性信息等等。
在使用Postprocessor时,需要根据实际的测试场景选择合适的类型并特别注意参数设置。
三、正则表达式提取器的使用
正则表达式提取器是最常用的一种Postprocessor。该工具通常用来处理从服务器返回的HTML页面内容中的数据,如抽取COOKIE、页面TOKEN等项。
1. 配置方式
在JMeter中,添加正则表达式提取器的方法是: 在请求之后添加“正则表达式提取器”。
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP请求" enabled="true"> <elementProp name="HTTPsampler.Files" elementtype="HTTPFileArgs" guiclass="HTTPFileArgsPanel" testclass="HTTPsampler.Files" enabled="false"> ... </elementProp> <stringProp name="HTTPSampler.path"/> </HTTPSamplerProxy> <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="正则表达式提取器" enabled="true"> <stringProp name="RegexExtractor.useHeaders"/> <stringProp name="RegexExtractor.refname"/> <stringProp name="RegexExtractor.regex"/> <stringProp name="RegexExtractor.template"/> <stringProp name="RegexExtractor.default"/> <stringProp name="RegexExtractor.match_number"/> </RegexExtractor>
2. 参数设置
参数设置如下:
- refname:变量名,即保存结果的变量名称,需要和后续的操作匹配。
- regex:正则表达式,用于匹配需要提取的内容。
- template:提取内容的模板,可以使用$1$等特殊符号。
下面是一个例子:
<RegexExtractor testname="正则表达式提取器" guiclass="RegexExtractorGui" enabled="true"> <stringProp name="RegexExtractor.regex">$token ='(.*?)';</stringProp> <stringProp name="RegexExtractor.template">$1$</stringProp> <stringProp name="RegexExtractor.default">NOT_FOUND</stringProp> <stringProp name="RegexExtractor.match_number">1</stringProp> <stringProp name="RegexExtractor.refname">TOKEN</stringProp> </RegexExtractor>
四、Json提取器的使用
Json提取器主要用于处理Json格式的数据。使用该工具可以提取Json格式数据中的某些字段,在测试时可以调用这些字段以实现对服务器数据的操作。
1. 配置方式
Json提取器的配置方法和正则表达式提取器类似,在请求之后添加Json提取器。
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP请求" enabled="true"> <elementProp name="HTTPsampler.Files" elementtype="HTTPFileArgs" ... </elementProp> <stringProp name="HTTPSampler.path"/> </HTTPSamplerProxy> <JSONExtractor guiclass="JSONPostProcessorGui" testclass="JSONExtractor" testname="JSON提取器" enabled="true"> <stringProp name="JSONExtractor.referenceNames"/> <stringProp name="JSONExtractor.jsonPathExpressions"/> <stringProp name="JSONExtractor.defaultValues"/> <stringProp name="JSONExtractor.match_numbers"/> <stringProp name="JSONExtractor.compute_concat"/> <stringProp name="JSONExtractor.compute_merge"/> <stringProp name="JSONExtractor.compute_regex"/> </JSONExtractor>
2. 参数设置
参数设置如下:
- referenceNames:变量名,即保存结果的变量名称,需要和后续的操作匹配。
- jsonPathExpressions:Json表达式,用于匹配需要提取的字段。
- defaultValues:如果未匹配到指定字段,则使用默认值。
- compute_concat、compute_merge、compute_regex:三种计算方式,用来处理匹配到的多个结果。
- match_numbers:表示最多匹配多少个结果。
下面是一个例子:
<JSONPostProcessor clearEachIteration="true" guiclass="JSONPostProcessorGui" testclass="JSONPostProcessor" testname="JSON提取器" enabled="true"> <stringProp name="JSONPostProcessor.referenceNames">token</stringProp> <stringProp name="JSONPostProcessor.jsonPathExprs">$..token</stringProp> <stringProp name="JSONPostProcessor.defaultValues">NOT_FOUND</stringProp> <stringProp name="JSONPostProcessor.compute_concat">false</stringProp> <stringProp name="JSONPostProcessor.compute_merge">false</stringProp> <stringProp name="JSONPostProcessor.compute_regex">false</stringProp> </JSONPostProcessor>
五、Beanshell Postprocessor的使用
Beanshell Postprocessor是一种高级Postprocessor,在JMeter的测试中占有重要的地位。Beanshell Postprocessor可以在测试过程中对采集到的数据进行处理,从而实现更为高级的功能。
1. 配置方式
在测试请求后添加Beanshell Postprocessor。
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP请求" enabled="true"> <elementProp name="HTTPsampler.Files" elementtype="HTTPFileArgs" ... </elementProp> <stringProp name="HTTPSampler.path"/> </HTTPSamplerProxy> <BeanShellPostProcessor guiclass="TestBeanGUI" testclass="BeanShellPostProcessor" testname="Beanshell Postprocessor" enabled="true"> <stringProp name="script">log.info(vars.get("var1"));</stringProp> </BeanShellPostProcessor>
2. 参数设置
参数设置如下:
- script:Beanshell脚本,用于处理测试结果数据。
下面是一个例子:
<BeanShellPostProcessor guiclass="TestBeanGUI" testclass="BeanShellPostProcessor" testname="Beanshell Postprocessor" enabled="true"> <stringProp name="script"> import java.text.*; double price = Double.parseDouble(vars.get("Price")); int qty = Integer.parseInt(vars.get("Quantity")); double sum = price * qty; DecimalFormat df = new DecimalFormat("#.##"); String total = df.format(sum); vars.put("Total", total); log.info("Total: " + total); </stringProp> </BeanShellPostProcessor>
六、JSR223 Postprocessor的使用
JSR223 Postprocessor是JMeter中的一个高级Postprocessor。它支持多种编程语言,如groovy、javasript等。JSR223 Postprocessor可以用于更复杂的数据处理操作,提供更大的自由度。
1. 配置方式
在测试请求后添加JSR223 Postprocessor。
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP请求" enabled="true"> <elementProp name="HTTPsampler.Files" elementtype="HTTPFileArgs" ... </elementProp> <stringProp name="HTTPSampler.path"/> </HTTPSamplerProxy> <JSR223PostProcessor guiclass="TestBeanGUI" testclass="JSR223PostProcessor" testname="JSR223 Postprocessor" enabled="true"> <stringProp name="scriptLanguage">groovy</stringProp> <stringProp name="cacheKey"></stringProp> <stringProp name="filename"></stringProp> <stringProp name="parameters"></stringProp> <stringProp name="script">log.info(vars.get("var1"));</stringProp> </JSR223PostProcessor>
2. 参数设置
参数设置如下:
- scriptLanguage:脚本语言。
- script:脚本代码。
下面是一个例子:
<JSR223PostProcessor guiclass="TestBeanGUI" testclass="JSR223PostProcessor"
testname="JSR223 Postprocessor" enabled="true">
<stringProp name="cacheKey"></stringProp>
<原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/288970.html