-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Design a File API for k6 #2977
Comments
Hey, I like the proposal 👍 Though I think we should be inspired more by Deno than Node, since it's more modern, and doesn't have all the cruft of Node. For example, take a look at how they handle file opening and streams. It's very readable, and integrates nicely with the HTTP API. I added an example in the HTTP API design document inspired by this, though we will need to make some decisions first. For example, will |
Thanks for pointing that out 🙇🏻 ! You're right, and the Deno API is likely better, indeed. I'll look into it and update the issue accordingly 👍🏻 I also like that it has support streams already, which is also an issue I've opened, and I believe we should address it. Regarding sync versus async, I think a good compromise would be to have open remaining sync (at least in the first iteration?) to avoid the issue you've pointed out, and also, as it is more of a syscall than pure IO. I would, however, have operations such as |
Note: supporting From technical standpoint I currently need to implement it (or something extremely close) just to make the ESM loading work correctly. Mind you this is the hard part, so it might take a while. On the other hand users can always use |
#3017 is a somewhat related issue, and it probably makes sense to consider both of these together, even if the implementation is not tied. For writing files, we probably will mostly care about the internal Go APIs first, while reading files without fully loading them into memory really needs a JS solution so we can deprecate |
We have reached a consensus on a proposed design and built a proof of concept illustrating its feasibility. This issue is now in the implementation phase and expected to land in version 0.46 of k6, estimated to be released mid-August 2023. |
Problem statement
The only way to interact with files from k6 scripts currently involves using the
open
function. However, theopen
function is somewhat (for historical and technical reasons) misnamed as it, under the hood, not only opens a file but reads its whole content and stores it in memory.Although this exists for technical reasons, for instance, how we package user scripts, resources, and assets and how we interact with the filesystem throughout k6 in general. We believe k6 could use a more "standard" file API; to be more flexible and intuitive and cater to the larger issue of handling large files in k6.
Proposal
We propose the introduction of a basic file API as a k6 module, in the same fashion as Node's. We would assume the existence of a
FileHandle
(temporary name) construct, which would expose an API to interact with the file itself.We assume the following operations would be made possible by this new API:
open
would open a file, regardless of its underlying storage, and return someFileHandle
(name used only for the example). A file handle would be the equivalent of an OS file descriptor but remain agnostic to the underlying storage (fs, tar archive file handle as described in Support finer-grained and richer access to tar archives content #2975, or even a memory map).FileHandle.read()
: would read the entire content of a file and return it to the user.FileHandle.read(buffer)
: would read the content of a file and fill the buffer with read data until it is full.FileHandle.close()
: would close theFileHandle
and render it unusable from this point forward (closing the underlying file descriptor/stream if relevant).Filehandle.seek(offset)
: would allow to move the "reading head" at a different offset in the file.Problem space
The text was updated successfully, but these errors were encountered: