HeapSnapshot.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var Class = require('./Class');
  2. var toBool = require('./toBool');
  3. var camelCase = require('./camelCase');
  4. var LinkedList = require('./LinkedList');
  5. var isStr = require('./isStr');
  6. var each = require('./each');
  7. var map = require('./map');
  8. exports = Class({
  9. initialize: function HeapSnapshot(profile) {
  10. if (isStr(profile)) {
  11. profile = JSON.parse(profile);
  12. }
  13. this.nodes = new LinkedList();
  14. this.edges = new LinkedList();
  15. var snapshot = profile.snapshot;
  16. var meta = snapshot.meta;
  17. this.nodeFields = map(meta.node_fields, camelCase);
  18. this.nodeTypes = meta.node_types[this.nodeFields.indexOf('type')];
  19. this.edgeFields = map(meta.edge_fields, camelCase);
  20. this.edgeTypes = meta.edge_types[this.edgeFields.indexOf('type')];
  21. this._init(profile);
  22. },
  23. _init: function(profile) {
  24. var _this = this;
  25. var nodes = profile.nodes,
  26. edges = profile.edges,
  27. strings = profile.strings;
  28. var nodeFields = this.nodeFields,
  29. edgeFields = this.edgeFields;
  30. var curEdgeIdx = 0;
  31. var nodeFieldCount = nodeFields.length;
  32. var edgeFieldCount = edgeFields.length;
  33. var nodeMap = {};
  34. for (var i = 0, len = nodes.length; i < len; i += nodeFieldCount) {
  35. var node = new Node(this);
  36. node.init(nodes.slice(i, i + nodeFieldCount), strings);
  37. this.nodes.push(node);
  38. nodeMap[i] = node;
  39. }
  40. this.nodes.forEach(function(node) {
  41. var edgeCount = node.edgeCount;
  42. delete node.edgeCount;
  43. var maxEdgeIdx = curEdgeIdx + edgeCount * edgeFieldCount;
  44. for (var _i = curEdgeIdx; _i < maxEdgeIdx; _i += edgeFieldCount) {
  45. var edge = new Edge(_this, node);
  46. edge.init(
  47. edges.slice(_i, _i + edgeFieldCount),
  48. strings,
  49. nodeMap
  50. );
  51. _this.edges.push(edge);
  52. }
  53. curEdgeIdx = maxEdgeIdx;
  54. });
  55. }
  56. });
  57. var Node = Class({
  58. initialize: function Node(heapSnapshot) {
  59. this._heapSnapshot = heapSnapshot;
  60. },
  61. init: function(fields, strings) {
  62. var _this2 = this;
  63. var heapSnapshot = this._heapSnapshot;
  64. var nodeFields = heapSnapshot.nodeFields,
  65. nodeTypes = heapSnapshot.nodeTypes;
  66. each(nodeFields, function(field, idx) {
  67. var val = fields[idx];
  68. switch (field) {
  69. case 'name':
  70. val = strings[val];
  71. break;
  72. case 'detachedness':
  73. val = toBool(val);
  74. break;
  75. case 'type':
  76. val = nodeTypes[val];
  77. break;
  78. }
  79. _this2[field] = val;
  80. });
  81. }
  82. });
  83. var Edge = Class({
  84. initialize: function Edge(heapSnapshot, fromNode) {
  85. this._heapSnapshot = heapSnapshot;
  86. this.fromNode = fromNode;
  87. },
  88. init: function(fields, strings, nodeMap) {
  89. var _this3 = this;
  90. var heapSnapshot = this._heapSnapshot;
  91. var edgeFields = heapSnapshot.edgeFields,
  92. edgeTypes = heapSnapshot.edgeTypes;
  93. each(edgeFields, function(field, idx) {
  94. var val = fields[idx];
  95. switch (field) {
  96. case 'nameOrIndex':
  97. val = strings[val];
  98. break;
  99. case 'type':
  100. val = edgeTypes[val];
  101. break;
  102. case 'toNode':
  103. val = nodeMap[val];
  104. break;
  105. }
  106. _this3[field] = val;
  107. });
  108. }
  109. });
  110. module.exports = exports;