bookworm-smart-assistant/scripts/poc/poc-h2-worker.js

27 lines
775 B
JavaScript
Raw Permalink Normal View History

'use strict';
const fs = require('fs');
const mode = process.argv[2]; // 'jsonl' | 'json-full'
const target = process.argv[3];
const workerId = process.argv[4];
const count = Number(process.argv[5]) || 100;
function writeJsonlRecord() {
for (let i = 0; i < count; i++) {
const rec = JSON.stringify({ worker: workerId, i, ts: Date.now() }) + '\n';
fs.appendFileSync(target, rec);
}
}
function writeJsonFullRewrite() {
for (let i = 0; i < count; i++) {
let arr = [];
try { arr = JSON.parse(fs.readFileSync(target, 'utf8') || '[]'); } catch { arr = []; }
arr.push({ worker: workerId, i, ts: Date.now() });
fs.writeFileSync(target, JSON.stringify(arr), 'utf8');
}
}
if (mode === 'jsonl') writeJsonlRecord();
else writeJsonFullRewrite();