Skip to content

Commit

Permalink
Improve randomness of test files
Browse files Browse the repository at this point in the history
  • Loading branch information
ljwagerfield committed Jan 10, 2024
1 parent d843c3f commit b4be44f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tests/utils/RandomStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ import * as Path from "path";
function createRandomStream(sizeInBytes: number): NodeJS.ReadableStream {
const dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");

// Ensure the chunk size is small enough to cause enough entropy (as we pick a new character for reach chunk).
const minRandomBlocks = 10;
const maxChunkSize = Math.min(64 * 1024, Math.ceil(sizeInBytes / minRandomBlocks));

let producedSize = 0;
let iteration = 0;
return new Readable({
read(readSize) {
read(requestedReadSize) {
const maxRequestedReadSize = Math.min(requestedReadSize, maxChunkSize);

// This ensures streams of the same size are unique, else they would have the same contents, assuming chunk size is the same.
// We use 0.8-1 to prevent really small chunks, which may create inefficiencies.
const randomness = randomBetween(0.8, 1);

let readSize = Math.ceil(maxRequestedReadSize * randomness);
let shouldEnd = false;

if (producedSize + readSize >= sizeInBytes) {
Expand Down Expand Up @@ -48,6 +59,10 @@ function createRandomStream(sizeInBytes: number): NodeJS.ReadableStream {
});
}

function randomBetween(min: number, max: number): number {
return Math.random() * (max - min) + min;
}

async function writeToDisk(reader: NodeJS.ReadableStream, path: string): Promise<void> {
await new Promise((resolve, reject) => {
const writer = fs.createWriteStream(path);
Expand Down

0 comments on commit b4be44f

Please sign in to comment.