|
| 1 | +/*! |
| 2 | + * Copyright 2022 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {execSync} from 'child_process'; |
| 18 | +import {mkdirSync} from 'fs'; |
| 19 | +import path = require('path'); |
| 20 | +import * as uuid from 'uuid'; |
| 21 | + |
| 22 | +export const BLOCK_SIZE_IN_BYTES = 1024; |
| 23 | +export const DEFAULT_SMALL_FILE_SIZE_BYTES = 5120; |
| 24 | +export const DEFAULT_LARGE_FILE_SIZE_BYTES = 2.147e9; |
| 25 | + |
| 26 | +const CREATE_DIRECTORY = 1; |
| 27 | + |
| 28 | +/** |
| 29 | + * Create a uniformly distributed random integer beween the inclusive min and max provided. |
| 30 | + * |
| 31 | + * @param {number} minInclusive lower bound (inclusive) of the range of random integer to return. |
| 32 | + * @param {number} maxInclusive upper bound (inclusive) of the range of random integer to return. |
| 33 | + * @returns {number} returns a random integer between minInclusive and maxInclusive |
| 34 | + */ |
| 35 | +export function randomInteger(minInclusive: number, maxInclusive: number) { |
| 36 | + // Utilizing Math.random will generate uniformly distributed random numbers. |
| 37 | + return ( |
| 38 | + Math.floor(Math.random() * (maxInclusive - minInclusive + 1)) + minInclusive |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Creates a random file name by appending a UUID to the baseName. |
| 44 | + * |
| 45 | + * @param {string} baseName the base file name. A random uuid will be appended to this value. |
| 46 | + * |
| 47 | + * @returns {string} random file name that was generated. |
| 48 | + */ |
| 49 | +export function generateRandomFileName(baseName: string): string { |
| 50 | + return `${baseName}.${uuid.v4()}`; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Creates a file with a size between the small (default 5120 bytes) and large (2.147e9 bytes) parameters. |
| 55 | + * The file is filled with random data. |
| 56 | + * |
| 57 | + * @param {string} fileName name of the file to generate. |
| 58 | + * @param {number} fileSizeLowerBoundBytes minimum size of file to generate. |
| 59 | + * @param {number} fileSizeUpperBoundBytes maximum size of file to generate. |
| 60 | + * @param {string} currentDirectory the directory in which to generate the file. |
| 61 | + * |
| 62 | + * @returns {number} the size of the file generated. |
| 63 | + */ |
| 64 | +export function generateRandomFile( |
| 65 | + fileName: string, |
| 66 | + fileSizeLowerBoundBytes: number = DEFAULT_SMALL_FILE_SIZE_BYTES, |
| 67 | + fileSizeUpperBoundBytes: number = DEFAULT_LARGE_FILE_SIZE_BYTES, |
| 68 | + currentDirectory: string = __dirname |
| 69 | +) { |
| 70 | + const fileSizeBytes = randomInteger( |
| 71 | + fileSizeLowerBoundBytes, |
| 72 | + fileSizeUpperBoundBytes |
| 73 | + ); |
| 74 | + const numberNeeded = Math.ceil(fileSizeBytes / BLOCK_SIZE_IN_BYTES); |
| 75 | + const cmd = `dd if=/dev/urandom of=${currentDirectory}/${fileName} bs=${BLOCK_SIZE_IN_BYTES} count=${numberNeeded} status=none iflag=fullblock`; |
| 76 | + execSync(cmd); |
| 77 | + |
| 78 | + return fileSizeBytes; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Creates a random directory structure consisting of subdirectories and random files. |
| 83 | + * |
| 84 | + * @param {number} maxObjects the total number of subdirectories and files to generate. |
| 85 | + * @param {string} baseName the starting directory under which everything else is added. File names will have this value prepended. |
| 86 | + * @param {number} fileSizeLowerBoundBytes minimum size of file to generate. |
| 87 | + * @param {number} fileSizeUpperBoundBytes maximum size of file to generate. |
| 88 | + */ |
| 89 | +export function generateRandomDirectoryStructure( |
| 90 | + maxObjects: number, |
| 91 | + baseName: string, |
| 92 | + fileSizeLowerBoundBytes: number = DEFAULT_SMALL_FILE_SIZE_BYTES, |
| 93 | + fileSizeUpperBoundBytes: number = DEFAULT_LARGE_FILE_SIZE_BYTES |
| 94 | +) { |
| 95 | + let curPath = baseName; |
| 96 | + for (let i = 0; i < maxObjects; i++) { |
| 97 | + const dirOrFile = randomInteger(0, 1); |
| 98 | + if (dirOrFile === CREATE_DIRECTORY) { |
| 99 | + curPath = path.join(curPath, uuid.v4()); |
| 100 | + mkdirSync(curPath, {recursive: true}); |
| 101 | + } else { |
| 102 | + generateRandomFile( |
| 103 | + generateRandomFileName(baseName), |
| 104 | + fileSizeLowerBoundBytes, |
| 105 | + fileSizeUpperBoundBytes, |
| 106 | + curPath |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments