فهرست منبع

Merge branch 'feature/perf165p'

Ka1-S 1 ماه پیش
والد
کامیت
2f520ca61c

+ 125 - 1
src/Main.cpp

@@ -80,6 +80,7 @@
 #include "ui/ThemeSettings.h"
 #include "ui/WaveformView.h"
 #include "ui/WindowOwner.h"
+#include "ui/Toolbar.h"
 #include "ui/ToolWindow.h"
 #include "platform/CrashGuard.h"
 #include "ui/CrashReporter.h"
@@ -949,6 +950,18 @@ public:
         // Double-clicking a .lynx/.flp opens LYNX with the file as an argument.
         if (args.size() > 0 && juce::File::isAbsolutePath (args[0].unquoted()))
             fileToOpen = juce::File (args[0].unquoted());
+        // LYNX --opentime project.(flp|lynx)|- report.txt [idle seconds] (round 165i): the normal start-up (splash, audio
+        // device, window) opens the project, waits until it is loaded and LYNX has gone quiet, measures the idle CPU and
+        // memory, writes the times and quits normally (a benchmark that killed LYNX left "closed unexpectedly" behind)
+        if (args.contains ("--opentime"))
+        {
+            const auto i = args.indexOf ("--opentime");
+            const auto project = args[i + 1].unquoted();
+            fileToOpen = project != "-" && juce::File::isAbsolutePath (project) ? juce::File (project) : juce::File();
+            openTimeReport = juce::File (args[i + 2].unquoted());
+            if (args.size() > i + 3 && ! args[i + 3].startsWith ("--"))
+                openTimeIdleSeconds = juce::jlimit (1, 120, args[i + 3].getIntValue());
+        }
 
         if (! startupOptions.splash)
         {
@@ -959,6 +972,7 @@ public:
             startupTrace.mark ("window revealed");
             openStartupFile();
             startupTrace.mark ("startup file opened");
+            startOpenTimeProbe();
             juce::MessageManager::callAsync ([] { startupTrace.mark ("first message loop turn"); });
             return;
         }
@@ -998,9 +1012,12 @@ public:
                     showUncleanNotice();
                     openStartupFile();
                     startupTrace.mark ("startup file opened");
+                    startOpenTimeProbe();
                     juce::MessageManager::callAsync ([] { startupTrace.mark ("first message loop turn"); });
                     // first run: language, audio, plugin folders (FL Studio's included) and a background scan
-                    if (! lynx::setupWizardDone())
+                    if (! lynx::setupWizardDone() && lynx::platform::testMode())
+                        lynx::platform::testModeLog ("setup wizard");
+                    else if (! lynx::setupWizardDone())
                         mainWindow->showSetupWizard();
                 });
             });
@@ -1047,6 +1064,12 @@ public:
     // Settings › General › "Crash reports" says (round 159), ask whether to send a report, send it unasked, or not
     void showUncleanNotice()
     {
+        if (lynx::platform::testMode()) // LYNX_TEST_MODE: no prompt, no report sent (written to test-mode.log)
+        {
+            if (uncleanLastStep.isNotEmpty())
+                lynx::platform::testModeLog ("\"closed unexpectedly\" notice (last step: " + uncleanLastStep + ")");
+            return;
+        }
         if (uncleanLastStep.isEmpty())
         {
             lynx::crashreporter::retryPending(); // (reports an earlier run could not send: once more)
@@ -1111,6 +1134,105 @@ public:
         juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, lynx::tr ("LYNX closed unexpectedly"), message);
     }
 
