-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
executable file
·95 lines (85 loc) · 2.09 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
/**
* Traffic generator based on Puppeteer.
*/
const path = require("path");
const { spawn, Worker, Pool } = require("threads");
const chalk = require("chalk");
const yargs = require("yargs");
const { getConfig } = require("./utils");
// Parse args.
const argv = yargs
.option("config", {
alias: "cfg",
description: "The config file path",
type: "string",
default: ".trafficator.js",
coerce: file => {
return path.resolve(file);
}
})
.option("concurrency", {
alias: "c",
description:
"The number of sessions to run simultaneously. A higher number will use more resources but will complete more quickly.",
type: "number"
})
.option("sessions", {
alias: "s",
description: "Number of sessions to run.",
type: "number"
})
.option("verbose", {
alias: "v",
description: "Whether to output full logs.",
type: "boolean",
default: false
})
.help()
.alias("help", "h").argv;
// Read config in.
const config = getConfig(argv.config);
// Set overrides.
if (argv.sessions) {
config.sessions = argv.sessions;
}
if (argv.concurrency) {
config.concurrency = argv.concurrency;
}
// Track stats.
const start = Date.now();
// Get output stats.
const end = () => {
console.log(chalk.green(`Time elapsed: ${(Date.now() - start) / 1000}s`));
process.exit(0);
};
// Pre-flight.
if (!config.funnels.length) {
console.log(chalk.red("You must configure at least one funnel object"));
process.exit(1);
}
if (!Array.isArray(config.funnels)) {
console.log(chalk.red(".funnels must be an array"));
process.exit(1);
}
// Quaid... start the reactor.
(async () => {
// Create worker pool.
const pool = Pool(() => {
const worker = new Worker("./session");
worker.on('error', e => {
console.log(e);
});
return spawn( worker );
}, config.concurrency);
// Queue sessions.
for (i = 0; i < config.sessions; i++) {
const sessionId = i + 1;
pool.queue(async session => {
await session(argv.config, sessionId);
});
}
await pool.completed();
await pool.terminate();
end();
})();