Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Sep 8, 2023
1 parent de8ed6e commit e66ee0d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
10 changes: 7 additions & 3 deletions examples/experimental/fs/fs-openclose.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import { open } from "k6/experimental/fs";
import { sleep } from "k6";

export const options = {
vus: 10,
iterations: 10,
vus: 1,
iterations: 1,
};

let file;
(async function () {
file = await open("bonjour.txt");
})();

export async function setup() {
return { something: 'crazy' };
}

export default async function () {
file.close();
sleep(10);
sleep(1);
}
27 changes: 17 additions & 10 deletions js/modules/k6/experimental/fs/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,28 @@ func (fr *cache) open(filename string, fromFs afero.Fs) ([]byte, error) {
panic(fmt.Errorf("registry's file %s is not stored as a byte slice", filename))
}

// Increase the ref count.
// As this is the first time we open the file, initialize
// the ref count to 1.
fr.mutex.Lock()
fr.openedRefCounts[filename]++
fmt.Println("cache.open[file already loaded]: openedRefCount=", fr.openedRefCounts[filename])
fmt.Println("cache.open[initializing open]: openedRefCount=", 1)
if c, ok := fr.openedRefCounts[filename]; ok {
fr.openedRefCounts[filename] = c + 1
} else {
fr.openedRefCounts[filename] = 1
}
// fr.openedRefCounts[filename] = 1
fmt.Println("cache.open[initializing open]: openedRefCount=", 1)
fr.mutex.Unlock()

return data, nil
}

// // Increase the ref count.
// fr.mutex.Lock()
// fr.openedRefCounts[filename]++
// fmt.Println("cache.open[file already loaded]: openedRefCount=", fr.openedRefCounts[filename])
// fr.mutex.Unlock()

// The underlying afero.Fs.Open method will cache the file content during this
// operation. Which will lead to effectively holding the content of the file in memory twice.
// However, as per #1079, we plan to eventually reduce our dependency on afero, and
Expand All @@ -90,13 +103,6 @@ func (fr *cache) open(filename string, fromFs afero.Fs) ([]byte, error) {

fr.openedFiles.Store(filename, data)

// As this is the first time we open the file, initialize
// the ref count to 1.
fr.mutex.Lock()
fr.openedRefCounts[filename] = 1
fmt.Println("cache.open[initializing open]: openedRefCount=", 1)
fr.mutex.Unlock()

return data, nil
}

Expand All @@ -116,6 +122,7 @@ func (fr *cache) close(filename string) error {
}

// Decrease the ref count.
fmt.Println("cache.close: decreasing ref count")
fr.openedRefCounts[filename]--
fmt.Println("cache.close: openedRefCount=", fr.openedRefCounts[filename])

Expand Down
8 changes: 8 additions & 0 deletions js/modules/k6/experimental/fs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (mi *ModuleInstance) Exports() modules.Exports {

// Open opens a file and returns a promise that will resolve to a [File] instance
func (mi *ModuleInstance) Open(path goja.Value) *goja.Promise {
fmt.Println("Open called")
promise, resolve, reject := promises.New(mi.vu)

// Files can only be opened in the init context.
Expand Down Expand Up @@ -169,6 +170,13 @@ func (f *File) Stat() *goja.Promise {

// Close closes the file.
func (f *File) Close() {
if f.vu.State() == nil {
common.Throw(
f.vu.Runtime(),
newFsError(ForbiddenError, "close() failed; reason: closing a file outside of the VU stage is forbidden"),
)
}

if err := f.file.close(); err != nil {
common.Throw(f.vu.Runtime(), err)
}
Expand Down

0 comments on commit e66ee0d

Please sign in to comment.