Created
February 6, 2025 23:50
-
-
Save matiaslopezd/b4b81f8ad77b50f61ec5ad2da40d2af2 to your computer and use it in GitHub Desktop.
CSV generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import crypto from 'crypto'; | |
const FILE_SIZE_MB = Number(process.env.SIZE) || 60; | |
const BYTES_PER_MB = 1024 * 1024; | |
const TARGET_SIZE = FILE_SIZE_MB * BYTES_PER_MB; | |
// Configuración de columnas | |
const columns = [ | |
'id', | |
'name', | |
'email', | |
'address', | |
'phone', | |
'created_at', | |
'status', | |
'score', | |
'description' | |
]; | |
// Función para generar una línea de datos aleatorios | |
function generateRandomLine() { | |
return [ | |
crypto.randomUUID(), // id | |
`User${Math.floor(Math.random() * 1000000)}`, // name | |
`user${Math.floor(Math.random() * 1000000)}@example.com`, // email | |
`${Math.floor(Math.random() * 1000)} Random St.`, // address | |
`+1${Math.floor(Math.random() * 1000000000)}`, // phone | |
new Date(Date.now() - Math.random() * 31536000000).toISOString(), // created_at | |
['active', 'inactive', 'pending'][Math.floor(Math.random() * 3)], // status | |
(Math.random() * 100).toFixed(2), // score | |
crypto.randomBytes(50).toString('hex') // description | |
].join(',') + '\n'; | |
} | |
// Crear el archivo | |
const writeStream = fs.createWriteStream(`test_${FILE_SIZE_MB}mb.csv`); | |
// Escribir cabeceras | |
writeStream.write(columns.join(',') + '\n'); | |
let currentSize = 0; | |
let linesWritten = 0; | |
function writeChunk() { | |
let canContinue = true; | |
while (canContinue && currentSize < TARGET_SIZE) { | |
const line = generateRandomLine(); | |
canContinue = writeStream.write(line); | |
currentSize += Buffer.byteLength(line); | |
linesWritten++; | |
if (linesWritten % 1000 === 0) { | |
const progressPercent = (currentSize / TARGET_SIZE * 100).toFixed(2); | |
process.stdout.write(`\rProgress: ${progressPercent}% - Size: ${(currentSize / BYTES_PER_MB).toFixed(2)}MB`); | |
} | |
} | |
if (currentSize < TARGET_SIZE) { | |
writeStream.once('drain', writeChunk); | |
} else { | |
writeStream.end(); | |
} | |
} | |
writeChunk(); | |
writeStream.on('finish', () => { | |
console.log(`\nArchivo generado con éxito!`); | |
console.log(`Tamaño final: ${(currentSize / BYTES_PER_MB).toFixed(2)}MB`); | |
console.log(`Líneas escritas: ${linesWritten}`); | |
}); | |
writeStream.on('error', (err) => { | |
console.error('Error al escribir el archivo:', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment