test_tree-model-allocations-01.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. /**
  5. * Tests that the tree model calculates correct costs/percentages for
  6. * allocation frame nodes.
  7. */
  8. function run_test() {
  9. run_next_test();
  10. }
  11. add_task(function () {
  12. let { ThreadNode } = require("devtools/client/performance/modules/logic/tree-model");
  13. const { getProfileThreadFromAllocations } = require("devtools/shared/performance/recording-utils");
  14. let allocationData = getProfileThreadFromAllocations(TEST_DATA);
  15. let thread = new ThreadNode(allocationData, { startTime: 0, endTime: 1000 });
  16. /* eslint-disable max-len */
  17. /**
  18. * Values are in order according to:
  19. * +-------------+------------+-------------+-------------+------------------------------+
  20. * | Self Bytes | Self Count | Total Bytes | Total Count | Function |
  21. * +-------------+------------+-------------+-------------+------------------------------+
  22. * | 1790272 41% | 8307 17% | 1790372 42% | 8317 18% | V someFunc @ a.j:345:6 |
  23. * | 100 1% | 10 1% | 100 1% | 10 1% | > callerFunc @ b.j:765:34 |
  24. * +-------------+------------+-------------+-------------+------------------------------+
  25. */
  26. /* eslint-enable max-len */
  27. [
  28. [100, 10, 1, 33, 1000, 100, 3, 100, "x (A:1:2)", [
  29. [200, 20, 1, 33, 900, 90, 2, 66, "y (B:3:4)", [
  30. [700, 70, 1, 33, 700, 70, 1, 33, "z (C:5:6)"]
  31. ]]
  32. ]]
  33. ].forEach(compareFrameInfo(thread));
  34. });
  35. function compareFrameInfo(root, parent) {
  36. parent = parent || root;
  37. let fields = [
  38. "selfSize", "selfSizePercentage", "selfCount", "selfCountPercentage",
  39. "totalSize", "totalSizePercentage", "totalCount", "totalCountPercentage"
  40. ];
  41. return function (def) {
  42. let children;
  43. if (Array.isArray(def[def.length - 1])) {
  44. children = def.pop();
  45. }
  46. let name = def.pop();
  47. let expected = def;
  48. let node = getFrameNodePath(parent, name);
  49. let data = node.getInfo({ root, allocations: true });
  50. fields.forEach((field, i) => {
  51. let actual = data[field];
  52. if (/percentage/i.test(field)) {
  53. actual = Number.parseInt(actual, 10);
  54. }
  55. equal(actual, expected[i], `${name} has correct ${field}: ${expected[i]}`);
  56. });
  57. if (children) {
  58. children.forEach(compareFrameInfo(root, node));
  59. }
  60. };
  61. }
  62. var TEST_DATA = {
  63. sites: [1, 2, 3],
  64. timestamps: [150, 200, 250],
  65. sizes: [100, 200, 700],
  66. frames: [
  67. null, {
  68. source: "A",
  69. line: 1,
  70. column: 2,
  71. functionDisplayName: "x",
  72. parent: 0
  73. }, {
  74. source: "B",
  75. line: 3,
  76. column: 4,
  77. functionDisplayName: "y",
  78. parent: 1
  79. }, {
  80. source: "C",
  81. line: 5,
  82. column: 6,
  83. functionDisplayName: "z",
  84. parent: 2
  85. }
  86. ]
  87. };