git ssb

4+

Dominic / scuttlebot



Tree: 483100052a6cca6309037475e220f6272a8ef11b

Files: 483100052a6cca6309037475e220f6272a8ef11b / test / random.js

6606 bytesRaw
1var pull = require('pull-stream')
2var paramap = require('pull-paramap')
3var ssbKeys = require('ssb-keys')
4var u = require('./util')
5var ssbClient = require('ssb-client')
6
7var tape = require('tape')
8
9var cats = require('cat-names')
10var dogs = require('dog-names')
11
12var generated = {}, F=100,N=10000
13
14//build a random network, with n members.
15function bar (prog) {
16 var r = prog.progress/prog.total
17 var s = '\r', M = 50
18 for(var i = 0; i < M; i++)
19 s += i < M*r ? '*' : '.'
20
21 return s + ' '+prog.progress+'/'+prog.total+' '//+':'+prog.feeds
22}
23
24function isNumber (n) {
25 return typeof n === 'number'
26}
27
28var createSbot = require('../')
29 .use(require('../plugins/friends'))
30 .use(require('../plugins/replicate'))
31 .use(require('../plugins/gossip'))
32
33function generateAnimals (sbot, feed, f, n, cb) {
34 var a = [feed]
35
36 while(f --> 0)
37 a.push(sbot.createFeed())
38
39 console.log('generate NAMES')
40
41 pull(
42 pull.values(a),
43 paramap(function (feed, cb) {
44 var animal = Math.random() > 0.5 ? 'cat' : 'dog'
45 var name = animal == 'cat' ? cats.random() : dogs.allRandom()
46
47 feed.name = name
48 feed.add(u.follow(feed.id), cb)
49 }, 10),
50 pull.drain(null, function (err) {
51 if(err) return cb(err)
52
53 var posts = []
54
55 pull(
56 pull.count(n),
57 paramap(function (n, cb) {
58
59 var me = a[~~(Math.random()*a.length)]
60 var r = Math.random()
61
62 //one in 20 messages is a random follow
63 if(r < 0.5) {
64 var f = a[~~(Math.random()*a.length)]
65 me.add(u.follow(f.id), cb)
66 } else if(r < 0.6) {
67 me.add({
68 type: 'post',
69 text: me.animal === 'dog' ? 'woof' : 'meow',
70 }, function (err, msg) {
71 posts.push(msg.key)
72 if(posts.length > 100)
73 posts.shift()
74 cb(null, msg)
75 })
76 } else {
77 var post = posts[~~(Math.random()*posts.length)]
78 me.add({
79 type: 'post',
80 repliesTo: post,
81 text: me.animal === 'dog' ? 'woof woof' : 'purr',
82 }, function (err, msg) {
83 cb(null, msg)
84 })
85
86 }
87 }, 32),
88 pull.drain(null, cb)
89 )
90
91 })
92 )
93
94}
95
96function latest (sbot, cb) {
97 sbot.friends.hops({hops: 3}, function (err, keys) {
98 if(err) return cb(err)
99 var n = 0, map = {}
100 for(var k in keys) (function (key) {
101 n++
102 sbot.latestSequence(key, function (err, value) {
103 map[key] = isNumber(value) ? value : value.sequence
104 if(--n) return
105 cb(null, map)
106 })
107 })(k)
108 })
109}
110
111 var alice = ssbKeys.generate()
112 var bob = ssbKeys.generate()
113
114
115
116var animalNetwork = createSbot({
117 temp: 'test-random-animals',
118 port: 45451, host: 'localhost', timeout: 20001,
119 replication: {hops: 3}, keys: alice
120})
121
122pull(
123 animalNetwork.replicate.changes(),
124 pull.drain(function (prog) {
125 prog.id = 'animal network'
126 console.log(prog)
127 })
128)
129
130tape('generate random network', function (t) {
131 var start = Date.now()
132 generateAnimals(animalNetwork, {add: animalNetwork.publish}, F, N, function (err) {
133 if(err) throw err
134 console.log('replicate GRAPH')
135 var c = 0
136 latest(animalNetwork, function (err, _generated) {
137 if(err) throw err
138
139 generated = _generated
140 var total = 0, feeds = 0
141 for(var k in generated) {
142 total += generated[k]
143 feeds ++
144 }
145
146 var time = (Date.now()-start)/1000
147 console.log('generated', total, 'messages in', time, 'at rate:', total/time)
148 console.log('over', feeds, 'feeds')
149 t.equal(total, N+1+F+1)
150 t.equal(feeds, F+1)
151 t.end()
152 })
153 })
154})
155
156tape('read all history streams', function (t) {
157 var opts = {
158 host: 'localhost', port: 45451,
159 key: alice.id,
160 manifest: animalNetwork.manifest()
161 }
162
163 var dump = createSbot({
164 temp: 'test-random-animals_dump',
165// port: 45453, host: 'localhost', timeout: 20001,
166 keys: bob
167 })
168 var live = 0, listeners = 0
169 var since = {}
170
171 var h = 0
172
173 pull(
174 dump.createLogStream({live: true, keys: false}),
175 pull.drain(function (e) {
176 live ++
177 })
178 )
179
180 var wants = {}, n = 0, c = 0, start = Date.now()
181
182 //test just dumping everything!
183 //not through network connection, because createLogStream is not on public api
184 pull(
185 animalNetwork.createLogStream({keys: false}),
186 pull.through(function (n) {
187 c++
188 }),
189 dump.createWriteStream(function (err, data) {
190 if(err) throw err
191 var time = (Date.now() - start)/1000
192 console.log("dump all messages via createLogStream")
193 console.log('all histories dumped', c, 'messages in', time, 'at rate', c/time)
194 console.log('read back live:', live, 'over', h, 'histories', listeners, 'listeners')
195 pull(
196 dump.createLogStream(),
197 pull.collect(function (err, ary) {
198 if(err) throw err
199 console.log(c)
200 t.equal(ary.length, F+N+2)
201 dump.close()
202 t.end()
203 })
204 )
205 })
206 )
207
208})
209
210tape('replicate social network for animals', function (t) {
211 //return t.end()
212 var c = 0
213 if(!animalNetwork.friends)
214 throw new Error('missing frineds plugin')
215
216 var start = Date.now()
217 var animalFriends = createSbot({
218 temp: 'test-random-animals2',
219 port: 45452, host: 'localhost', timeout: 20001,
220 replication: {hops: 3},
221 progress: true,
222 seeds: [animalNetwork.getAddress()],
223 keys: bob
224 })
225 animalFriends.logging
226 var connections = 0
227
228 animalFriends.on('rpc:connect', function (rpc) {
229 connections++
230 console.log("CONNECT", c)
231 rpc.on('closed', function () {
232 console.log("DISCONNECT", -c)
233 })
234 })
235
236 pull(
237 animalFriends.replicate.changes(),
238 pull.drain(function (prog) {
239 prog.id = 'animal friends'
240 var target = F+N+3
241 console.log(prog, target)
242// progress.push(prog)
243// process.stdout.write(bar(prog))
244 if(prog.progress === target) {
245 console.log("DONE!!!!")
246 var time = (Date.now() - start) / 1000
247 console.log('replicated', target, 'messages in', time, 'at rate',target/time)
248 animalFriends.close(true)
249 t.end()
250 }
251 })
252 )
253
254 animalFriends.logging = true
255
256 if(!animalFriends.friends)
257 throw new Error('missing friends plugin')
258
259 animalFriends.publish({
260 type: 'contact',
261 contact: animalNetwork.id,
262 following: true
263 }, function (err, msg) {
264 if(err) throw err
265 })
266
267})
268
269
270tape('shutdown', function (t) {
271 animalNetwork.close(true)
272 t.end()
273})
274
275
276
277
278
279
280

Built with git-ssb-web