Perf167Tests.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Round 167e: flaky tests, performance and robustness leftovers (docs/analise/REA_rodada167e.md)
  2. #include "core/CacheBudget.h"
  3. TEST (perf167_cache_budget_prunes_the_least_recently_used)
  4. {
  5. using lynx::cachebudget::Entry;
  6. // within the limit: nothing goes
  7. CHECK (lynx::cachebudget::toPrune ({ { 400, 1 }, { 500, 2 } }, 1000).empty());
  8. // above it: the oldest first, down to 3/4 of the limit (750): 1700 - 600 (t=1) - 300 (t=2) = 800 > 750, - 200 (t=3)
  9. const std::vector<Entry> files { { 200, 3 }, { 600, 1 }, { 400, 9 }, { 300, 2 }, { 200, 7 } };
  10. const auto gone = lynx::cachebudget::toPrune (files, 1000);
  11. CHECK (gone == (std::vector<std::size_t> { 1, 3, 0 }));
  12. std::int64_t left = 0;
  13. for (std::size_t i = 0; i < files.size(); ++i)
  14. if (std::find (gone.begin(), gone.end(), i) == gone.end())
  15. left += files[i].bytes;
  16. CHECK (left == 600 && left <= 750);
  17. // the most recently used one stays even when it alone is above the limit's 3/4
  18. const auto big = lynx::cachebudget::toPrune ({ { 900, 1 }, { 900, 5 } }, 1000);
  19. CHECK (big == (std::vector<std::size_t> { 0, 1 }) || big == (std::vector<std::size_t> { 0 }));
  20. CHECK (! big.empty() && big[0] == 0);
  21. }
  22. TEST (perf167_vfx_script_sees_the_song_stopped_in_an_export_tail)
  23. {
  24. using namespace lynx::vfx;
  25. // FL's Tutorial 3 pulse (a note every 1/8 while the song plays): FL's render has none after the song's end
  26. const char* src = R"(
  27. import flvfx as vfx
  28. def onTick():
  29. if vfx.context.isPlaying and vfx.context.ticks % 48 == 1:
  30. v = vfx.Voice()
  31. v.note = 60
  32. v.length = 24
  33. v.trigger()
  34. )";
  35. auto p = makeProcessor (Kind::script, vfxs165::state (src));
  36. const auto playing = vfxs165::play (*p, {}, 44100);
  37. CHECK (vfxs165::keysOn (playing).size() >= 3);
  38. script::setSongEnded (true);
  39. auto q = makeProcessor (Kind::script, vfxs165::state (src));
  40. const auto tail = vfxs165::play (*q, {}, 44100);
  41. script::setSongEnded (false);
  42. CHECK (vfxs165::keysOn (tail).empty());
  43. }
  44. TEST (perf167_stopped_vfx_script_costs_the_audio_thread_nothing)
  45. {
  46. using namespace lynx::vfx;
  47. // a script that stopped (here: it doesn't compile) is not handed blocks: no wait for the Python thread
  48. auto p = makeProcessor (Kind::script, vfxs165::state ("import flvfx as vfx\ndef onTick(:\n pass\n"));
  49. script::setRealtime (true);
  50. Transport t;
  51. t.sampleRate = 44100.0;
  52. t.playing = true;
  53. EventList out;
  54. p->process (nullptr, 0, 512, t, out); // (the first block learns that it answers nothing)
  55. std::this_thread::sleep_for (std::chrono::milliseconds (20));
  56. p->process (nullptr, 0, 512, t, out);
  57. // 5000 blocks: each was a round trip to the Python thread (its wake-up: 20-60 us, up to the 2 ms budget on a busy
  58. // machine), at least 100 ms in all
  59. const auto begin = std::chrono::steady_clock::now();
  60. for (int b = 0; b < 5000; ++b)
  61. {
  62. auto e = vfx164::on (60);
  63. p->process (&e, 1, 512, t, out);
  64. }
  65. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - begin).count();
  66. script::setRealtime (false);
  67. std::printf (" stopped script: 5000 blocks in %.3f ms\n", ms);
  68. CHECK (out.size() == 0);
  69. CHECK (ms < 60.0); // (well under 1 ms in all now)
  70. }
  71. TEST (perf167_reeverb2_first_block_and_predelay_changes_do_not_allocate)
  72. {
  73. // the heavy-project profile (renderwav LYNX_ALLOCS=1, FL's demo "Thief"): every Reeverb 2 allocated its delay lines
  74. // in its first audio block (prepared lazily there) and a predelay change re-allocates them (FL's FUN_006d1d00)
  75. auto fx = lynx::makeReeverb2();
  76. fx->prepare (44100.0);
  77. lynx::reeverb2::Settings s;
  78. lynx::EffectParams params {};
  79. lynx::reeverb2::toParams (s, params);
  80. std::vector<float> l (512, 0.1f), r (512, -0.1f);
  81. rt95::allocs = 0;
  82. rt95::countAllocs = true;
  83. fx->process (l.data(), r.data(), 512, params, 120.0);
  84. for (int pre = 0; pre <= 384; pre += 64) // (the predelay knob from 0 to its maximum, 1 s)
  85. {
  86. s.p[2] = pre;
  87. lynx::reeverb2::toParams (s, params);
  88. fx->process (l.data(), r.data(), 512, params, 120.0);
  89. }
  90. s.tempoPredelay = true; // (tempo-synced: 2 s at 60 BPM)
  91. lynx::reeverb2::toParams (s, params);
  92. fx->process (l.data(), r.data(), 512, params, 60.0);
  93. rt95::countAllocs = false;
  94. CHECK (rt95::allocs.load() == 0);
  95. if (rt95::allocs.load() != 0)
  96. std::printf (" %ld allocations\n", rt95::allocs.load());
  97. }
  98. // Harmless: a new channel builds its effects rack on the UI thread (settleNew), so its first note and block allocate
  99. // nothing on the audio thread (before: the rack's chorus / delay / reverb were made at the first note)
  100. TEST (perf167_new_harmless_channel_does_not_allocate_at_its_first_note)
  101. {
  102. lynx::ChannelDef channel;
  103. channel.instrument = lynx::InstrumentKind::harmless;
  104. auto inst = lynx::makeHarmlessInstrument();
  105. inst->prepare (44100.0);
  106. channel.modelParams.assign ((std::size_t) lynx::harmlessParamCount, 0.0f);
  107. // the rack's chorus, delay and reverb on (all other values 0)
  108. channel.modelParams[90] = 1.0f;
  109. channel.modelParams[91] = 500.0f;
  110. channel.modelParams[92] = 1.0f;
  111. channel.modelParams[96] = 1.0f;
  112. channel.modelParams[97] = 500.0f;
  113. inst->configure (channel, 120.0);
  114. inst->settleNew();
  115. std::vector<float> l (512, 0.0f), r (512, 0.0f);
  116. rt95::allocs = 0;
  117. rt95::countAllocs = true;
  118. inst->noteOn (60, 0.8f);
  119. for (int b = 0; b < 4; ++b)
  120. inst->render (l.data(), r.data(), 512, 1.0f, 1.0f);
  121. rt95::countAllocs = false;
  122. CHECK (rt95::allocs.load() == 0);
  123. if (rt95::allocs.load() != 0)
  124. std::printf (" %ld allocations\n", rt95::allocs.load());
  125. }
  126. // Fruity Reeverb, Stereo Enhancer and Fruity Delay 2 make their buffers in prepare (UI thread): the first block, a
  127. // delay change and a tempo change down to 60 BPM allocate nothing on the audio thread
  128. TEST (perf167_reeverb1_stereo_enhancer_delay2_do_not_allocate_on_the_audio_thread)
  129. {
  130. for (const auto kind : { lynx::EffectKind::reeverb1, lynx::EffectKind::stereoEnhancer, lynx::EffectKind::fruityDelay2 })
  131. {
  132. auto fx = lynx::makeEffect (kind);
  133. fx->prepare (44100.0);
  134. auto slot = lynx::makeEffectSlot (kind);
  135. std::vector<float> l (512, 0.1f), r (512, -0.1f);
  136. rt95::allocs = 0;
  137. rt95::countAllocs = true;
  138. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  139. for (int k = 0; k < 8; ++k) // every control to its maximum, one by one
  140. {
  141. slot.params[(std::size_t) k] = 1.0f;
  142. fx->process (l.data(), r.data(), 512, slot.params, 120.0);
  143. }
  144. fx->process (l.data(), r.data(), 512, slot.params, 60.0);
  145. fx->reset();
  146. fx->process (l.data(), r.data(), 512, slot.params, 90.0);
  147. rt95::countAllocs = false;
  148. CHECK (rt95::allocs.load() == 0);
  149. if (rt95::allocs.load() != 0)
  150. std::printf (" effect %d: %ld allocations\n", (int) kind, rt95::allocs.load());
  151. }
  152. }
  153. // Fruity Delay 2 made in prepare and restarted at the first block sounds as one made at the first block (before)
  154. TEST (perf167_delay2_restart_matches_a_new_engine)
  155. {
  156. lynx::fldelay::Delay2State s;
  157. s.v = { 20, 100, 90, 80, 300, -200, 1, 90 };
  158. lynx::fldelay::Delay2State other;
  159. other.v = lynx::fldelay::delay2Default;
  160. lynx::fldelay::Delay2Engine fresh (s, 48000.0, 97.0, 512, true);
  161. lynx::fldelay::Delay2Engine reused (other, 48000.0, 120.0, 512, true);
  162. reused.reserve (60.0);
  163. std::vector<float> junkL (512, 0.3f), junkR (512, 0.2f);
  164. reused.process (junkL.data(), junkR.data(), 512); // (it played something before the restart)
  165. reused.restart (s, 97.0, true);
  166. double diff = 0.0;
  167. for (int b = 0; b < 40; ++b)
  168. {
  169. std::vector<float> l1 (512), r1 (512);
  170. for (int i = 0; i < 512; ++i)
  171. l1[(std::size_t) i] = std::sin (0.01 * (b * 512 + i)) * (b < 4 ? 1.0f : 0.0f), r1[(std::size_t) i] = -0.5f * l1[(std::size_t) i];
  172. auto l2 = l1, r2 = r1;
  173. fresh.process (l1.data(), r1.data(), 512);
  174. reused.process (l2.data(), r2.data(), 512);
  175. for (int i = 0; i < 512; ++i)
  176. diff = std::max (diff, (double) std::fabs (l1[(std::size_t) i] - l2[(std::size_t) i]) + std::fabs (r1[(std::size_t) i] - r2[(std::size_t) i]));
  177. }
  178. CHECK (diff == 0.0);
  179. }