-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (57 loc) · 1.46 KB
/
app.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
import { config } from "dotenv";
import faker from "faker";
import FormData from "form-data";
import fetch from "node-fetch";
import async from "async";
config();
const DEFAULT_ITERATIONS = 10;
const DEFAULT_CONCURRENT_REQUESTS = 1;
const MAX_ERRORS = 10;
const [iterations = DEFAULT_ITERATIONS, concurrentRequests = DEFAULT_CONCURRENT_REQUESTS] =
process.argv.splice(2);
const instances = Array(Number(iterations)).fill();
async function sendFakeForm() {
const { formData, user } = getFakeForm();
const logText = `Sent ${user.email}, ${user.password}`;;
console.time(logText);
const response = await fetch(process.env.ENDPOINT_URL, {
method: "POST",
body: formData,
});
console.log(response.status);
console.timeEnd(logText);
return response.statusText;
}
function getFakeForm() {
const user = getUser();
const { email, password } = user;
const formData = new FormData();
formData.append("email", email);
formData.append("password", password);
return {formData, user};
}
function getUser() {
return {
email: faker.internet.email(),
password: faker.internet.password(),
};
}
const errors = [];
async.mapLimit(
instances,
Number(concurrentRequests),
async function () {
const result = await sendFakeForm();
return result;
},
(error, results) => {
if (error) {
if (errors.length >= MAX_ERRORS) {
throw error;
}
console.error(error);
errors.push(error);
}
console.log('\n', `Sent ${results.length} fake forms`)
}
);