debug.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ;(function(){
  2. if('debug' !== process.env.GUN_ENV){ return }
  3. var db = {length: 0, hash: {}};
  4. console.log("start :)");
  5. global.DEBUG = 1;
  6. setInterval(function(){
  7. var print = '', tmp;
  8. var mem = process.memoryUsage();
  9. var used = mem.rss / 1024 / 1024;
  10. used = used.toFixed(1);
  11. print += used +' MB rss. ';
  12. var used = mem.heapTotal / 1024 / 1024;
  13. used = used.toFixed(1);
  14. print += used +' MB hT. ';
  15. var used = mem.heapUsed / 1024 / 1024;
  16. used = used.toFixed(1);
  17. print += used +' MB hU. ';
  18. if(db.root){
  19. db.concurrency = Object.keys(db.peers||{}).length;
  20. print += db.concurrency +' peers. ';
  21. db.nodes = Object.keys(db.root.graph||{}).length;
  22. print += db.nodes + ' nodes. ';
  23. if(db.count){ print += db.count + ' msgs. '}
  24. if(tmp = db.root.msgsLength){
  25. tmp = (tmp / 1024 / 1024).toFixed(2);
  26. print += tmp + ' length MB. ';
  27. }
  28. if(db.last){ print += '\n' + JSON.stringify(db.last, null, 2) }
  29. if(db.hash){
  30. print += '\nSome 100 Fast Hash Counts: \n' + JSON.stringify(db.hash, null, 2);
  31. var l = Object.keys(db.hash), i = l.length;
  32. if(i > 100){
  33. i = i - 100;
  34. Gun.list.map(l, function(k){
  35. if(--i <= 0){ return }
  36. delete db.hash[k];
  37. });
  38. }
  39. }
  40. }
  41. db.print = print;
  42. print = print.split('\n')[0];
  43. console.log(print);
  44. }, 2500);
  45. var Gun = require('../gun');
  46. Gun.on('opt', function(root){
  47. this.to.next(root);
  48. if(root.once){ return }
  49. console.log(">>>>>>>>>", root);
  50. root.debug = db;
  51. db.root = root;
  52. db.peers = root.opt.peers;
  53. db.count = 0;
  54. root.on('in', function(msg){
  55. this.to.next(msg);
  56. if(!msg.NTS){ db.last = msg }
  57. db.count++;
  58. var tmp = msg['##'];
  59. if(tmp && msg.put){
  60. if(!db.hash[tmp]){ db.hash[tmp] = [0, ''] }
  61. db.hash[tmp][0] = (db.hash[tmp][0] || 0) + 1;
  62. var preview = Object.keys(msg.put||{});
  63. db.hash[tmp][1] = preview.toString(', ').slice(0,500) + ' ...';
  64. }
  65. });
  66. })
  67. }());