test_profiler_bufferstatus.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. /**
  5. * Tests if the profiler actor returns its buffer status via getBufferInfo.
  6. */
  7. const Profiler = Cc["@mozilla.org/tools/profiler;1"].getService(Ci.nsIProfiler);
  8. const INITIAL_WAIT_TIME = 100; // ms
  9. const MAX_WAIT_TIME = 20000; // ms
  10. const MAX_PROFILER_ENTRIES = 10000000;
  11. function run_test()
  12. {
  13. // Ensure the profiler is not running when the test starts (it could
  14. // happen if the MOZ_PROFILER_STARTUP environment variable is set).
  15. Profiler.StopProfiler();
  16. get_chrome_actors((client, form) => {
  17. let actor = form.profilerActor;
  18. check_empty_buffer(client, actor, () => {
  19. activate_profiler(client, actor, startTime => {
  20. wait_for_samples(client, actor, () => {
  21. check_buffer(client, actor, () => {
  22. deactivate_profiler(client, actor, () => {
  23. client.close().then(do_test_finished);
  24. });
  25. });
  26. });
  27. });
  28. });
  29. });
  30. do_test_pending();
  31. }
  32. function check_buffer(client, actor, callback)
  33. {
  34. client.request({ to: actor, type: "isActive" }, response => {
  35. do_check_true(typeof response.position === "number");
  36. do_check_true(typeof response.totalSize === "number");
  37. do_check_true(typeof response.generation === "number");
  38. do_check_true(response.position > 0 && response.position < response.totalSize);
  39. do_check_true(response.totalSize === MAX_PROFILER_ENTRIES);
  40. // There's no way we'll fill the buffer in this test.
  41. do_check_true(response.generation === 0);
  42. callback();
  43. });
  44. }
  45. function check_empty_buffer(client, actor, callback)
  46. {
  47. client.request({ to: actor, type: "isActive" }, response => {
  48. do_check_false(Profiler.IsActive());
  49. do_check_false(response.isActive);
  50. do_check_true(response.position === void 0);
  51. do_check_true(response.totalSize === void 0);
  52. do_check_true(response.generation === void 0);
  53. do_check_false(response.isActive);
  54. do_check_eq(response.currentTime, undefined);
  55. calback();
  56. });
  57. }
  58. function activate_profiler(client, actor, callback)
  59. {
  60. client.request({ to: actor, type: "startProfiler", entries: MAX_PROFILER_ENTRIES }, response => {
  61. do_check_true(response.started);
  62. client.request({ to: actor, type: "isActive" }, response => {
  63. do_check_true(response.isActive);
  64. callback(response.currentTime);
  65. });
  66. });
  67. }
  68. function deactivate_profiler(client, actor, callback)
  69. {
  70. client.request({ to: actor, type: "stopProfiler" }, response => {
  71. do_check_false(response.started);
  72. client.request({ to: actor, type: "isActive" }, response => {
  73. do_check_false(response.isActive);
  74. callback();
  75. });
  76. });
  77. }
  78. function wait_for_samples(client, actor, callback)
  79. {
  80. function attempt(delay)
  81. {
  82. // No idea why, but Components.stack.sourceLine returns null.
  83. let funcLine = Components.stack.lineNumber - 3;
  84. // Spin for the requested time, then take a sample.
  85. let start = Date.now();
  86. let stack;
  87. do_print("Attempt: delay = " + delay);
  88. while (Date.now() - start < delay) { stack = Components.stack; }
  89. do_print("Attempt: finished waiting.");
  90. client.request({ to: actor, type: "getProfile" }, response => {
  91. // At this point, we may or may not have samples, depending on
  92. // whether the spin loop above has given the profiler enough time
  93. // to get started.
  94. if (response.profile.threads[0].samples.length == 0) {
  95. if (delay < MAX_WAIT_TIME) {
  96. // Double the spin-wait time and try again.
  97. do_print("Attempt: no samples, going around again.");
  98. return attempt(delay * 2);
  99. } else {
  100. // We've waited long enough, so just fail.
  101. do_print("Attempt: waited a long time, but no samples were collected.");
  102. do_print("Giving up.");
  103. do_check_true(false);
  104. return;
  105. }
  106. }
  107. callback();
  108. });
  109. }
  110. // Start off with a 100 millisecond delay.
  111. attempt(INITIAL_WAIT_TIME);
  112. }