如何使用每个 jmeter 采样器发送随机数量的 XML 块

问题描述 投票:0回答:1

我有一个带有以下 XML 帖子正文的采样器,

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <GetSoapRequest xmlns="http://www.demandmatrix.net/HBSIXML4/">
      <InterfacePayload>
        <ChannelIdentifierId>Dnata_XML4R</ChannelIdentifierId>
        <Version>2005A</Version>
        <Interface>HBSI XML 4 OTA</Interface>
        <TimeOut>30</TimeOut>
      </InterfacePayload>
      <Message>
        <RequestId>20220728122327zQOxgSf6ZQ</RequestId>
        <Transaction>HotelRateAmountNotifRQ</Transaction>
        <XML>
                    &lt;OTA_HotelRateAmountNotifRQ xmlns="http://www.abcd.org/ORR/2003/05" TimeStamp="2022-07-28T12:23:27+00:00" Target="TEST" Version="1.003" PrimaryLangID="en-us" HotelCode="${HotelCode9}" HotelCityCode="XYZ" HotelName="abc_Hotel"&gt;
                        &lt;POS&gt;
                            &lt;Source&gt;
                                &lt;RequestorID Type="abc" ID="abc"/&gt;
                                &lt;BookingChannel Type="2" Primary="true"&gt;
                                    &lt;CompanyName&gt;abc&lt;/CompanyName&gt;
                                &lt;/BookingChannel&gt;
                            &lt;/Source&gt;
                        &lt;/POS&gt;
                        &lt;RateAmountMessages HotelCode="${HotelCode9}" HotelCityCode="MEL" HotelName="XYZ"&gt;
  &lt;RateAmountMessage LocatorID="1"&gt;
    &lt;StatusApplicationControl Override="1" RatePlanCodeType="RatePlanCode" InvCodeApplication="InvCode" IsRoom="1" Start="${__timeShift(yyyy-MM-dd ,,P${StartDate9}D,,)}" End="${__timeShift(yyyy-MM-dd ,,P${EndDate9}D,,)}" RatePlanCode="${RatePlanCode9}" InvCode="${RoomCode9}" /&gt;
    &lt;Rates&gt;
      &lt;Rate NumberOfUnits="1" RateTimeUnit="Day" UnitMultiplier="1"&gt;
        &lt;BaseByGuestAmts&gt;
          &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${AdultPrice9}" AmountAfterTax="${__jexl2(${AdultPrice9}+(${AdultPrice9}*${Tax9}))}" NumberOfGuests="1" AgeQualifyingCode="10" /&gt;
          &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9}+${AdultPrice9}*2*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="2" AgeQualifyingCode="10" /&gt;
          &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9}+${AdultPrice9}*3*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="3" AgeQualifyingCode="10" /&gt;
          &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9}+${AdultPrice9}*4*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="4" AgeQualifyingCode="10" /&gt;
        &lt;/BaseByGuestAmts&gt;
      &lt;/Rate&gt;
    &lt;/Rates&gt;
  &lt;/RateAmountMessage&gt;
&lt;/RateAmountMessages&gt;
                    &lt;/OTA_HotelRateAmountNotifRQ&gt;
                </XML>
      </Message>
      <Login>
        <LoginType>Component</LoginType>
        <ComponentInfo>
          <ComponentType>Hotel</ComponentType>
          <Id>${HotelCode}</Id>
          <User>abc</User>
          <Pwd>hu@QTUls#*3U2C$Yt0Ni</Pwd>
        </ComponentInfo>
      </Login>
    </GetSoapRequest>
  </soap:Body>
</soap:Envelope>

