一、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/zh-tw/n/288970.html