-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-data.js
More file actions
67 lines (54 loc) · 2.03 KB
/
Copy pathgenerate-data.js
File metadata and controls
67 lines (54 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const fs = require('fs');
const path = require('path');
function generateData(targetFile, sizeMB = 100) {
return new Promise((resolve, reject) => {
const line = "2023-10-25 [INFO] Processing data stream...\n2023-10-25 [DEBUG] Memory check OK\n";
const errorLine = "2023-10-25 [ERROR] Critical failure detected at index ID\n";
const totalSize = sizeMB * 1024 * 1024;
let written = 0;
const CHUNK_SIZE = 1024 * 1024;
const writeStream = fs.createWriteStream(targetFile);
writeStream.on('error', reject);
writeStream.on('finish', () => {
resolve(targetFile);
});
function writeChunk() {
if (written >= totalSize) {
writeStream.end();
return;
}
const buffer = Buffer.alloc(CHUNK_SIZE);
let offset = 0;
while (offset < CHUNK_SIZE) {
if (Math.random() > 0.85) {
offset += buffer.write(errorLine, offset);
} else {
offset += buffer.write(line, offset);
}
}
const ok = writeStream.write(buffer);
written += CHUNK_SIZE;
if (process.stdout.isTTY) {
process.stdout.write(`\r[DataGen] Progress: ${(written / (1024 * 1024)).toFixed(1)} / ${sizeMB} MB`);
}
if (!ok) {
writeStream.once('drain', writeChunk);
} else {
setImmediate(writeChunk);
}
}
writeChunk();
});
}
if (require.main === module) {
const sizeMB = parseInt(process.argv[2], 10) || 100;
const target = path.join(__dirname, 'big_data.log');
console.log(`Generating ${sizeMB} MB test dataset at ${target}...`);
generateData(target, sizeMB).then(() => {
console.log(`\n✅ Generated successfully: ${target}`);
}).catch(err => {
console.error("Failed to generate data:", err);
process.exit(1);
});
}
module.exports = { generateData };