Write data to file

I always was in situations when I need to save some data to file, like the response of the requests, different bodies etc. Is there an easy way to do this? will this be added in some future realeses? I want to avoid cli commands that outputs everything to file.
Something like fs.write would be nice to have.

Hi, welcome to the forum.

Because of the highly concurrent and potentially distributed nature of k6, it would be difficult to support writing to local disk like fs.write().

Instead you could use some type of distributed storage for this, like Redis or S3. There are a couple of Redis extensions for k6 that could help you with this, otherwise any system like S3 that exposes an HTTP API could be used. Though keep in mind that this will potentially impact performance of your test, and in the case of HTTP services would also skew your end-of-test summary results. You can avoid the metric skew by not depending on the summary and filtering these requests using tags in whatever output system you use.

You could also write a new k6 extension that stores files on the local filesystem, just be aware that this likely won’t be supported in k6 Cloud and would only work for your local single-instance tests.

Not tested, but see below an extension from the community:

1 Like

Hello guys.

I do not agree with the statement “Because of the highly concurrent and potentially distributed nature of k6, it would be difficult to support writing to local disk as fs.write().”.

Since it is possible to print the result using console.log instantly there must be a way to print it to some file. I just couldn’t find a library or stuff that can be imported over http like import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/2.4.0/dist/bundle.js"; because k6 is not based on nodejs.

Did anyone find some workaround?

@szanelato You may disagree with it, but it’s not a trivial problem when running a k6 test in a distributed way on multiple instances. In that case even console.log() output is done per instance, and you need some central mechanism to aggregate it. We know because this is the way we do things in k6 Cloud.

That said, if you’re only running k6 on a single instance/machine, you may find the above mentioned xk6-file extension useful.

1 Like

I could able to write responses in file using xk6 command line extension.

Steps need to follow,

  1. Install GO
  2. Install xk6 extension using cmd: xk6 build v0.46.0 --with github.com/avitalique/xk6-file@latest
  3. Download Git Repo that uses xk6 extension from GitHub - avitalique/xk6-file: k6 extension for writing files
  4. run the script ./k6 run examples/sample-script.js , it will create new text as well as binary file
1 Like