-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.js
More file actions
72 lines (62 loc) · 2.13 KB
/
Copy pathrandom.js
File metadata and controls
72 lines (62 loc) · 2.13 KB
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
(async function() {
const fs = require('fs')
const args = process.argv.reduce((o, a) => {
if (~a.indexOf('=')) {
const kv = a.split('=')
o[kv[0]] = kv[1]
}
return o
}, {})
let nodes = []
try {
let users = await get(args.count)
users.results.forEach(u => {
nodes.push(u.name.last)
})
} catch (e) {
for (let i = 0; i < args.count; i++) {
nodes.push(Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15))
}
}
let graph = ''
let cons = {}
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
const peers = Math.floor(Math.random() * (args.maxchannels || 3)) + 1
cons[node] = []
for (let p = 0; p < peers; p++) {
const d = Math.floor(Math.random() * args.count)
if (d === i) { p--; continue } // con't connect to self, try again
const src = nodes[d]
if (~cons[node].indexOf(src)) { p--; continue } // only one connection
if (cons[src] && ~cons[src].indexOf(node)) { p--; continue } // the other way
cons[node].push(src)
const cap = Math.floor(Math.random() * (10000000 - 500000 + 1) + 500000)
graph += ` "${src}" -- "${node}" [capacity="${cap}"];\n`
}
}
fs.writeFileSync(args.out, Buffer.from(`graph g {\n${graph}}`))
})()
function get(n) {
const https = require('https')
return new Promise((resolve, reject) => {
const req = https.request({
host:'randomuser.me',
method: 'GET',
port: '443',
path: `/api/?results=${n}&inc=name`}, (res) => {
res.setEncoding('utf8');
let response = ''
res.on('data', (chunk) => {
response += chunk
});
res.on('end', () => {
resolve(JSON.parse(response))
});
});
req.on('error', (e) => {
reject(`problem with request: ${e.message}`);
});
req.end();
})
}