Artillery: Using {{ variable }} as Int variable

Created on 1 Feb 2017  路  11Comments  路  Source: artilleryio/artillery

I can't do that

"sender_id": {{ fbID }}, as it complains on an invalid json.

Doing this "sender_id": "{{ fbID }}", fails the API, as now the number is a string.

Any solution for this ?

enhancement

All 11 comments

Well, "sender_id": {{ fbID }} is not valid JSON

I have encountered a similar problem, I ended up solving it in the following way:
conversion.js:

function convertIDToInt(requestParams, context, ee, next) {
    requestParams.json.id = parseInt(requestParams.json.id)
    return next();
}

scenario.yaml

config:
  environments:
    dev:
      target: http://localhost:8080
  phases:
    -
      duration: 1
      arrivalRate: 1
  processor: "conversion.js"
  variables:
    id:
      - "1"
scenarios:
  -
    name: "Convert id to int"
    flow:
      -
        get:
          url: "/api/v1/something/{{ id }}"
          beforeRequest: "convertIDToInt"
          json:
            id: "{{ id }}"

@paulbes that's very nice! I was looking for a hook to plug something like convertIDToInt but couldn't find one, so I switch to using https://github.com/wg/wrk.

Now that a workaround exists I might look at it again for my next project.

Thanks.

A nice way to solve this would be with YAML tags, so you could do something like:

- post:
  url: "/some/url"
  json:
    field: !Number "2"

But that requires some changes to YAML parsing inside Artillery.

you can use as
json:
name: "{{name}}"
age: !!int "{{age}}"
state: "{{state}}"
country: "{{country}}"

!!int treat as integer

I couldn't find this documented:

variables:
    idTest:
      - "1"

works but

variables:
    id-test:
      - "1"

doesn't.

I found another solution as I was trying to dynamically chain a variable that was captured along the flow.

module.exports = {
    createBody: createBody
};
function createBody(requestParams, context, ee, next) {
    requestParams.json = {
        bodyOne: {
            id: context.vars.variableOne
        },
        bodyTwo: {
            id: context.vars.variableTwo
        },
    return next();
}

I found that Context.vars was correctly storing my variables as integers already. It was when passing them in through the yaml config that they would be converted to strings no matter what I did. This helper function then can be called with a beforeRequest and I removed the json: section from my POST in the artillery test.

that's a great solution @jordandlaman, we'll prioritize getting this sorted properly without needing to resort to writing custom JS

@hassy +1 this! We are writing a lot of our new services in Go and have discovered that the custom js functions can alter the response times recorded (which I realize is a separate issue). But it would be great to be able to pass in arrays of integers.

The most recent version of Artillery will now preserve the type of the variable rendered by a template if that's the only operation in that template, so in the something like the following:

    flow:
      -
        post:
          url: "/api/v1/something/{{ id }}"
          json:
            id: "{{ id }}"

If id = 42, the resulting JSON payload sent to the target will be:

{"id": 42}

@tiffanywilson Mind opening another issue about the custom JS stuff? very curious

I've trying to do the same with an environment variable and a beforeRequest script to cast it to integer but it seems that the environment variable is still not replaced when the script runs so the conversion fails... Anybody has experienced that?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hartmut-co-uk picture hartmut-co-uk  路  4Comments

SamDecrock picture SamDecrock  路  5Comments

DanielMSchmidt picture DanielMSchmidt  路  4Comments

sonisaurabh19 picture sonisaurabh19  路  5Comments

ericmacfarland picture ericmacfarland  路  6Comments