Skip to content

Instantly share code, notes, and snippets.

@mstoykov
Created September 2, 2021 14:41
Show Gist options
  • Save mstoykov/586b51a982d87fffc75cba0bd37914d8 to your computer and use it in GitHub Desktop.
Save mstoykov/586b51a982d87fffc75cba0bd37914d8 to your computer and use it in GitHub Desktop.
import { getter, sequentialIndex, randomizedUniqueIndex, uniquePerScenario, uniquePerVU } from "./lib.js";
import { SharedArray } from "k6/data";
// this are here for easier configuration
const vuNumbers = 3;
const dataPoints = 10;
function generateDataFor(number) {
var result = new Array(number)
for (let i=0;i<number;i++) {
result[i] = "something"+i
}
return result
}
const data = new SharedArray("name", function(){
// This can be something opening a file or something else, but needs to return an array with
// constant length between invocations in order to work in the cloud
return generateDataFor(dataPoints)
})
export const options = {
scenarios :{
"use-all-the-data": {
executor: "shared-iterations",
vus: vuNumbers,
iterations: data.length, // here you can also multiply so it goes through it multiple times
maxDuration: "1h" // this will need to be big enough so that all the iterations can happen if that is what is wanted
}
}
}
const r = getter(data, randomizedUniqueIndex(uniquePerScenario));
const s = getter(data, sequentialIndex(uniquePerScenario));
const rv = getter(data, randomizedUniqueIndex(uniquePerVU));
const sv = getter(data, sequentialIndex(uniquePerVU));
export default function() {
console.log(__VU, __ITER, r(), s(), rv(), sv())
}
import exec from "k6/execution";
export function uniquePerScenario() {
return exec.scenario.iterationInTest;
}
export function uniquePerVU() {
return exec.vu.iterationInScenario;
}
// sequentialIndex
export function sequentialIndex(originalIndexFunc) {
return function(length) {
return function() {
return originalIndexFunc()%length;
}
};
}
function getMutuallyRandomNumber(a) {
let possible = [2,3,5,7,11,13,17,2017]; // better implementations
for (let b of possible) {
if (a%b != 0) {
return b
}
}
return 2027
}
// randomizedUniqueIndex
export function randomizedUniqueIndex(originalIndexFunc) {
// based on https://lemire.me/blog/2017/09/18/visiting-all-values-in-an-array-exactly-once-in-random-order/
// see also https://community.k6.io/t/data-parameterisation-with-unique-index/2128/2
return function (length) {
let a = getMutuallyRandomNumber(length);
// we should add b to be completely randomized
return function() {
return (a*originalIndexFunc())%length;
};
}
}
// data is the data, preferrably as SharedArray but it only cares about having length and being indexable
// indexerFunc is what gives a general indexer
// distrubutionFunc is going to transform the Index in some way to make it either unique or just
export function getter(data, indexerFunc) {
let indexer = indexerFunc(data.length);
return function() {
var index = indexer()
return data[index];
};
};
// whenOutOfRows is currently not usable
// segment is IMO not interesting if you use sharedarray for the underlying thing so will just be skipped for now.
// we can have random per access but for to be unique we need execution segments exported
export function Table( { data, rowSelection, updateRow} ) {
if (rowSelection === undefined) {
rowSelection = sequentialIndex;
}
if (updateRow === undefined) {
updateRow = uniquePerScenario;
}
return getter(data, rowSelection(updateRow));
}
import { Table, randomizedUniqueIndex, uniquePerVU } from "./lib.js";
import { SharedArray } from "k6/data";
// this are here for easier configuration
const vuNumbers = 3;
const dataPoints = 10;
function generateDataFor(number) {
var result = new Array(number)
for (let i=0;i<number;i++) {
result[i] = "something"+i
}
return result
}
const data = new SharedArray("name", function(){
// This can be something opening a file or something else, but needs to return an array with
// constant length between invocations in order to work in the cloud
return generateDataFor(dataPoints)
})
export const options = {
scenarios :{
"use-all-the-data": {
executor: "shared-iterations",
vus: vuNumbers,
iterations: data.length, // here you can also multiply so it goes through it multiple times
maxDuration: "1h" // this will need to be big enough so that all the iterations can happen if that is what is wanted
}
}
}
const r = Table({data: data, rowSelection: randomizedUniqueIndex});
const s = Table({data: data});
const rv = Table({data: data, rowSelection: randomizedUniqueIndex, updateRow: uniquePerVU});
const sv = Table({data: data, updateRow: uniquePerVU});
export default function() {
console.log(__VU, __ITER, r(), s(), rv(), sv())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment