jeffbski / bench-rest

bench-rest - benchmark REST (HTTP/HTTPS) API's. node.js client module for easy load testing / benchmarking REST API's using a simple structure/DSL can create REST flows with setup and teardown and returns (measured) metrics.
MIT License
303 stars 51 forks source link

let 'main' be a function that passes in Index #29

Open Jonahss opened 7 years ago

Jonahss commented 7 years ago

Hello,

I was looking for a module just like this one, so was glad to find it :)

In my use case, I wanted each request to have different random information in the payload, or a random header. I assumed I could pass bench-rest a function, and it would call the function each time a request is generated so that I can easily calculate different parameters each time.

Here's an example of sending different coordinates to an api endpoint that tracks the location of a package:

  let flow = (index) => {
    return {
      main: [
        {
          post: `${env}/track`,
          headers: {
            'Auth-Token': _.sample(authTokens)
          },
          json: randomLocation(),
          delayAfter: 500,
        }
      ]
    }
  }

Notice how interpolation of the INDEX is no longer necessary, if we just have the flow defined as a function instead of a plain json object. The index can be passed in as the first argument and used by users if they choose.

Of course, the change is backwards compatible, if you pass in a plain object, it uses it. If you pass in a function it runs the function to get the object.

My implementation in this PR is a first draft. If you like the idea I can flesh-out the support and add docs.

What do you think?

jeffbski commented 7 years ago

@Jonahss This sounds like a great idea. I had always planned that we might need a way to extend the values that are interpolated, so this seems like a nice way to do it.

I'm going to ponder this a bit to consider if there are any other things we should take into account in this API. If we think that the number of things we pass might grow, we might want to pass an object and then using destructuring it is just as easy to get at what you want, yet it allows us to pass more things later.

Jonahss commented 7 years ago

bump