+    // --opentime: after the start-up file was handed to the window
+    void startOpenTimeProbe()
+    {
+        if (openTimeReport == juce::File())
+            return;
+
+        struct Probe final : juce::Timer
+        {
+            LynxApplication& app;
+            juce::File report, project;
+            int idleSeconds;
+            int phase = 0; // 0 loading, 1 settling, 2 idle measurement
+            double revealedS = 0.0, loadedS = -1.0, settledS = -1.0, cpuAtLoaded = 0.0;
+            double lastCpu = 0.0, lastT = 0.0, quietSince = -1.0, idleCpu0 = 0.0, idleT0 = 0.0;
+
+            Probe (LynxApplication& a, juce::File r, juce::File p, int idle)
+                : app (a), report (std::move (r)), project (std::move (p)), idleSeconds (idle)
+            {
+                revealedS = startupTrace.seconds();
+                startTimer (250);
+            }
+
+            bool loaded() const
+            {
+                if (project == juce::File())
+                    return true;
+                return app.mainWindow != nullptr
+                    && app.mainWindow->getName().containsIgnoreCase (project.getFileNameWithoutExtension());
+            }
+
+            void finish (const juce::String& result)
+            {
+                stopTimer();
+                const auto now = startupTrace.seconds();
+                const auto cpu = lynx::processCpuSeconds();
+                juce::String text;
+                text << "project=" << (project == juce::File() ? juce::String ("-") : project.getFullPathName()) << "\n"
+                     << "window_s=" << juce::String (revealedS, 3) << "\n"
+                     << "loaded_s=" << juce::String (loadedS, 3) << "\n"
+                     << "settled_s=" << juce::String (settledS, 3) << "\n"
+                     << "cpu_at_loaded_s=" << juce::String (cpuAtLoaded, 3) << "\n"
+                     << "idle_s=" << juce::String (now - idleT0, 2) << "\n"
+                     << "idle_cpu_pct=" << juce::String (phase == 2 && now > idleT0 ? 100.0 * (cpu - idleCpu0) / (now - idleT0) : -1.0, 1) << "\n"
+                     << "working_set_mb=" << juce::String ((double) lynx::toolbar::PerformanceMeter::processMemoryBytes() / 1048576.0, 0) << "\n"
+                     << "cpu_total_s=" << juce::String (cpu, 3) << "\n"
+                     << "result=" << result << "\n";
+                report.replaceWithText (text);
+                app.setApplicationReturnValue (result == "OK" ? 0 : 1);
+                app.quit();
+            }
+
+            void timerCallback() override
+            {
+                const auto now = startupTrace.seconds();
+                const auto cpu = lynx::processCpuSeconds();
+                if (phase == 0)
+                {
+                    if (loaded())
+                    {
+                        loadedS = now;
+                        cpuAtLoaded = cpu;
+                        phase = 1;
+                        lastCpu = cpu;
+                        lastT = now;
+                    }
+                    else if (now - revealedS > 240.0)
+                        finish ("TIMEOUT loading");
+                    return;
+                }
+                if (phase == 1)
+                {
+                    // quiet = under 10 % of one core for 1.5 s (the harness's rule), at most 30 s after loading
+                    const auto use = now > lastT ? 100.0 * (cpu - lastCpu) / (now - lastT) : 0.0;
+                    lastCpu = cpu;
+                    lastT = now;
+                    if (use < 10.0)
+                    {
+                        if (quietSince < 0.0)
+                            quietSince = now - 0.25;
+                        if (now - quietSince >= 1.5)
+                            settledS = quietSince;
+                    }
+                    else
+                        quietSince = -1.0;
+                    if (settledS >= 0.0 || now - loadedS > 30.0)
+                    {
+                        phase = 2;
+                        idleCpu0 = cpu;
+                        idleT0 = now;
+                    }
+                    return;
+                }
+                if (now - idleT0 >= (double) idleSeconds)
+                    finish ("OK");
+            }
+        };
+        perf = std::make_unique<Probe> (*this, openTimeReport, fileToOpen, openTimeIdleSeconds);
+    }
+
     void openStartupFile()
     {
         if (fileToOpen != juce::File() && mainWindow != nullptr)
@@ -6445,6 +6567,8 @@ private:
     double splashDuration = lynx::SplashScreen::minimumDuration; // seconds of its clock (round 121: from the process start)
     lynx::StartupOptions startupOptions;
     juce::File fileToOpen;
+    juce::File openTimeReport; // --opentime
+    int openTimeIdleSeconds = 10;
 };
 
 START_JUCE_APPLICATION (LynxApplication)

+ 54 - 0
src/core/TestProfile.h