有两个要求,

  1. 我需要使用每个采样器发送随机数量(从 1 到 50 的任何数量)的消息(消息位于上面的帖子正文中,从第 25 行到第 37 行,即标签名称为“RateAmountMessage”)。例如,1 个请求可以有 5 条消息,下一个请求可以有 10 条消息。
  2. 现在,一旦我们准备好包含不同数量消息的帖子正文(假设有 10 条消息),每条消息都会从 csv 中读取动态值(即 StartDate9、EndDate9、RatePlanCode9、RoomCode9、AdultPrice9、Tax9)。例如,第一条消息将采用 csv 第一行的值,第二条消息将采用 csv 第二行的值,依此类推。

csv 文件如下,

95106,BAR1,Double Deluxe Room,142,162,25.1,5,0.9,0.1
95106,BAR2,Double Deluxe Room,142,162,35.2,10,0.9,0.1
95106,BAR3,Double Deluxe Room,142,162,45.3,15,0.9,0.1
95106,BAR4,Double Deluxe Room,142,162,55.4,20,0.9,0.1
95106,BAR5,Double Deluxe Room,142,162,65.5,25,0.9,0.1
95106,BAR1,Double Room,142,162,75.6,30,0.9,0.1
95106,BAR2,Double Room,142,162,85.7,35,0.9,0.1
95106,BAR3,Double Room,142,162,95.8,40,0.9,0.1

我们尝试在 jsr223 的帮助下执行此操作,但它无法从 csv 中获取值,也无法解析 __jexl2 函数。我使用的是 Jmeter 5.6.3 有人可以帮忙吗!

xml jmeter jmeter-plugins
1个回答
0
投票

您可以添加JSR223预处理器并使用如下代码:

def message = '''
                    &lt;RateAmountMessage LocatorID="1"&gt;
                    &lt;StatusApplicationControl Override="1" RatePlanCodeType="RatePlanCode" InvCodeApplication="InvCode" IsRoom="1" Start="${__timeShift(yyyy-MM-dd ,,P${StartDate9}D,,)}" End="${__timeShift(yyyy-MM-dd ,,P${EndDate9}D,,)}" RatePlanCode="${RatePlanCode9}" InvCode="${RoomCode9}" /&gt;
                    &lt;Rates&gt;
                    &lt;Rate NumberOfUnits="1" RateTimeUnit="Day" UnitMultiplier="1"&gt;
                    &lt;BaseByGuestAmts&gt;
                    &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${AdultPrice9}" AmountAfterTax="${__jexl2(${AdultPrice9}+(${AdultPrice9}*${Tax9}))}" NumberOfGuests="1" AgeQualifyingCode="10" /&gt;
                    &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*2*${PercentIncreasePerGuest9}+${AdultPrice9}*2*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="2" AgeQualifyingCode="10" /&gt;
                    &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*3*${PercentIncreasePerGuest9}+${AdultPrice9}*3*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="3" AgeQualifyingCode="10" /&gt;
                    &lt;BaseByGuestAmt CurrencyCode="USD" AmountBeforeTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9})}" AmountAfterTax="${__jexl2(${AdultPrice9}*4*${PercentIncreasePerGuest9}+${AdultPrice9}*4*${PercentIncreasePerGuest9}*${Tax9})}" NumberOfGuests="4" AgeQualifyingCode="10" /&gt;
                    &lt;/BaseByGuestAmts&gt;
                    &lt;/Rate&gt;
                    &lt;/Rates&gt;
                    &lt;/RateAmountMessage&gt;'''

def randomNumber = org.apache.commons.lang3.RandomUtils.nextInt(1, 51)
def messages = new StringBuilder()

1.upto(randomNumber, { index ->
    def lineFromCSV = new File('/path/to/your/file.csv').readLines().get(index - 1)
    //do what you need with CSV file here
    messages.append(message)
})

vars.put('messages', messages.toString())

然后将帖子正文从第 25 行到第 37 行替换为

${messages}

请参阅 Apache Groovy:Groovy 用于什么? 文章,了解有关 JMeter 中 Groovy 脚本编写的更多信息。

__jexl2() 函数在 JMeter 5.6.3 中工作正常:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.