artillery
Version:
Cloud-scale load testing. https://www.artillery.io
60 lines (59 loc) • 1.77 kB
JavaScript
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import _ from 'lodash';
export default function createReader(order, spec) {
if (order === 'sequence') {
return createSequencedReader();
}
else if (typeof order === 'undefined' &&
typeof spec?.name !== 'undefined' &&
spec?.loadAll === true) {
return createEverythingReader(spec);
}
else {
// random
return createRandomReader();
}
}
function createSequencedReader() {
let i = 0;
return (data) => {
const result = data[i];
if (i < data.length - 1) {
i++;
}
else {
i = 0;
}
return result;
};
}
function createEverythingReader(spec) {
let parsedData;
return (data) => {
if (!parsedData) {
const parsed = [];
// Parse the row into an object based on the fields spec
if (spec.fields && spec.fields.length > 0) {
for (const row of data) {
const o = {};
for (let i = 0; i < spec.fields.length; i++) {
const fieldName = spec.fields[i];
o[fieldName] = row[i];
}
parsed.push(o);
}
parsedData = parsed;
}
else {
// Otherwise just return the array of rows
parsedData = data;
}
}
return parsedData;
};
}
function createRandomReader() {
return (data) => data[Math.max(0, _.random(0, data.length - 1))];
}