@@ -0,0 +1,54 @@
+#pragma once
+
+// Test profile and test mode (round 165i). Benchmarks and automated GUI runs must not touch the user's real LYNX
+// profile, and nothing may wait for a click on the user's desktop:
+//   LYNX_PROFILE_DIR=<absolute folder>  every user-data path of LYNX goes there: settings, autosave, sessions and crash
+//                                       markers / history, plugin list and scan files, caches (waveforms, PCM and
+//                                       time-stretch caches), and the Documents\LYNX folders (templates, mixer states…)
+//   LYNX_TEST_MODE=1                    no prompts: the "closed unexpectedly" notice / crash-report question, the
+//                                       Missing files dialog, the setup wizard, the project info shown on open and the
+//                                       background plugin scan at start are skipped and written to test-mode.log
+// The parsing lives here (no JUCE) so the tests can check it; platform/Paths.cpp applies it.
+
+#include <cctype>
+#include <optional>
+#include <string>
+
+namespace lynx::testprofile
+{
+// The profile folder named by LYNX_PROFILE_DIR's value: only an absolute path counts (a relative one would depend on
+// the working directory); trailing separators are dropped.
+inline std::optional<std::string> profileDirFrom (const char* value)
+{
+    if (value == nullptr)
+        return std::nullopt;
+    std::string s (value);
+    while (! s.empty() && std::isspace ((unsigned char) s.back()))
+        s.pop_back();
+    std::size_t start = 0;
+    while (start < s.size() && std::isspace ((unsigned char) s[start]))
+        ++start;
+    s = s.substr (start);
+    if (s.size() >= 2 && s.front() == '"' && s.back() == '"')
+        s = s.substr (1, s.size() - 2);
+    const auto absolute = (! s.empty() && (s[0] == '/' || s[0] == '\\'))
+                       || (s.size() >= 3 && std::isalpha ((unsigned char) s[0]) && s[1] == ':' && (s[2] == '\\' || s[2] == '/'));
+    if (! absolute)
+        return std::nullopt;
+    while (s.size() > 1 && (s.back() == '/' || s.back() == '\\') && ! (s.size() == 3 && s[1] == ':'))
+        s.pop_back();
+    return s;
+}
+
+// LYNX_TEST_MODE's value: on for anything but empty, "0", "false", "no", "off"
+inline bool testModeFrom (const char* value)
+{
+    if (value == nullptr)
+        return false;
+    std::string s;
+    for (const char* p = value; *p != 0; ++p)
+        if (! std::isspace ((unsigned char) *p))
+            s += (char) std::tolower ((unsigned char) *p);
+    return ! (s.empty() || s == "0" || s == "false" || s == "no" || s == "off");
+}
+} // namespace lynx::testprofile

+ 2 - 1
src/engine/AudioLogger.cpp

@@ -1,3 +1,4 @@
+#include "platform/Paths.h"
 #include "engine/AudioLogger.h"
 
 namespace lynx
@@ -11,7 +12,7 @@ AudioLogger::~AudioLogger()
 
 juce::File AudioLogger::defaultFolder()
 {
-    return juce::File::getSpecialLocation (juce::File::userDocumentsDirectory).getChildFile ("LYNX").getChildFile ("Audio logger");
+    return lynx::platform::documentsFolder().getChildFile ("LYNX").getChildFile ("Audio logger");
 }
 
 bool AudioLogger::start (const juce::File& folder, double sampleRate)

+ 2 - 1
src/engine/ProjectPackage.cpp

@@ -1,3 +1,4 @@
+#include "platform/Paths.h"
 #include "engine/ProjectPackage.h"
 
 #include "core/MissingFiles.h"
@@ -146,6 +147,6 @@ juce::File extractZip (const juce::File& zipFile, const juce::File& destinationR
 
 juce::File defaultExtractRoot()
 {
-    return juce::File::getSpecialLocation (juce::File::userDocumentsDirectory).getChildFile ("LYNX/Projects/Unpacked");
+    return lynx::platform::documentsFolder().getChildFile ("LYNX/Projects/Unpacked");
 }
 } // namespace lynx::ProjectPackage

+ 3 - 1
src/engine/SampleLibrary.cpp

@@ -215,7 +215,9 @@ std::shared_ptr<const SampleData> SampleLibrary::loadStretched (const juce::Stri
 
 juce::File SampleLibrary::cacheFolder()
 {
-    auto folder = juce::File::getSpecialLocation (juce::File::tempDirectory).getChildFile ("LYNX-cache");
+    // (under a test profile, LYNX_PROFILE_DIR: inside it)
+    const auto profile = platform::profileOverride();
+    auto folder = (profile != juce::File() ? profile.getChildFile ("cache") : juce::File::getSpecialLocation (juce::File::tempDirectory)).getChildFile ("LYNX-cache");
     folder.createDirectory();
     return folder;
 }

+ 49 - 0
src/platform/Paths.cpp

@@ -1,6 +1,7 @@
 #include "platform/Paths.h"
 
 #include "core/FlpImport.h"
+#include "core/TestProfile.h"
 
 #include <iterator>
 #include <string>
@@ -30,8 +31,54 @@ juce::File xdgFolder (const char* variable, const char* fallback)
 #endif
 } // namespace
 
