【问题标题】:Random number in Lua script Load ImpactLua 脚本中的随机数负载影响
【发布时间】:2015-09-16 15:59:17
【问题描述】:

我正在尝试在 Lua 中创建一个随机数生成器。我发现我可以使用math.random(1,100) 随机化一个介于 1 和 100 之间的数字,这就足够了。

但我不太明白如何在脚本中使用随机数作为变量。

试过了,当然没用。

$randomCorr = math.random(1,100);

http.request_batch({
    {"POST", "https://store.thestore.com/priceAndOrder/selectProduct", headers={["Content-Type"]="application/json;charset=UTF-8"}, data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\"$randomCorr\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}", auto_decompress=true},
    {"GET", "https://store.thestore.com/api/checkout/getproduct?correlationId=$randomCorr", auto_decompress=true},
    })

【问题讨论】:

    标签: random lua httprequest


    【解决方案1】:

    Lua 中,不能以 $ 开头的变量名。这是您的主要问题所在。从代码中删除 $ 后,我们可以很容易地看到如何在 Lua 中引用变量。

    randomCorr = math.random(100)
    print("The random number:", randomCorr)
    randomCorr = math.random(100)
    print("New Random Number:", randomCorr)
    

    此外,连接并不像您将其暗示到您的 Http 数组中那样工作。您必须在 Lua 中使用 .. 连接值

    看看下面的例子:

    ran = math.random(100)
    data = "{\""..ran.."\"}"
    print(data)
    --{"14"}
    

    您的代码中可以隐含相同的逻辑:

    data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\""..randomCorr.."\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}"
    

    或者您可以使用字符串库提供的方法之一来格式化值

    看看下面的例子:

    ran = math.random(100)
    data = "{%q}"
    print(string.format(data,ran))
    --{"59"}
    

    %q 说明符会将您输入的任何内容作为输入,并用引号将其安全地括起来

    同样的逻辑可以应用到你的 Http 数据上。

    【讨论】:

    • 非常感谢。把我的头放在这上面:) 现在像一个魅力一样工作。再次感谢。
    【解决方案2】:

    这里是代码sn-p的修正版:

    local randomCorr = math.random(1,100)
    
    http.request_batch({
    {"POST", "https://store.thestore.com/priceAndOrder/selectProduct", headers={["Content-Type"]="application/json;charset=UTF-8"}, data="{\"ChoosenPhoneModelId\":4,\"PricePlanId\":\"phone\",\"CorrelationId\":\"" .. randomCorr .. "\",\"DeliveryTime\":\"1 vecka\",\"$$hashKey\":\"006\"},\"ChoosenAmortization\":{\"AmortizationLength\":0,\"ChoosenDataPackage\":{\"Description\":\"6 GB\",\"PricePerMountInKr\":245,\"DataAmountInGb\":6,\"$$hashKey\":\"00W\"},\"ChoosenPriceplan\":{\"IsPostpaid\":true,\"Title\":\"Fastpris\",\"Description\":\"Fasta kostnader till fast pris\",\"MonthlyAmount\":0,\"AvailiableDataPackages\":null,\"SubscriptionBinding\":0,\"$$hashKey\":\"00K\"}}", auto_decompress=true},
    {"GET", "https://store.thestore.com/api/checkout/getproduct?correlationId=" .. randomCorr, auto_decompress=true},
    })
    

    在引用的字符串中还有一个叫做 $$hashKey 的东西。不确定这是否应该引用变量。如果是,还需要使用 .. 运算符将其连接到结果字符串中(就像使用 randomCorr 变量一样)。

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 2014-05-08
      • 2021-07-05
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多