test_criticalpath.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-FileCopyrightText: 2012-2026 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. // Run with: node test/test_criticalpath.js
  4. //
  5. // graphSlack decides which plugins the CPU panel marks as worth optimising, so a
  6. // wrong answer here is a wrong answer in front of the user. Everything else in the
  7. // panel is display.
  8. const assert = require('assert')
  9. const fs = require('fs')
  10. const path = require('path')
  11. // html/js/utils/*.js are plain globals, no module system
  12. eval(fs.readFileSync(path.join(__dirname, '..', 'html', 'js', 'utils', 'criticalpath.js'), 'utf8'))
  13. function check(name, weights, edges, critical, slack) {
  14. const got = graphSlack(weights, edges)
  15. assert.strictEqual(got.critical, critical, `${name}: critical ${got.critical}, expected ${critical}`)
  16. for (const n in slack) {
  17. assert.strictEqual(got.slack[n], slack[n], `${name}: slack[${n}] ${got.slack[n]}, expected ${slack[n]}`)
  18. }
  19. }
  20. // a plain chain: everything is critical, nothing has slack
  21. check('chain',
  22. {a: 10, b: 20, c: 5}, [['a','b'], ['b','c']],
  23. 35, {a: 0, b: 0, c: 0})
  24. // two branches off one source: only the slower branch is critical
  25. check('branches',
  26. {src: 0, slow: 30, fast: 10, sink: 5},
  27. [['src','slow'], ['src','fast'], ['slow','sink'], ['fast','sink']],
  28. 35, {slow: 0, fast: 20, sink: 0})
  29. // the expensive plugin is NOT the critical one -- the whole point of the panel
  30. check('hog off the critical path',
  31. {src: 0, hog: 40, a: 25, b: 25, sink: 0},
  32. [['src','hog'], ['src','a'], ['a','b'], ['hog','sink'], ['b','sink']],
  33. 50, {hog: 10, a: 0, b: 0})
  34. // disconnected plugins still get measured, and are their own path
  35. check('island',
  36. {a: 10, island: 3}, [],
  37. 10, {a: 0, island: 7})
  38. // nodes named only by an edge (hardware ports) count as free
  39. check('unweighted node',
  40. {a: 10}, [['capture', 'a'], ['a', 'playback']],
  41. 10, {capture: 0, a: 0, playback: 0})
  42. // a feedback loop must not hang or produce a negative slack
  43. const loop = graphSlack({a: 10, b: 20, c: 5},
  44. [['a','b'], ['b','c'], ['c','a']])
  45. assert.ok(loop.critical > 0, 'feedback loop should still yield a path')
  46. for (const n in loop.slack) {
  47. assert.ok(loop.slack[n] >= 0, `feedback loop gave node ${n} negative slack ${loop.slack[n]}`)
  48. }
  49. // A real 21-plugin board, captured off a Dwarf: per-client cpu in microseconds per
  50. // 2667us cycle, and the audio connection graph. This is the case the panel exists for
  51. // -- the biggest plugin is not the one holding up the cycle.
  52. const DWARF_US = {
  53. 'effect_79': 510, 'effect_5': 447, 'effect_71': 407, 'effect_50': 397, 'effect_27': 360,
  54. 'effect_70': 250, 'effect_54': 210, 'effect_11': 147, 'effect_3': 123, 'mod-host': 87,
  55. 'effect_81': 83, 'effect_4': 73, 'effect_30': 70, 'effect_44': 63, 'effect_83': 63,
  56. 'effect_84': 57, 'mod-monitor': 57, 'effect_41': 57, 'mod-peakmeter': 57, 'effect_82': 53,
  57. 'effect_85': 53, 'effect_77': 50, 'effect_31': 47, 'ttymidi': 47, 'mod-midi-merger': 47,
  58. 'effect_78': 37, 'mod-midi-broadcaster': 37,
  59. }
  60. const DWARF_EDGES = [
  61. // jack's capture and playback are one client but opposite ends of the cycle;
  62. // collapsing them into a single node would invent a loop through the whole graph
  63. ['capture','mod-host'], ['capture','mod-peakmeter'], ['mod-host','effect_3'],
  64. ['mod-monitor','playback'], ['mod-monitor','mod-peakmeter'], ['effect_82','effect_27'],
  65. ['effect_83','effect_5'], ['effect_44','effect_70'], ['effect_5','effect_71'],
  66. ['effect_84','effect_44'], ['effect_85','effect_79'], ['effect_71','mod-monitor'],
  67. ['effect_11','effect_70'], ['effect_70','mod-monitor'], ['effect_70','effect_78'],
  68. ['effect_54','effect_77'], ['effect_54','effect_50'], ['effect_27','effect_71'],
  69. ['effect_27','effect_83'], ['effect_27','effect_84'], ['effect_3','effect_4'],
  70. ['effect_4','effect_41'], ['effect_81','effect_70'], ['effect_79','effect_71'],
  71. ['effect_30','effect_81'], ['effect_30','effect_84'], ['effect_30','effect_82'],
  72. ['effect_30','effect_83'], ['effect_30','effect_85'], ['effect_30','effect_11'],
  73. ['effect_50','effect_31'], ['effect_50','effect_70'], ['effect_41','effect_54'],
  74. ]
  75. const dwarf = graphSlack(DWARF_US, DWARF_EDGES)
  76. // no node may end up with negative slack
  77. for (const n in dwarf.slack) {
  78. assert.ok(dwarf.slack[n] >= 0, `${n} got negative slack ${dwarf.slack[n]}`)
  79. }
  80. // jack reported ~63% DSP load while this was measured; the longest chain should land
  81. // near that, and never below the single most expensive plugin
  82. assert.ok(dwarf.critical > 1200 && dwarf.critical < 1900,
  83. `critical path ${dwarf.critical}us should land near the 1670us (63%) jack measured`)
  84. assert.strictEqual(dwarf.slack['effect_5'], 0, 'effect_5 should be on the critical path')
  85. assert.ok(dwarf.slack['effect_79'] > 300,
  86. `effect_79 is the top cpu consumer but off the critical path, got slack ${dwarf.slack['effect_79']}`)
  87. // The answer must not depend on the order plugins happen to arrive in. Breaking a
  88. // cycle at a different edge silently changes the whole ranking, which is exactly the
  89. // kind of bug nobody notices in a UI.
  90. const reversedWeights = {}
  91. Object.keys(DWARF_US).reverse().forEach(k => { reversedWeights[k] = DWARF_US[k] })
  92. const shuffled = graphSlack(reversedWeights, DWARF_EDGES.slice().reverse())
  93. assert.strictEqual(shuffled.critical, dwarf.critical,
  94. `critical path changed with input order: ${shuffled.critical} vs ${dwarf.critical}`)
  95. for (const n in dwarf.slack) {
  96. assert.strictEqual(shuffled.slack[n], dwarf.slack[n], `slack[${n}] changed with input order`)
  97. }
  98. console.log(` dwarf board: critical path ${dwarf.critical}us of a 2667us cycle ` +
  99. `(${(dwarf.critical / 2667 * 100).toFixed(0)}%, jack measured 63%)`)
  100. console.log('criticalpath: all assertions passed')