Created
May 15, 2025 18:03
-
-
Save Hricha-Shandily/4c09fac752f3d02994111c0b994aa8b3 to your computer and use it in GitHub Desktop.
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
| /** | |
| * Parallel Browser Bot Runner | |
| * ------------------------- | |
| * | |
| * This script runs multiple browser bot instances in parallel using Node.js worker threads. | |
| * Each bot instance will: | |
| * - Use random viewport sizes and user agents | |
| * - Have 50% chance of scrolling | |
| * - Have 30% chance of clicking the FAQ link | |
| * | |
| * Usage: | |
| * 1. Basic run (10 instances, localhost:4321): | |
| * node parallel.js | |
| * | |
| * 2. With custom URL: | |
| * node parallel.js https://example.com | |
| * - or - | |
| * TARGET_URL=https://example.com node parallel.js | |
| * | |
| * | |
| * Notes: | |
| * - Each instance runs independently with its own random behaviors | |
| * - The script will report when all instances complete | |
| * - Sometimes a worker might fail trying to fetch the request, this is tolerable | |
| * Since the intent is to study bot blocking capabilities of Plausible | |
| */ | |
| const { | |
| Worker, | |
| isMainThread, | |
| parentPort, | |
| workerData, | |
| } = require("worker_threads"); | |
| const path = require("path"); | |
| if (isMainThread) { | |
| // Parse command line arguments for URL | |
| const args = process.argv.slice(2); | |
| if (args.length > 0) { | |
| process.env.TARGET_URL = args[0]; | |
| } | |
| // Main thread logic | |
| const numWorkers = 10; | |
| const workers = new Set(); | |
| console.log(`Starting ${numWorkers} browser instances...`); | |
| // Create workers | |
| for (let i = 0; i < numWorkers; i++) { | |
| const worker = new Worker(__filename, { | |
| workerData: { workerId: i }, | |
| }); | |
| workers.add(worker); | |
| worker.on("error", (error) => { | |
| console.error(`Worker ${i} error:`, error); | |
| }); | |
| worker.on("exit", (code) => { | |
| workers.delete(worker); | |
| console.log(`Worker ${i} exited with code ${code}`); | |
| if (workers.size === 0) { | |
| console.log("All workers completed!"); | |
| } | |
| }); | |
| worker.on("message", (message) => { | |
| console.log(`Worker ${i}:`, message); | |
| }); | |
| } | |
| } else { | |
| // Worker thread logic - import and run the main bot script | |
| const { workerId } = workerData; | |
| const visitPage = require("./index.js").visitPage; | |
| // Get URL from environment variable or use default | |
| const targetUrl = process.env.TARGET_URL || "http://localhost:4321"; | |
| async function runWorker() { | |
| try { | |
| console.log(`Worker ${workerId} starting...`); | |
| await visitPage(targetUrl); | |
| console.log(`Worker ${workerId} finished successfully`); | |
| process.exit(0); | |
| } catch (error) { | |
| console.error(`Worker ${workerId} failed:`, error); | |
| process.exit(1); | |
| } | |
| } | |
| runWorker(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment