Skip to content

Commit

Permalink
Este/predictable size (#8)
Browse files Browse the repository at this point in the history
* keep random string in a predictable lenght

* added some docs

* better docs

---------

Co-authored-by: Ronny Esterluss <[email protected]>
  • Loading branch information
ausias-armesto and esterlus authored Oct 21, 2024
1 parent 3bcc7d1 commit 39bc6f3
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions k6/src/k6/send-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Counter, Trend } from "k6/metrics";
import http, { RefinedParams, RefinedResponse, ResponseType } from "k6/http";
import { check, fail } from "k6";
import ws from "k6/ws";
import crypto from 'k6/crypto';
// 1. Begin Init section
const nodes = __ENV.NODES || "many2many";

Expand All @@ -18,7 +17,9 @@ const amountOfReceivers = nodesData.nodes.filter(
node.enabled && node.isReceiver != undefined && node.isReceiver,
).length;

const MaxPayloadBytes = 325;
const MaxPayloadBytes = 400;
const Alphabet =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

// Override API Token
if (__ENV.HOPRD_API_TOKEN) {
Expand Down Expand Up @@ -247,15 +248,17 @@ export function multipleHopMessage(dataPool: {
);
}

// use random ASCI chars to extend the payload
// UTF-8 uses variying byte sizes - try to keep them in the single byte range
// so we have a predicatble payload size
function extendWrandomBytes(body: string): string {
const count = MaxPayloadBytes - body.length - 1;
const bytes = crypto.randomBytes(count);
const uintArray = new Uint8Array(bytes);
const str = uintArray.reduce<string>((acc, v) => {
acc += String.fromCharCode(v);
return acc;
}, "");
return `${body} ${str}`;
let rndmContent = "";
for (let i = 0; i < count; i++) {
const idx = Math.floor(Math.random() * Alphabet.length);
rndmContent += Alphabet.charAt(idx);
}
return `${body} ${rndmContent}`;
}

export function teardown(dataPool: {
Expand Down

0 comments on commit 39bc6f3

Please sign in to comment.