diff --git a/gun.js b/gun.js index 6663de8b..709aaed1 100644 --- a/gun.js +++ b/gun.js @@ -307,7 +307,6 @@ if(nj != ni){ nj = ni; if(!(soul = nl[ni])){ ctx.stun--; // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there. - console.log('done!', ctx.stun); fire(ctx); return; } @@ -416,7 +415,6 @@ var ctx = msg._||'', DBG = ctx.DBG = msg.DBG; DBG && (DBG.g = +new Date); //console.log("GET", get); - console.only(1, 'GET', node); if(!node){ return root.on('get', msg) } if(has){ if('string' != typeof has || !obj_has(node, has)){ return root.on('get', msg) } @@ -449,7 +447,6 @@ var faith = function(){}; faith.ram = faith.faith = true; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited. faith.$ = msg.$; DBG && (DBG.ga = +new Date); - console.only(2, 'GOT:', msg['#'], node); root.on('in', { '@': msg['#'], put: node, @@ -1515,11 +1512,15 @@ ;(function(){ var SMIA = 0; var message, loop; - mesh.hash = function(msg, cb){ var data; - if(!cb){ data = msg.put } - console.log("HASH:", data); - json(data, function(err, text){ - console.log("STRINGIFIED!", text); + mesh.hash = function(msg, peer){ var h, s, t; + json(msg.put, function hash(err, text){ + var ss = (s || (s = t = text||'')).slice(0, 32768); // 1024 * 32 + h = String.hash(ss, h); s = s.slice(32768); + if(s){ puff(hash, 0); return } + msg._.$put = t; + msg['##'] = h; + say(msg, peer); + delete msg._.$put; }) } function each(peer){ mesh.say(message, peer) } @@ -1531,19 +1532,9 @@ var meta = msg._||(msg._=function(){}); if(!(id = msg['#'])){ id = msg['#'] = String.random(9) } !loop && dup_track(id);//.it = it(msg); // track for 9 seconds, default. Earth<->Mars would need more! // always track, maybe move this to the 'after' logic if we split function. - if(!(hash = msg['##']) && u !== msg.put && !meta.via && ack){ console.log('HASH:', msg); mesh.hash(msg); return } // TODO: Should broadcasts be hashed? - if(!(raw = meta.raw)){ - raw = mesh.raw(msg); - if(hash && ack){ - dup_track(ack+hash);//.it = it(msg); - if((tmp = (dup.s[ack]||{}).it) || ((tmp = mesh.last) && ack === tmp['#'])){ - if(hash === tmp['##']){ return false } - tmp['##'] = hash; - } - } - } + if(!(hash = msg['##']) && u !== msg.put && !meta.via && ack){ mesh.hash(msg, peer); return } // TODO: Should broadcasts be hashed? + if(!(raw = meta.raw)){ mesh.raw(msg, peer); return } S && console.STAT && console.STAT(S, +new Date - S, 'say prep'); - //console.log("SEND!", JSON.parse(JSON.stringify(msg))); if(!peer && ack){ peer = ((tmp = dup.s[ack]) && (tmp.via || ((tmp = tmp.it) && (tmp = tmp._) && tmp.via))) || mesh.leap } // warning! mesh.leap could be buggy! if(!peer && ack){ console.STAT && console.STAT(+new Date, ++SMIA, 'total no peer to ack to'); @@ -1553,6 +1544,7 @@ if(!peer || !peer.id){ message = msg; if(!Object.plain(peer || opt.peers)){ return false } var P = opt.puff, ps = opt.peers, pl = Object.keys(peer || opt.peers || {}); + console.log('peers', pl); ;(function go(){ var S = +new Date; //Type.obj.map(peer || opt.peers, each); // in case peer is a peer list. @@ -1590,10 +1582,49 @@ setTimeout(function(){ console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, '0ms TO', id, peer.id); flush(peer); - }, opt.gap); + }, opt.gap); // TODO: queuing/batching might be bad for low-latency video game performance! Allow opt out? send(raw, peer); } mesh.say.c = mesh.say.d = 0; + // TODO: this caused a out-of-memory crash! + mesh.raw = function(msg, peer){ // TODO: Clean this up / delete it / move logic out! + if(!msg){ return '' } + var meta = (msg._) || {}, put, tmp; + if(tmp = meta.raw){ return tmp } + if('string' == typeof msg){ return msg } + if(!msg.dam){ + var i = 0, to = []; tmp = opt.peers; + for(var k in tmp){ var p = tmp[k]; // TODO: Make up peers instead! + to.push(p.url || p.pid || p.id); + if(++i > 6){ break } + } + if(i > 1){ msg['><'] = to.join() } + } + var hash = msg['##'], ack = msg['@']; + if(put = meta.$put){ + tmp = {}; Object.keys(msg).forEach(function(k){ tmp[k] = msg[k] }); + tmp.put = ':])([:'; + json(tmp, function(err, raw){ + if(err){ return } // TODO: Handle!! + tmp = raw.indexOf('"put":":])([:"'); + res(u, raw = raw.slice(0, tmp+6) + put + raw.slice(tmp + 14)); + }); + return; + } + json(msg, res); + function res(err, raw){ + if(err){ return } // TODO: Handle!! + meta.raw = raw; //if(meta && (raw||'').length < (999 * 99)){ meta.raw = raw } // HNPERF: If string too big, don't keep in memory. + if(hash && ack){ + dup_track(ack+hash);//.it = it(msg); + if((tmp = (dup.s[ack]||{}).it) || ((tmp = mesh.last) && ack === tmp['#'])){ + if(hash === tmp['##']){ return false } + tmp['##'] = hash; + } + } + say(msg, peer); + } + } }()); function flush(peer){ @@ -1621,35 +1652,6 @@ (peer.queue = peer.queue || []).push(raw); }} - ;(function(){ - // TODO: this caused a out-of-memory crash! - mesh.raw = function(msg){ // TODO: Clean this up / delete it / move logic out! - if(!msg){ return '' } - var meta = (msg._) || {}, put, hash, tmp; - if(tmp = meta.raw){ return tmp } - if('string' == typeof msg){ return msg } - /*if(!msg.dam){ // TOOD: COME BACK TO THIS LATER!!! IMPORTANT MESH STUFF!! - var i = 0, to = []; Type.obj.map(opt.peers, function(p){ - to.push(p.url || p.pid || p.id); if(++i > 3){ return true } // limit server, fast fix, improve later! // For "tower" peer, MUST include 6 surrounding ids. // REDUCED THIS TO 3 for temporary relay peer performance, towers still should list neighbors. - }); if(i > 1){ msg['><'] = to.join() } - }*/ // TOOD: COME BACK TO THIS LATER!!! IMPORTANT MESH STUFF!! - var raw = $(msg); // optimize by reusing put = the JSON.stringify from .hash? - /*if(u !== put){ - tmp = raw.indexOf(_, raw.indexOf('put')); - raw = raw.slice(0, tmp-1) + put + raw.slice(tmp + _.length + 1); - //raw = raw.replace('"'+ _ +'"', put); // NEVER USE THIS! ALSO NEVER DELETE IT TO NOT MAKE SAME MISTAKE! https://github.com/amark/gun/wiki/@$$ Heisenbug - }*/ - // TODO: PERF: tgif, CPU way too much on re-JSONifying ^ it. - /* - // NOTE TO SELF: Switch NTS to DAM now. - */ - if(meta && (raw||'').length < (999 * 99)){ meta.raw = raw } // HNPERF: If string too big, don't keep in memory. - return raw; - } - var $ = JSON.stringify, _ = ':])([:'; - - }()); - mesh.hi = function(peer){ var tmp = peer.wire || {}; if(peer.id){ diff --git a/test/common.js b/test/common.js index 1cf70726..3ffa07d8 100644 --- a/test/common.js +++ b/test/common.js @@ -63,6 +63,23 @@ describe('Gun', function(){ } ); */ + describe.only('YSON', function(){ + it('parse', function(){ + //var json = require('fs').readFileSync('./radix.json').toString(); + //var json = require('fs').readFileSync('./data.json').toString(); + //var json = require('fs').readFileSync('./big.json').toString(); + //var json = require('fs').readFileSync('./stats.json').toString(); + //var json = require('fs').readFileSync('./video.json').toString(); + }); + it('stringify', function(){ + //var obj = {"what\"lol": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}}; + var obj = {"what": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}}; + YSON.stringifyAsync(obj, function(err, text){ + console.log("done!", text); + }) + }); + }); + describe('Type Check', function(){ it('binary', function(){ expect(Gun.bi.is(false)).to.be(true); @@ -2839,6 +2856,28 @@ describe('Gun', function(){ }); }); + it.only('once put once', function(done){ + gun.get('opo').get('a').put('yay!'); + var ref = gun.get('opo').get('a'); + setTimeout(function(){ + ref.once(function(data){ + expect(data).to.be('yay!'); + + setTimeout(function(){ + gun.get('opo').get('a').put('z'); + + + setTimeout(function(){ + ref.once(function(data){ + expect(data).to.be('z'); + done(); + }); + }, 25); + }, 25); + }) + }, 25); + }); + it('get node after recursive field', function(done){ var bob = {age: 29, name: "Bob!"}; var cat = {name: "Fluffy", species: "kitty"}; diff --git a/test/panic/chat.js b/test/panic/chat.js new file mode 100644 index 00000000..49e000de --- /dev/null +++ b/test/panic/chat.js @@ -0,0 +1,175 @@ +var config = { + IP: require('ip').address(), + port: 8765, + servers: 1, + browsers: 3, + each: 1500, + wait: 1, + route: { + '/': __dirname + '/index.html', + '/gun.js': __dirname + '/../../gun.js', + '/jquery.js': __dirname + '/../../examples/jquery.js' + } +} + +/* +Assume we have 4 peers in a star topology, + +...B... +./.|.\. +A..C..D + +And they share a chat room with 10K messages. + +A - GET chat -> B (cache miss) -> C +*/ + +var panic = require('panic-server'); +panic.server().on('request', function(req, res){ // Static server + config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res); +}).listen(config.port); // Start panic server. + +// In order to tell the clients what to do, +// We need a way to reference all of them. +var clients = panic.clients; + +// Some of the clients may be NodeJS servers on different machines. +// PANIC manager is a nifty tool that lets us remotely spawn them. +var manager = require('panic-manager')(); +manager.start({ + clients: Array(config.servers).fill().map(function(u, i){ // Create a bunch of servers. + return { + type: 'node', + port: config.port + (i + 1) // They'll need unique ports to start their servers on, if we run the test on 1 machine. + } + }), + panic: 'http://' + config.IP + ':' + config.port // Auto-connect to our panic server. +}); + +// Now lets divide our clients into "servers" and "browsers". +var servers = clients.filter('Node.js'); +var browsers = clients.excluding(servers); +var alice = browsers.pluck(1); +var carl = browsers.excluding(alice).pluck(1); +var dave = browsers.excluding([alice, carl]).pluck(1); + +describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" server(s)!", function(){ + + // We'll have to manually launch the browsers, + // So lets up the timeout so we have time to do that. + this.timeout(5 * 60 * 1000); + + it("Servers have joined!", function(){ + // Alright, lets wait until enough gun server peers are connected. + return servers.atLeast(config.servers); + }); + + it("GUN has spawned!", function(){ + // Once they are, we need to actually spin up the gun server. + var tests = [], i = 0; + servers.each(function(client){ + // for each server peer, tell it to run this code: + tests.push(client.run(function(test){ + // NOTE: Despite the fact this LOOKS like we're in a closure... + // it is not! This code is actually getting run + // in a DIFFERENT machine or process! + var env = test.props; + // As a result, we have to manually pass it scope. + test.async(); + // Clean up from previous test. + try{ require('fs').unlinkSync(env.i+'data.json') }catch(e){} + var server = require('http').createServer(function(req, res){ + res.end("I am "+ env.i +"!"); + }); + // Launch the server and start gun! + var Gun = require('gun'); + // Attach the server to gun. + var gun = Gun({file: env.i+'data', web: server, axe: false, localStorage: false, radisk: false}); + server.listen(env.config.port + env.i, function(){ + // This server peer is now done with the test! + // It has successfully launched. + test.done(); + }); + }, {i: i += 1, config: config})); + }); + // NOW, this is very important: + // Do not proceed to the next test until + // every single server (in different machines/processes) + // have ALL successfully launched. + return Promise.all(tests); + }); + + it(config.browsers +" browser(s) have joined!", function(){ + console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + return browsers.atLeast(config.browsers); + }); + + it("Browsers initialized gun!", function(){ + var tests = [], i = 0; + browsers.each(function(client, id){ + tests.push(client.run(function(test){ + try{ localStorage.clear() }catch(e){} + try{ indexedDB.deleteDatabase('radata') }catch(e){} + var env = test.props; + var gun = Gun('http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun'); + window.gun = gun; + window.ref = gun.get('chat'); + }, {i: i += 1, config: config})); + }); + return Promise.all(tests); + }); + + it("Carl Create Chats", function(){ + return carl.run(function(test){ + console.log("I AM CARL"); + test.async(); + var rand = String.random || Gun.text.random; + var i = 10000, chat = {}, S = Gun.state(); + while(i--){ + Gun.state.ify(chat, rand(9), S, rand(200), 'chat'); + } + //window.chat = chat; + gun._.graph.chat = chat; + console.log(JSON.stringify(chat,null,2)); + test.done(); + }); + }); + + it("Alice Asks for Chat", function(){ + return alice.run(function(test){ + console.log("I AM ALICE"); + test.async(); + var i = 0, S = +new Date; + ref.map().once(function(v,k){ + S && console.log('first:', +new Date - S) || (S = null); + console.log(++i, "chat:",k,v); + }) + + setTimeout(function(){ + //test.done(); + },1000); + }); + }); + + /*it("Carl Recovers Chats", function(){ + return carl.run(function(test){ + console.log("... why not sending ...", window.chat); + test.async(); + gun.on('in', {'#': 'asdf', put: {chat: chat}}); + //test.done(); + }); + });*/ + + after("Everything shut down.", function(){ + // which is to shut down all the browsers. + browsers.run(function(){ + setTimeout(function(){ + location.reload(); + }, 15 * 1000); + }); + // And shut down all the servers. + return servers.run(function(){ + process.exit(); + }); + }); +}) \ No newline at end of file