Emulate a once only controller

Hi all,

moving along in my jmeter to k6 conversion and need to be able to emulate the jmeter function of a Once Only Controller (Apache JMeter - User's Manual: Component Reference)

Where I need to initiate, for example, a connection to a physical device, once during a multi user scenario then never again. So many virtual users will be utilizing this one connection.

I could I suppose code this manually but if there’s a better way I’m all ears.


Wouldn’t many concurrent users utilizing a single connection result in a data race :confused:

I am not familiar with JMeter, but here are various ways you can execute something only once in k6, with various caveats for each:

  1. setup() function
  2. SharedArray creation
  3. per-vu-iterations or shared-iterations scenarios with vus:1, iterations: 1
  4. Using the scenario.iterationInTest or iterationInInstance from k6/execution in an if clause (e.g. if (exec.scenario.iterationInTest) { /* do something only once */ })

The problem with all of these is that you can’t share network connections between VUs, only data, and that’s only with the first 2 options… Can you elaborate on your use case, what are you trying to do exactly. Because if you’re truly trying to share a network connection, probably the only way to do that now is to write an extension with xk6:

The physical devices needs to be registered once and to have the security tokens available for multiple iterations. In this scenario the device registration is essentially the same as a user logging in.

Easiest way would probably be to set a boolean variable to true indicating whether the device is registered or not and if it is it’s skipped in subsequent iterations.

We’ve also considered registering the devices in the the setup section and storing the tokens in an in memory data store such as redis but we are still looking into redis as a concept for K6.

added the following code and it works as I need it too.

import exec from 'k6/execution';
export default function () {

if (exec.scenario.iterationInTest == 0){
//      do one thing