-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
49 lines (38 loc) · 1.21 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
const dns = require('dns');
const util = require('util');
const Timeout = require('await-timeout');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const dnsLoopupAsync = util.promisify(dns.lookup);
const dnsName = process.env.DNS_NAME || 'kubernetes.default.svc.cluster.local';
const dnsTimeout = parseInt(process.env.DNS_NAME_TIMEOUT, 10) || 100;
const waitBetweenRequests = parseInt(process.env.WAIT_BETWEEN_REQUESTS, 10) || 100;
(async () => {
const timeout = new Timeout();
const startDate = +new Date();
let lookups = 0;
let failedLookups = 0;
while(true) {
try {
const lookup = await Promise.race([
dnsLoopupAsync(dnsName),
timeout.set(dnsTimeout, 'DNS Timeout!')
]);
if (!lookup.address) {
throw new Error(lookup);
}
lookups += 1;
} catch (err) {
failedLookups += 1;
console.error('Lookup failed', err);
} finally {
timeout.clear();
}
await sleep(waitBetweenRequests);
if (lookups % 100 == 0) {
const time = Math.round((+new Date() - startDate) / 1000);
console.log(lookups, failedLookups, time + 's', Math.round(lookups / time) + 'lookups / s')
}
}
})()