Skip to content

Commit

Permalink
add WriteString method
Browse files Browse the repository at this point in the history
  • Loading branch information
avitalique committed Apr 12, 2021
1 parent 8a37283 commit cf7cf8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# xk6-file
A [k6](https://github.com/loadimpact/k6) extension for writing files, implemented using the
[k6](https://github.com/loadimpact/k6) extension for writing files, implemented using the
[xk6](https://github.com/k6io/xk6) system.

## Build
`xk6 build v0.31.1 --with github.com/avitalique/xk6-file`
```shell
xk6 build v0.31.1 --with github.com/avitalique/xk6-file
```

## Example
```javascript
Expand All @@ -12,6 +14,7 @@ import file from 'k6/x/file';
const filepath = 'sample-output.txt';

export default function () {
file.appendString(filepath, `Some text. VU: ${__VU} - ITER: ${__ITER}\n`);
file.writeString(filepath, 'New file. First line.\n');
file.appendString(filepath, `Second line. VU: ${__VU} - ITER: ${__ITER}`);
}
```
18 changes: 16 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@ func init() {
modules.Register("k6/x/file", new(FILE))
}

// FILE is the k6 extension.
// FILE is the k6 extension
type FILE struct{}

// Append a string to file
// Write string to file
func (*FILE) WriteString(path string, s string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()

if _, err := f.WriteString(s); err != nil {
return err
}
return nil
}

// Append string to file
func (*FILE) AppendString(path string, s string) error {
f, err := os.OpenFile(path,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
Expand Down
3 changes: 2 additions & 1 deletion sample-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import file from 'k6/x/file';
const filepath = 'sample-output.txt';

export default function () {
file.appendString(filepath, `Some text. VU: ${__VU} - ITER: ${__ITER}\n`);
file.writeString(filepath, 'New file. First line.\n');
file.appendString(filepath, `Second line. VU: ${__VU} - ITER: ${__ITER}`);
}

0 comments on commit cf7cf8b

Please sign in to comment.