-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an experimental csv module exposing a streaming csv parser (#3743)
* Expose fs.File underlying implementation's ReadSeeker to other modules * Add ability to produce a SharedArray from Go code * Add experimental csv parser module * Add csv module usage examples * Apply suggestions from code review Co-authored-by: Oleg Bespalov <[email protected]> * Rename data.Reader to data.RecordReader * Apply pull request review suggestions * Update js/modules/k6/experimental/csv/module_test.go Co-authored-by: Joan López de la Franca Beltran <[email protected]> * Update js/modules/k6/experimental/csv/reader.go Co-authored-by: Joan López de la Franca Beltran <[email protected]> * Update js/modules/k6/experimental/fs/module.go Co-authored-by: Joan López de la Franca Beltran <[email protected]> * Apply Pull Request suggestions * Fix linting errors --------- Co-authored-by: Oleg Bespalov <[email protected]> Co-authored-by: Joan López de la Franca Beltran <[email protected]>
- Loading branch information
1 parent
5f36e9b
commit 93667ec
Showing
14 changed files
with
1,364 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { open } from 'k6/experimental/fs' | ||
import csv from 'k6/experimental/csv' | ||
import { scenario } from 'k6/execution' | ||
|
||
export const options = { | ||
iterations: 10, | ||
} | ||
|
||
// Open the csv file, and parse it ahead of time. | ||
let file; | ||
let csvRecords; | ||
(async function () { | ||
file = await open('data.csv'); | ||
|
||
// The `csv.parse` function consumes the entire file at once, and returns | ||
// the parsed records as a SharedArray object. | ||
csvRecords = await csv.parse(file, {delimiter: ','}) | ||
})(); | ||
|
||
|
||
export default async function() { | ||
// The csvRecords a SharedArray. Each element is a record from the CSV file, represented as an array | ||
// where each element is a field from the CSV record. | ||
// | ||
// Thus, `csvRecords[scenario.iterationInTest]` will give us the record for the current iteration. | ||
console.log(csvRecords[scenario.iterationInTest]) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { open } from 'k6/experimental/fs' | ||
import csv from 'k6/experimental/csv' | ||
|
||
export const options = { | ||
iterations: 10, | ||
} | ||
|
||
let file; | ||
let parser; | ||
(async function () { | ||
file = await open('data.csv'); | ||
parser = new csv.Parser(file); | ||
})(); | ||
|
||
export default async function() { | ||
// The parser `next` method attempts to read the next row from the CSV file. | ||
// | ||
// It returns an iterator-like object with a `done` property that indicates whether | ||
// there are more rows to read, and a `value` property that contains the row fields | ||
// as an array. | ||
const {done, value} = await parser.next(); | ||
if (done) { | ||
throw new Error("No more rows to read"); | ||
} | ||
|
||
// We expect the `value` property to be an array of strings, where each string is a field | ||
// from the CSV record. | ||
console.log(done, value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
firstname,lastname,age | ||
fariha,ehlenfeldt,72 | ||
qudratullah,gillfillan,50 | ||
jeleah,rodina,41 | ||
thaisia,nowalk,99 | ||
joey-lynn,wilsford,75 | ||
tudur,granville,81 | ||
aytek,umber,56 | ||
aynoor,hisaw,30 | ||
fiadh-rose,iravani,31 | ||
annely,ooley,70 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.