+namespace
+{
+// (JUCE's reader: the wide environment on Windows, so a non-ASCII folder survives; "" when unset)
+std::string envValue (const char* name)
+{
+    return juce::SystemStats::getEnvironmentVariable (name, {}).toStdString();
+}
+} // namespace
+
+juce::File profileOverride()
+{
+    static const juce::File folder = [] {
+        const auto dir = testprofile::profileDirFrom (envValue ("LYNX_PROFILE_DIR").c_str());
+        if (! dir)
+            return juce::File();
+        juce::File f (juce::String::fromUTF8 (dir->c_str()));
+        f.createDirectory();
+        return f;
+    }();
+    return folder;
+}
+
+bool testMode()
+{
+    static const bool on = testprofile::testModeFrom (envValue ("LYNX_TEST_MODE").c_str());
+    return on;
+}
+
+void testModeLog (const juce::String& what)
+{
+    settingsFile ("test-mode.log").appendText (juce::Time::getCurrentTime().toISO8601 (true) + " | skipped: " + what + "\n");
+}
+
+juce::File documentsFolder()
+{
+    if (const auto profile = profileOverride(); profile != juce::File())
+        return profile.getChildFile ("Documents");
+    return juce::File::getSpecialLocation (juce::File::userDocumentsDirectory);
+}
+
 juce::File settingsFolder()
 {
+    if (const auto profile = profileOverride(); profile != juce::File())
+    {
+        auto folder = profile.getChildFile ("LYNX");
+        folder.createDirectory();
+        return folder;
+    }
    #if JUCE_WINDOWS
     auto folder = juce::File::getSpecialLocation (juce::File::userApplicationDataDirectory).getChildFile ("LYNX");
    #elif JUCE_MAC
@@ -50,6 +97,8 @@ juce::File settingsFile (const juce::String& relativePath)
 
 juce::File cacheFolder (const juce::String& name)
 {
+    if (const auto profile = profileOverride(); profile != juce::File())
+        return profile.getChildFile ("cache").getChildFile (name);
    #if JUCE_WINDOWS
     return settingsFolder().getChildFile (name);
    #elif JUCE_MAC

+ 9 - 0
src/platform/Paths.h

@@ -13,6 +13,15 @@ namespace lynx::platform
 //   Linux    $XDG_CONFIG_HOME/LYNX (default ~/.config/LYNX)
 juce::File settingsFolder();
 
+// LYNX_PROFILE_DIR (core/TestProfile.h): the folder that replaces every user-data location, or an empty File.
+juce::File profileOverride();
+// LYNX_TEST_MODE: no prompts (they are logged to test-mode.log in the settings folder instead).
+bool testMode();
+void testModeLog (const juce::String& what);
+
+// The user's Documents folder for what LYNX writes there (Documents/LYNX/...): <profile>/Documents under a test profile.
+juce::File documentsFolder();
+
 // A file or sub-folder inside the settings folder, e.g. settingsFile ("autosave").
 juce::File settingsFile (const juce::String& relativePath);
 

+ 12 - 3
src/ui/MainComponent.cpp

@@ -497,7 +497,9 @@ MainComponent::MainComponent (AudioEngine& e, bool restoreUserSettings)
 
     // new or updated plugins since the last run: an incremental scan in the background (unchanged files are
     // skipped), a few seconds after start so it doesn't compete with loading; the first run asks in the wizard
-    if (persistent && setupWizardDone() && backgroundScanAtStart())
+    if (persistent && setupWizardDone() && backgroundScanAtStart() && platform::testMode())
+        platform::testModeLog ("background plugin scan at start");
+    else if (persistent && setupWizardDone() && backgroundScanAtStart())
         juce::Timer::callAfterDelay (4000, [safe = juce::Component::SafePointer<MainComponent> (this)] {
             if (safe != nullptr)
                 safe->engine.getPluginHost().startBackgroundScan();
@@ -3284,7 +3286,7 @@ void MainComponent::saveToNewProjectFolder()
     // recordings go to <name>/Audio/Recorded). This session's takes still in the global recordings folder move along
     // (copied there, the project uses the copies; the originals are then offered for deletion as unused).
     const auto start = currentFile != juce::File() ? currentFile.getParentDirectory()
-                                                   : juce::File::getSpecialLocation (juce::File::userDocumentsDirectory).getChildFile ("LYNX/Projects");
+                                                   : lynx::platform::documentsFolder().getChildFile ("LYNX/Projects");
     start.createDirectory();
     fileChooser = std::make_unique<juce::FileChooser> (tr ("Save to new project folder"),
                                                        start.getChildFile (projectName.isNotEmpty() ? juce::File::createLegalFileName (projectName) : juce::String ("untitled")),
@@ -3646,7 +3648,9 @@ void MainComponent::openLynxProject (const juce::File& file)
     if (persistent)
     {
         showMissingFiles (true);
-        if (engine.getSong().info.showOnOpen)
+        if (engine.getSong().info.showOnOpen && platform::testMode())
+            platform::testModeLog ("project info shown on open");
+        else if (engine.getSong().info.showOnOpen)
             openSettings (1);
     }
 }
@@ -3772,6 +3776,11 @@ void MainComponent::showShortcutManager()
 
 void MainComponent::showMissingFiles (bool afterOpening)
 {
+    if (afterOpening && platform::testMode()) // (LYNX_TEST_MODE: nothing waits for a click)
+    {
+        platform::testModeLog ("Missing files dialog after opening " + projectName);
+        return;
+    }
     MissingFilesInfo info;
     info.projectName = projectName;
     const auto fromFlp = importedFlpFile.existsAsFile() && currentFile == juce::File();

+ 2 - 1
src/ui/MixerComponent.cpp

@@ -1,3 +1,4 @@
+#include "platform/Paths.h"
 #include "ui/ColourPicker.h"
 #include "ui/MixerComponent.h"
 #include "ui/UiTrace.h"
@@ -1460,7 +1461,7 @@ namespace
 {
 juce::File mixerStatesFolder()
 {
-    return juce::File::getSpecialLocation (juce::File::userDocumentsDirectory).getChildFile ("LYNX/Mixer states");
+    return lynx::platform::documentsFolder().getChildFile ("LYNX/Mixer states");
 }
 
 juce::Array<juce::File> savedMixerStates()

+ 2 - 2
src/ui/NewProjectDialog.cpp

@@ -174,7 +174,7 @@ public:
         name.onTextChange = [this] { updatePath(); };
         label (folderLabel, tr ("Folder"));
         folder = juce::File::isAbsolutePath (remembered.folder) ? juce::File (remembered.folder)
-                                                                 : juce::File::getSpecialLocation (juce::File::userDocumentsDirectory).getChildFile ("LYNX Projects");
+                                                                 : lynx::platform::documentsFolder().getChildFile ("LYNX Projects");
         if (! persistent)
             folderText = remembered.folder;
         folderValue.setFont (theme::font (theme::type::body));
@@ -421,7 +421,7 @@ private:
 
 juce::File userTemplatesFolder()
 {
-    return juce::File::getSpecialLocation (juce::File::userDocumentsDirectory).getChildFile ("LYNX/Templates");
+    return lynx::platform::documentsFolder().getChildFile ("LYNX/Templates");
 }
 
 bool newProjectDialogEnabled()

+ 21 - 0
tests/tests.cpp

@@ -34,6 +34,7 @@
 #include "core/FruityDelayBank.h"
 #include "core/FruityMultiband.h"
 #include "core/Denormals.h"
+#include "core/TestProfile.h"
 #include "core/FlpImport.h"
 #include "core/MidiControls.h"
 #include "core/MidiImport.h"
@@ -5352,6 +5353,26 @@ TEST (parallel_rendering_matches_sequential)
     CHECK (sum.load() == 2000 * 15);
 }
 
+TEST (rea165i_test_profile_and_test_mode_parsing)
+{
+    using namespace lynx::testprofile;
+    CHECK (! profileDirFrom (nullptr));
+    CHECK (! profileDirFrom (""));
+    CHECK (! profileDirFrom ("relative/dir")); // only absolute folders
+    CHECK (! profileDirFrom ("C:relative"));
+    CHECK (profileDirFrom ("/tmp/lynx-profile/") == std::string ("/tmp/lynx-profile"));
+    CHECK (profileDirFrom ("  \"C:\\Users\\me\\bench profile\\\" ") == std::string ("C:\\Users\\me\\bench profile"));
+    CHECK (profileDirFrom ("D:/x") == std::string ("D:/x"));
+    CHECK (profileDirFrom ("C:\\") == std::string ("C:\\")); // a drive root keeps its separator
+    CHECK (! testModeFrom (nullptr));
+    CHECK (! testModeFrom (""));
+    CHECK (! testModeFrom ("0"));
+    CHECK (! testModeFrom (" Off "));
+    CHECK (! testModeFrom ("false"));
+    CHECK (testModeFrom ("1"));
+    CHECK (testModeFrom ("yes"));
+}
+
 TEST (arrangements_switch_and_keep_clip_indices)
 {
     auto s = lynx::Song::makeDefault();