Running Other Script (Python or Javascript) from K6 Script

Hello, I have a python script and a javascript script that build a flatbuffer and converts it to binary. I need to run one of these scripts from inside the k6 default function and use it’s output. For example, lets say I have a javascript script called “test.js” and it returns a value “123.” I need to be able to call that “test.js” from inside the K6 default function and be able use the “123” value and send it over a websocket. Is this possible?

Hi @mipeters , welcome to the community forum!

You can run external programs with the GitHub - grafana/xk6-exec: A k6 extension for running external commands. extension.

hello.py:

print("123")

script.js:

import exec from 'k6/x/exec';

export function setup(){
  console.log(exec.command("python3",["hello.py"]));
}

export default function () {
  console.log("iteration")
}

The output of the ./k6 run -i 10 script.js command (where k6 is the custom k6 binary with xk6-exec built into) is:

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: script.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 10 iterations shared among 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0000] 123                                           source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console
INFO[0000] iteration                                     source=console

     █ setup

     data_received........: 0 B 0 B/s
     data_sent............: 0 B 0 B/s
     iteration_duration...: avg=1.43ms min=8.23µs med=8.87µs max=15.6ms p(90)=48.73µs p(95)=7.82ms
     iterations...........: 10  618.017599/s


running (00m00.0s), 0/1 VUs, 10 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m00.0s/10m0s  10/10 shared iters

I hope it helps