深入了解Postprocessor

一、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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-24 03:01
下一篇 2024-12-24 03:01

相關推薦

  • 深入解析Vue3 defineExpose

    Vue 3在開發過程中引入了新的API `defineExpose`。在以前的版本中,我們經常使用 `$attrs` 和` $listeners` 實現父組件與子組件之間的通信,但…

    編程 2025-04-25
  • 深入理解byte轉int

    一、位元組與比特 在討論byte轉int之前,我們需要了解位元組和比特的概念。位元組是計算機存儲單位的一種,通常表示8個比特(bit),即1位元組=8比特。比特是計算機中最小的數據單位,是…

    編程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什麼是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一個內置小部件,它可以監測數據流(Stream)中數據的變…

    編程 2025-04-25
  • 深入探討OpenCV版本

    OpenCV是一個用於計算機視覺應用程序的開源庫。它是由英特爾公司創建的,現已由Willow Garage管理。OpenCV旨在提供一個易於使用的計算機視覺和機器學習基礎架構,以實…

    編程 2025-04-25
  • 深入了解scala-maven-plugin

    一、簡介 Scala-maven-plugin 是一個創造和管理 Scala 項目的maven插件,它可以自動生成基本項目結構、依賴配置、Scala文件等。使用它可以使我們專註於代…

    編程 2025-04-25
  • 深入了解LaTeX的腳註(latexfootnote)

    一、基本介紹 LaTeX作為一種排版軟體,具有各種各樣的功能,其中腳註(footnote)是一個十分重要的功能之一。在LaTeX中,腳註是用命令latexfootnote來實現的。…

    編程 2025-04-25
  • 深入剖析MapStruct未生成實現類問題

    一、MapStruct簡介 MapStruct是一個Java bean映射器,它通過註解和代碼生成來在Java bean之間轉換成本類代碼,實現類型安全,簡單而不失靈活。 作為一個…

    編程 2025-04-25
  • 深入探討馮諾依曼原理

    一、原理概述 馮諾依曼原理,又稱「存儲程序控制原理」,是指計算機的程序和數據都存儲在同一個存儲器中,並且通過一個統一的匯流排來傳輸數據。這個原理的提出,是計算機科學發展中的重大進展,…

    編程 2025-04-25
  • 深入理解Python字元串r

    一、r字元串的基本概念 r字元串(raw字元串)是指在Python中,以字母r為前綴的字元串。r字元串中的反斜杠(\)不會被轉義,而是被當作普通字元處理,這使得r字元串可以非常方便…

    編程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一個程序就是一個模塊,而一個模塊可以引入另一個模塊,這樣就形成了包。包就是有多個模塊組成的一個大模塊,也可以看做是一個文件夾。包可以有效地組織代碼和數據…

    編程 2025-04-25

發表回復

登錄後才能評論