mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 01:23:24 -08:00
* [DeckList] Apply include-what-you-use to libcockatrice_deck_list * [IWYU] Apply include-what-you-use across the tree Run one whole-tree IWYU pass over the libcockatrice_* libraries, the Cockatrice client, Oracle, Servatrice and the test suite, so each translation unit includes what it uses directly and forward-declares what it does not need. IWYU only analyses real translation units, never the moc code CMake generates from Q_OBJECT headers, so it strips includes that moc output silently relies on. Where a type is passed by value (or nested in a QList/QMap) in a signal or slot signature, the generated moc code needs the complete type; those includes are kept with an explanatory `// IWYU pragma: keep`. The same holds for Q_DECLARE_METATYPE targets such as QSqlDatabase. A handful of includes are added by hand where callers brace-initialize a const-reference parameter, which also requires the complete type. Applied as a single commit on purpose: stripping includes from the libcockatrice_* headers is precisely what breaks the client, test and server translation units, and the fixes for those live in the same translation units. Splitting by component would therefore leave intermediate commits that do not compile. All 426 modified Q_OBJECT/Q_GADGET headers were verified to be moc-self-contained by compiling each generated moc_*.cpp standalone; several built only because of moc include ordering in mocs_compilation.cpp before the keep-includes were added. * [Tooling] Emit public Qt headers and fix IWYU fallout CI found Three problems the local build could not see, all because a green local build only proves the configuration it was run under. Private Qt headers. IWYU emitted private, lowercase Qt headers, and qtypes.h does not exist at all before Qt 6.9, so the Debian 12 and Ubuntu 24.04 jobs on Qt 6.4.2 failed to compile. The generator only emitted a mapping when the capital header name mirrored the private one (QAbstractAnimation for qabstractanimation.h), so qtypes.h, which is wrapped by QtTypes instead, got no mapping and kept the private spelling. Map it explicitly to <QtTypes>, which is public since Qt 5 and provides the same typedefs either way, and rewrite the 100 affected includes. Do not generalize this to "prefer any Qt* wrapper". The table is generated from whichever Qt is installed, and the wrappers are not equally old: QtLogging only appeared in Qt 6.8 and QtMinMax in 6.5, so that heuristic trades one breakage for another. Every other lowercase header still in the tree has shipped publicly since Qt 5, so those are left alone. Platform-guarded includes. IWYU only sees the preprocessor configuration it is handed, so it dropped an include that only the macOS branch of main.cpp needs, and the macOS jobs failed on an undeclared UrlSchemeEventFilter. Restore the include. Force-compiling every file that carries a platform guard under Q_OS_MAC, Q_OS_MACOS and __APPLE__ reports nothing else, so the macOS branches are covered rather than assumed. shellcheck. Drop a leftover variable and stop assigning the include-what-you-use lookup, which was only ever an existence check. * [Tooling] Map Qt 6.5+ split headers to QtGlobal for Qt 6.4 compatibility IWYU emitted QtTypes, QtPreprocessorSupport, QtVersionChecks, QtAssert, QtMinMax, QtClassHelperMacros, QtEnvironmentVariables and QtTranslation. Qt 6.5 split those declarations out of qglobal.h into separate headers, so neither the Qt* wrapper nor the q*.h private name exists on the oldest Qt we support (6.4.2), and Debian 12 / Ubuntu 24.04 fail with "No such file or directory". qtypes.h is worse still: it only appeared in Qt 6.9. QtGlobal is the one spelling that works across the whole supported range. It has been public since Qt 5, defines these declarations itself up to Qt 6.4, and includes the split headers from Qt 6.5 on, so redirect all eight private headers to it and collapse the affected includes in the tree. Curated overrides now take precedence over the name-mirroring rule, which cannot know about Qt version floors because the mapping is generated from whatever Qt is installed. That rule was mapping qtversionchecks.h to <QtVersionChecks> even with an override present, because the override was only consulted as a fallback. A curated key that matches nothing is now a hard error rather than a silent fallback, and the redirected headers are no longer advertised as the home of a symbol. Verified every Qt include in the tree resolves against the real qt6-base-dev 6.4.2 package: 15 distinct includes, 0 missing. * [Tooling] Map QOverload to QtGlobal and drop the QMetaMethodArgument include Two more headers IWYU emitted do not exist on Qt 6.4.2: QOverload was split out of qglobal.h in Qt 6.5, so on 6.4.2 it is declared in qglobal.h directly and the standalone header is a hard error. QtGlobal covers both, so map qoverload.h to it like the other 6.5+ split headers. QMetaMethodArgument is never referenced anywhere in the tree. It is a bare forwarder for qobjectdefs.h that our generator advertised as a class symbol, which is how IWYU came to add the include at all. The symbol mappings are generated from header names, so they cannot tell a forwarder that happens to be capitalised from a real class; removing the unused include is the fix, and the generic case stays a known limitation of the generator. Extending the audit to every Q* header (an earlier pass only matched headers starting with "Qt" or lowercase q*.h, so it missed all of them) against the real qt6-base-dev, qt6-multimedia-dev, qt6-declarative-dev, qt6-svg-dev and qt6-websockets-dev 6.4.2 packages: 341 distinct Qt includes, 0 missing. * [Game] Give moc the complete CardItem type in player_actions.h player_actions.h forward declares CardItem but passes QList<CardItem *> by value in actPlay/actPlayFacedown/actHide. moc instantiates the metatype for those parameters, and on Qt 6.4 that expands to sizeof(CardItem), so Ubuntu 24.04 fails with "invalid application of 'sizeof' to incomplete type 'CardItem'". Newer Qt tolerates the incomplete type, which is why only the 6.4.2 jobs failed. Include the defining header with an IWYU pragma keep, matching how the other MOC-required includes in this area are handled. * [Game] Give moc the complete ServerInfo_Card type player_logic.h, game_scene.h, view_zone.h and view_zone_widget.h forward declare ServerInfo_Card but pass QList<const ServerInfo_Card *> through signal and slot signatures. moc instantiates the metatype for those parameters, which Qt 6.4 expands to sizeof(ServerInfo_Card), so the connect in game_scene.cpp fails on Debian 12 and Ubuntu 24.04 with "invalid application of 'sizeof' to incomplete type 'const ServerInfo_Card'". Newer Qt tolerates it, so only the 6.4.2 jobs failed. Include the generated protobuf header with an IWYU pragma keep, following the pattern already used for ServerInfo_Player and ServerInfo_User in game_event_handler.h. * [Client] Complete the types Qt 6.4 needs for connect and moc Qt 6.4 expands QMetaType::fromType on the normalised argument list of a PMF connect and on moc-generated metatypes, which requires sizeof on every signal and slot parameter. Newer Qt tolerates forward declarations there, so only the Debian 12 and Ubuntu 24.04 jobs failed, with "invalid application of 'sizeof' to incomplete type" for QMouseEvent, QPrinter and GameReplay. Replace the forward declarations with the real includes: - QMouseEvent in the nine widget headers that pass it through signals - QPrinter in abstract_tab_deck_editor.cpp, for the actPrintDeck lambda - game_replay.pb.h in tab_replays.h. GameReplay is a protobuf message, so the project's game/replay.h only forward declares it and cannot supply the complete type. Found by recompiling all 1918 project TUs against the Debian Qt 6.4.2 headers, which reproduces those two CI jobs locally in one pass. * [Client] Complete Release and QMenu for Qt 6.4 moc and connect Continuing the Qt 6.4 complete-type sweep: - window_main.cpp connects ClientUpdateChecker::finishedCheck to MainWindow::checkClientUpdatesFinished, whose Release * parameter made moc 6.4 instantiate the metatype. GameReplay turned out to be a protobuf message, so Release likewise needed release_channel.h in the three headers declaring those signals: window_main.h, dlg_update.h and client_update_checker.h. - moc 6.4 also needs sizeof(QMenu) for the QList<QMenu *> carried by TabSupervisor::setMenu and MainWindow::updateTabMenu. Verified by regenerating moc output with the Debian moc 6.4.2 for all 434 Q_OBJECT/Q_GADGET classes and compiling it, plus recompiling all 1612 source TUs, against the Qt 6.4.2 headers: no remaining failures. --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
561 lines
22 KiB
C++
561 lines
22 KiB
C++
/*
|
|
* Picture loader benchmark / regression suite against the real card image hosts.
|
|
*
|
|
* Deliberately not registered with ctest: it hits live Scryfall / Gatherer
|
|
* endpoints at ~10 requests per second and takes minutes. Run it by hand.
|
|
*
|
|
* picture_loader_benchmark_test --carddb /path/to/cards.xml [options]
|
|
*
|
|
* Modes
|
|
* -----
|
|
* default : for every URL template in the configured download list (or for each
|
|
* --url given), load #count pictures twice: once cold (network) and
|
|
* once cached (served from the QNetworkDiskCache). Both passes must
|
|
* load every card with zero failures. The cached pass must complete
|
|
* well under the cold time, which is the regression gate for serving
|
|
* cached pictures instead of re-fetching them. The cold pass must stay
|
|
* above a pacing lower bound, the regression gate for burst-free
|
|
* request throttling.
|
|
* --stress: two CardPictureLoaderWorker instances loading the same cards
|
|
* concurrently against one host (~20 req/s aggregate), which forces
|
|
* real 429 responses. Both workers must still complete 100% of their
|
|
* cards via the shared backoff logic.
|
|
*/
|
|
|
|
#include "client/settings/cache_settings.h"
|
|
#include "interface/card_picture_loader/card_picture_loader_worker.h"
|
|
#include "interface/card_picture_loader/card_picture_to_load.h"
|
|
#include "libcockatrice/card/card_info.h"
|
|
#include "libcockatrice/card/database/card_database_loader.h"
|
|
|
|
#include <QByteArrayView>
|
|
#include <QCoreApplication>
|
|
#include <QDir>
|
|
#include <QElapsedTimer>
|
|
#include <QEventLoop>
|
|
#include <QFile>
|
|
#include <QFlags>
|
|
#include <QImage>
|
|
#include <QLatin1String>
|
|
#include <QList>
|
|
#include <QMessageLogContext>
|
|
#include <QMetaType>
|
|
#include <QMutex>
|
|
#include <QMutexLocker>
|
|
#include <QObject>
|
|
#include <QSharedPointer>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QStringLiteral>
|
|
#include <QTemporaryDir>
|
|
#include <QThread>
|
|
#include <QTimer>
|
|
#include <QUrl>
|
|
#include <QtGlobal>
|
|
#include <QtMessageHandler>
|
|
#include <atomic>
|
|
#include <cstdio>
|
|
#include <libcockatrice/card/database/card_database.h>
|
|
#include <libcockatrice/card/printing/exact_card.h>
|
|
#include <libcockatrice/card/printing/printing_info.h>
|
|
#include <libcockatrice/interfaces/interface_card_database_path_provider.h>
|
|
#include <libcockatrice/interfaces/noop_card_preference_provider.h>
|
|
#include <libcockatrice/interfaces/noop_card_set_priority_controller.h>
|
|
#include <libcockatrice/settings/download_settings.h>
|
|
#include <optional>
|
|
#include <qsystemdetection.h>
|
|
#include <utility>
|
|
|
|
class BenchmarkCardDatabasePathProvider : public ICardDatabasePathProvider
|
|
{
|
|
public:
|
|
BenchmarkCardDatabasePathProvider(QString _cardsXml, QString _customSetsDir)
|
|
: cardsXml(std::move(_cardsXml)), customSetsDir(std::move(_customSetsDir))
|
|
{
|
|
}
|
|
|
|
QString getCardDatabasePath() const override
|
|
{
|
|
return cardsXml;
|
|
}
|
|
|
|
QString getCustomCardDatabasePath() const override
|
|
{
|
|
return customSetsDir;
|
|
}
|
|
|
|
QString getTokenDatabasePath() const override
|
|
{
|
|
return QString();
|
|
}
|
|
|
|
QString getSpoilerCardDatabasePath() const override
|
|
{
|
|
return QString();
|
|
}
|
|
|
|
private:
|
|
QString cardsXml;
|
|
QString customSetsDir;
|
|
};
|
|
|
|
struct PassResult
|
|
{
|
|
int enqueued = 0;
|
|
int finished = 0;
|
|
int failed = 0;
|
|
qint64 elapsedMs = 0;
|
|
QStringList failedCards;
|
|
};
|
|
|
|
static QList<ExactCard> selectCardsForTemplate(CardDatabase &db, const QString &urlTemplate, int maxCards)
|
|
{
|
|
QList<ExactCard> selected;
|
|
const QList<CardInfoPtr> cards = db.getCardList().values();
|
|
for (const CardInfoPtr &card : cards) {
|
|
if (selected.size() >= maxCards) {
|
|
break;
|
|
}
|
|
const SetToPrintingsMap &sets = card->getSets();
|
|
if (sets.isEmpty()) {
|
|
continue;
|
|
}
|
|
const QList<PrintingInfo> printings = sets.first();
|
|
if (printings.isEmpty()) {
|
|
continue;
|
|
}
|
|
const ExactCard cardToLoad(card, printings.first());
|
|
if (CardPictureToLoad(cardToLoad).transformUrl(urlTemplate).isEmpty()) {
|
|
continue;
|
|
}
|
|
selected.append(cardToLoad);
|
|
}
|
|
return selected;
|
|
}
|
|
|
|
static PassResult runPass(CardPictureLoaderWorker *worker, const QList<ExactCard> &cards, int timeoutMs)
|
|
{
|
|
PassResult result;
|
|
result.enqueued = cards.size();
|
|
|
|
QEventLoop loop;
|
|
QTimer watchdog;
|
|
watchdog.setSingleShot(true);
|
|
watchdog.setInterval(timeoutMs);
|
|
QObject::connect(&watchdog, &QTimer::timeout, &loop, &QEventLoop::quit);
|
|
|
|
QElapsedTimer clock;
|
|
QObject::connect(worker, &CardPictureLoaderWorker::imageLoaded, &loop,
|
|
[&](const ExactCard &card, const QImage &image) {
|
|
++result.finished;
|
|
if (image.isNull()) {
|
|
++result.failed;
|
|
if (result.failedCards.size() < 10) {
|
|
result.failedCards.append(card.getName());
|
|
}
|
|
}
|
|
if (result.finished >= result.enqueued) {
|
|
loop.quit();
|
|
}
|
|
});
|
|
|
|
clock.start();
|
|
for (const ExactCard &card : cards) {
|
|
worker->enqueueImageLoad(card);
|
|
}
|
|
watchdog.start();
|
|
loop.exec();
|
|
result.elapsedMs = clock.elapsed();
|
|
return result;
|
|
}
|
|
|
|
struct StressResult
|
|
{
|
|
PassResult a;
|
|
PassResult b;
|
|
int http429Count = 0;
|
|
};
|
|
|
|
struct LogCounters
|
|
{
|
|
int http429 = 0;
|
|
QMutex mutex;
|
|
};
|
|
|
|
static std::atomic<LogCounters *> s_activeCounters{nullptr};
|
|
static QtMessageHandler s_previousMessageHandler = nullptr;
|
|
|
|
static void stressLogHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
{
|
|
if (LogCounters *counters = s_activeCounters.load(std::memory_order_acquire);
|
|
counters && msg.contains(QStringLiteral("Too many requests from"))) {
|
|
QMutexLocker locker(&counters->mutex);
|
|
++counters->http429;
|
|
}
|
|
if (s_previousMessageHandler) {
|
|
s_previousMessageHandler(type, context, msg);
|
|
} else {
|
|
// qInstallMessageHandler() reports the built-in handler as nullptr, so a plain `if` would
|
|
// swallow every message - including the 429 warnings this run is meant to surface. Fall
|
|
// back to Qt's message pattern written to stderr instead.
|
|
std::fprintf(stderr, "%s\n", qPrintable(qFormatLogMessage(type, context, msg)));
|
|
}
|
|
}
|
|
|
|
// Stops a worker's thread and frees it. shutdownThread()'s bounded wait guarantees that the worker
|
|
// object was freed by its finished() -> deleteLater chain, so the thread itself can then be deleted
|
|
// safely. A worker whose thread refused to stop is left alone (and leaked) rather than freed while
|
|
// still running.
|
|
static void destroyWorker(CardPictureLoaderWorker *worker)
|
|
{
|
|
if (!worker) {
|
|
return;
|
|
}
|
|
QThread *thread = worker->workerThread();
|
|
if (worker->shutdownThread()) {
|
|
delete thread;
|
|
}
|
|
}
|
|
|
|
static StressResult runStress(CardPictureLoaderWorker *workerA,
|
|
CardPictureLoaderWorker *workerB,
|
|
const QList<ExactCard> &cards,
|
|
int timeoutMs)
|
|
{
|
|
StressResult result;
|
|
result.a.enqueued = cards.size();
|
|
result.b.enqueued = cards.size();
|
|
|
|
int completed = 0;
|
|
QMutex completedMutex;
|
|
|
|
QEventLoop loop;
|
|
QTimer watchdog;
|
|
watchdog.setSingleShot(true);
|
|
watchdog.setInterval(timeoutMs);
|
|
QObject::connect(&watchdog, &QTimer::timeout, &loop, &QEventLoop::quit);
|
|
|
|
const auto finishOne = [&](PassResult &pass, const ExactCard &card, const QImage &image) {
|
|
++pass.finished;
|
|
if (image.isNull()) {
|
|
++pass.failed;
|
|
if (pass.failedCards.size() < 10) {
|
|
pass.failedCards.append(card.getName());
|
|
}
|
|
}
|
|
QMutexLocker locker(&completedMutex);
|
|
++completed;
|
|
if (completed >= result.a.enqueued + result.b.enqueued) {
|
|
loop.quit();
|
|
}
|
|
};
|
|
|
|
QElapsedTimer clock;
|
|
QObject::connect(workerA, &CardPictureLoaderWorker::imageLoaded, &loop,
|
|
[&](const ExactCard &card, const QImage &image) { finishOne(result.a, card, image); });
|
|
QObject::connect(workerB, &CardPictureLoaderWorker::imageLoaded, &loop,
|
|
[&](const ExactCard &card, const QImage &image) { finishOne(result.b, card, image); });
|
|
|
|
// Count 429 responses as seen by the shared rate limiter.
|
|
LogCounters counters;
|
|
s_activeCounters = &counters;
|
|
s_previousMessageHandler = qInstallMessageHandler(stressLogHandler);
|
|
|
|
clock.start();
|
|
for (const ExactCard &card : cards) {
|
|
workerA->enqueueImageLoad(card);
|
|
workerB->enqueueImageLoad(card);
|
|
}
|
|
watchdog.start();
|
|
loop.exec();
|
|
const qint64 elapsedMs = clock.elapsed();
|
|
result.a.elapsedMs = elapsedMs;
|
|
result.b.elapsedMs = elapsedMs;
|
|
|
|
// Stop both workers before touching the counters or restoring the message handler: their
|
|
// threads log from stressLogHandler, and must not outlive the stack-local counters (which is
|
|
// guaranteed on the watchdog path, where requests and deferred retries are still pending).
|
|
destroyWorker(workerA);
|
|
destroyWorker(workerB);
|
|
|
|
result.http429Count = counters.http429;
|
|
s_activeCounters.store(nullptr, std::memory_order_release);
|
|
qInstallMessageHandler(s_previousMessageHandler);
|
|
return result;
|
|
}
|
|
|
|
static QString formatDuration(qint64 ms)
|
|
{
|
|
return QStringLiteral("%1.%2 s").arg(ms / 1000).arg((ms % 1000) / 100);
|
|
}
|
|
|
|
static bool likelyRedirects(const QString &urlTemplate)
|
|
{
|
|
return urlTemplate.contains(QStringLiteral("api.scryfall.com"));
|
|
}
|
|
|
|
static QString hostOf(const QString &urlTemplate)
|
|
{
|
|
return QUrl(urlTemplate).host();
|
|
}
|
|
|
|
static void printUsage()
|
|
{
|
|
std::printf("usage: picture_loader_benchmark_test --carddb <path> [options]\n"
|
|
"\n"
|
|
"Loads card pictures from the real configured hosts (not a mock server) and\n"
|
|
"verifies the picture loader's pacing / cache 429 behavior.\n"
|
|
"\n"
|
|
"options:\n"
|
|
" --carddb <path> cards.xml to load card data from (required)\n"
|
|
" --count <N> cards to load per template (default 300)\n"
|
|
" --url <template> test only this URL template; repeatable\n"
|
|
" --stress run the two-worker concurrency stress instead\n"
|
|
" --stress-url <t> template used by --stress (default: scryfall uuid)\n"
|
|
" --timeout-min <N> per-pass watchdog in minutes (default auto)\n"
|
|
" --cache-dir <dir> reuse this directory as the sandbox root; non-empty\n"
|
|
" cache skips the cold pacing lower bound\n"
|
|
" --help show this help\n");
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
QString cardDbArg;
|
|
QStringList explicitUrls;
|
|
int count = 300;
|
|
bool stress = false;
|
|
bool stressUrlSet = false;
|
|
QString stressUrl(QStringLiteral("https://api.scryfall.com/cards/!set:uuid!?format=image"));
|
|
QString cacheDirArg;
|
|
std::optional<int> timeoutMin;
|
|
|
|
QStringList args;
|
|
for (int i = 1; i < argc; ++i) {
|
|
args.append(QString::fromLocal8Bit(argv[i]));
|
|
}
|
|
for (int i = 0; i < args.size(); ++i) {
|
|
const QString &arg = args.at(i);
|
|
const auto value = [&]() -> QString { return i + 1 < args.size() ? args.at(++i) : QString(); };
|
|
if (arg == QLatin1String("--carddb")) {
|
|
cardDbArg = value();
|
|
} else if (arg == QLatin1String("--count")) {
|
|
count = value().toInt();
|
|
} else if (arg == QLatin1String("--url")) {
|
|
explicitUrls.append(value());
|
|
} else if (arg == QLatin1String("--stress")) {
|
|
stress = true;
|
|
} else if (arg == QLatin1String("--stress-url")) {
|
|
stressUrl = value();
|
|
stressUrlSet = true;
|
|
} else if (arg == QLatin1String("--timeout-min")) {
|
|
bool ok = false;
|
|
const int parsed = value().toInt(&ok);
|
|
if (!ok || parsed <= 0) {
|
|
std::fprintf(stderr, "error: --timeout-min must be a positive integer\n");
|
|
return 2;
|
|
}
|
|
timeoutMin = parsed;
|
|
} else if (arg == QLatin1String("--cache-dir")) {
|
|
cacheDirArg = value();
|
|
} else if (arg == QLatin1String("--help") || arg == QLatin1String("-h")) {
|
|
printUsage();
|
|
return 0;
|
|
} else {
|
|
std::fprintf(stderr, "unknown argument: %s\n", qPrintable(arg));
|
|
printUsage();
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
if (cardDbArg.isEmpty()) {
|
|
std::fprintf(stderr, "error: --carddb is required (a cards.xml generated by Oracle)\n");
|
|
printUsage();
|
|
return 2;
|
|
}
|
|
if (count <= 0) {
|
|
std::fprintf(stderr, "error: --count must be positive\n");
|
|
return 2;
|
|
}
|
|
|
|
// In --stress mode an explicit --url selects the template to hammer, matching --url's meaning
|
|
// in the normal mode; only fall back to the built-in Scryfall template when neither --stress-url
|
|
// nor --url was supplied.
|
|
if (stress && !stressUrlSet && !explicitUrls.isEmpty()) {
|
|
stressUrl = explicitUrls.first();
|
|
}
|
|
|
|
QCoreApplication app(argc, argv);
|
|
// Unique names so a benchmark run can never read or write the real client's settings, cache or
|
|
// picture URLs on platforms where the XDG redirection below does not apply (macOS, Windows).
|
|
app.setApplicationName(QStringLiteral("Cockatrice-benchmark"));
|
|
app.setOrganizationName(QStringLiteral("Cockatrice-benchmark"));
|
|
app.setApplicationVersion(QStringLiteral("9.0.0-benchmark"));
|
|
|
|
// The ExactCard argument of imageLoaded crosses threads via a queued connection.
|
|
qRegisterMetaType<ExactCard>();
|
|
|
|
QTemporaryDir sandbox;
|
|
if (cacheDirArg.isEmpty() && !sandbox.isValid()) {
|
|
std::fprintf(stderr, "error: could not create a temporary sandbox directory\n");
|
|
return 2;
|
|
}
|
|
const QString rootDir = cacheDirArg.isEmpty() ? sandbox.path() : cacheDirArg;
|
|
QDir().mkpath(rootDir);
|
|
QDir().mkpath(rootDir + "/config");
|
|
QDir().mkpath(rootDir + "/data");
|
|
QDir().mkpath(rootDir + "/cache");
|
|
|
|
#ifdef Q_OS_LINUX
|
|
// Redirect every QStandardPaths lookup (and therefore SettingsCache paths) into the sandbox so
|
|
// the benchmark never touches user config or caches. XDG_* only affects Qt's path resolution on
|
|
// Linux; elsewhere the unique application/organization names above keep the run isolated.
|
|
qputenv("XDG_CONFIG_HOME", (rootDir + "/config").toUtf8());
|
|
qputenv("XDG_DATA_HOME", (rootDir + "/data").toUtf8());
|
|
qputenv("XDG_CACHE_HOME", (rootDir + "/cache").toUtf8());
|
|
#endif
|
|
|
|
// Derive the probe from SettingsCache rather than reconstructing it: Qt appends both the
|
|
// organization and the application name, so a hand-built path is easy to get wrong.
|
|
const bool warmStart = QDir(SettingsCache::instance().getNetworkCachePath())
|
|
.entryList(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)
|
|
.size() > 0;
|
|
|
|
// Copied into the sandbox so the loader's binary cache ("cards.xml.cache")
|
|
// is written next to it instead of next to the user's file, and so a
|
|
// --cache-dir rerun can pick it up again.
|
|
const QString dataPath = SettingsCache::instance().getDataPath();
|
|
QDir().mkpath(dataPath);
|
|
const QString cardsXml = dataPath + "/cards.xml";
|
|
if (!QFile::exists(cardsXml)) {
|
|
if (!QFile::copy(cardDbArg, cardsXml)) {
|
|
std::fprintf(stderr, "error: could not copy %s to sandbox\n", qPrintable(cardDbArg));
|
|
return 2;
|
|
}
|
|
}
|
|
const QString customSetsDir = dataPath + "/customsets";
|
|
QDir().mkpath(customSetsDir);
|
|
|
|
auto *prefs = new NoopCardPreferenceProvider();
|
|
auto *priorityController = new NoopCardSetPriorityController();
|
|
auto *pathProvider = new BenchmarkCardDatabasePathProvider(cardsXml, customSetsDir);
|
|
CardDatabase db(nullptr, prefs, pathProvider, priorityController);
|
|
db.loadCardDatabases();
|
|
if (db.getLoadStatus() != Ok || db.getCardList().isEmpty()) {
|
|
std::fprintf(stderr, "error: failed to load card database from %s\n", qPrintable(cardsXml));
|
|
return 1;
|
|
}
|
|
const int availableCards = db.getCardList().size();
|
|
|
|
const QStringList urlsToTest =
|
|
explicitUrls.isEmpty() ? SettingsCache::instance().downloads().getAllURLs() : explicitUrls;
|
|
|
|
if (stress) {
|
|
const QList<ExactCard> cards = selectCardsForTemplate(db, stressUrl, count);
|
|
if (cards.isEmpty()) {
|
|
std::fprintf(stderr, "error: no cards satisfy template %s\n", qPrintable(stressUrl));
|
|
return 1;
|
|
}
|
|
SettingsCache::instance().downloads().setDownloadUrls({stressUrl});
|
|
|
|
std::printf("=== STRESS: 2 workers x %d cards, host %s ===\n", static_cast<int>(cards.size()),
|
|
qPrintable(hostOf(stressUrl)));
|
|
auto *workerA = new CardPictureLoaderWorker();
|
|
auto *workerB = new CardPictureLoaderWorker();
|
|
const int timeoutMs = (timeoutMin.has_value() ? timeoutMin.value() : 15) * 60 * 1000;
|
|
const StressResult result = runStress(workerA, workerB, cards, timeoutMs);
|
|
|
|
const bool completeA = result.a.finished >= result.a.enqueued;
|
|
const bool completeB = result.b.finished >= result.b.enqueued;
|
|
const bool zeroFailures = result.a.failed == 0 && result.b.failed == 0;
|
|
const bool pass = completeA && completeB && zeroFailures;
|
|
|
|
std::printf("worker A: %d/%d loaded, %d failed (%.2f images/s)\n", result.a.finished, result.a.enqueued,
|
|
result.a.failed,
|
|
result.a.elapsedMs > 0
|
|
? static_cast<double>(result.a.finished) * 1000.0 / static_cast<double>(result.a.elapsedMs)
|
|
: 0.0);
|
|
std::printf("worker B: %d/%d loaded, %d failed (%.2f images/s)\n", result.b.finished, result.b.enqueued,
|
|
result.b.failed,
|
|
result.b.elapsedMs > 0
|
|
? static_cast<double>(result.b.finished) * 1000.0 / static_cast<double>(result.b.elapsedMs)
|
|
: 0.0);
|
|
std::printf("429 responses observed: %d\n", result.http429Count);
|
|
std::printf("elapsed: %s\n", qPrintable(formatDuration(result.a.elapsedMs)));
|
|
if (result.a.failed > 0) {
|
|
std::printf(" worker A failures: %s\n", qPrintable(result.a.failedCards.join(QStringLiteral(", "))));
|
|
}
|
|
if (result.b.failed > 0) {
|
|
std::printf(" worker B failures: %s\n", qPrintable(result.b.failedCards.join(QStringLiteral(", "))));
|
|
}
|
|
std::printf("RESULT: %s\n", pass ? "PASS" : "FAIL");
|
|
return pass ? 0 : 1;
|
|
}
|
|
|
|
bool allPass = true;
|
|
std::printf("=== PICTURE LOADER BENCHMARK (%d cards available, %d per template)%s ===\n", availableCards, count,
|
|
warmStart ? ", WARM cache from previous run" : "");
|
|
|
|
for (const QString &urlTemplate : urlsToTest) {
|
|
const QList<ExactCard> cards = selectCardsForTemplate(db, urlTemplate, count);
|
|
if (cards.isEmpty()) {
|
|
std::printf("SKIP %-22s %-52s (no cards satisfy the template)\n", qPrintable(hostOf(urlTemplate)),
|
|
qPrintable(urlTemplate));
|
|
continue;
|
|
}
|
|
SettingsCache::instance().downloads().setDownloadUrls({urlTemplate});
|
|
|
|
const bool redirects = likelyRedirects(urlTemplate);
|
|
const qint64 perCardMs = redirects ? 200 : 100;
|
|
const int timeoutMs =
|
|
(timeoutMin.has_value() ? timeoutMin.value() : (cards.size() * perCardMs * 8 + 60000) / 60000) * 60 * 1000;
|
|
const qint64 coldLowerMs = static_cast<qint64>(cards.size()) * perCardMs / 2;
|
|
// Decoding and cache-reading scale with the card count, so a flat budget would spuriously
|
|
// fail larger --count runs served entirely from a healthy cache.
|
|
const qint64 cachedUpperMs = qMax<qint64>(2000, static_cast<qint64>(cards.size()) * 10);
|
|
|
|
// A fresh worker per pass: if the cold pass hits the watchdog, its outstanding cards stay
|
|
// in the worker's currentlyLoading set, which would make the cached pass silently skip them
|
|
// and burn its own watchdog; late cold replies would also be misattributed to the cached
|
|
// pass.
|
|
auto *coldWorker = new CardPictureLoaderWorker();
|
|
const PassResult cold = runPass(coldWorker, cards, timeoutMs);
|
|
destroyWorker(coldWorker);
|
|
|
|
auto *cachedWorker = new CardPictureLoaderWorker();
|
|
const PassResult cached = runPass(cachedWorker, cards, timeoutMs);
|
|
destroyWorker(cachedWorker);
|
|
|
|
const bool coldComplete = cold.finished >= cold.enqueued;
|
|
const bool coldZeroFailures = cold.failed == 0;
|
|
const bool coldPaced = warmStart || cold.elapsedMs >= coldLowerMs;
|
|
const bool cachedComplete = cached.finished >= cached.enqueued;
|
|
const bool cachedZeroFailures = cached.failed == 0;
|
|
const bool cachedFast = cached.elapsedMs < cachedUpperMs;
|
|
|
|
const bool pass =
|
|
coldComplete && coldZeroFailures && coldPaced && cachedComplete && cachedZeroFailures && cachedFast;
|
|
allPass = allPass && pass;
|
|
|
|
std::printf("%s %-22s %-52s cold %s (paced>=%s) cached %s (must be <%.0fs) | %d/%d %d/%d loaded, "
|
|
"%d/%d failed\n",
|
|
pass ? "PASS " : "FAIL ", qPrintable(hostOf(urlTemplate)), qPrintable(urlTemplate),
|
|
qPrintable(formatDuration(cold.elapsedMs)), qPrintable(formatDuration(coldLowerMs)),
|
|
qPrintable(formatDuration(cached.elapsedMs)), static_cast<double>(cachedUpperMs) / 1000.0,
|
|
cold.finished, cold.enqueued, cached.finished, cached.enqueued, cold.failed, cached.failed);
|
|
if (cold.failed > 0) {
|
|
std::printf(" cold failures: %s\n", qPrintable(cold.failedCards.join(QStringLiteral(", "))));
|
|
}
|
|
if (cached.failed > 0) {
|
|
std::printf(" cached failures: %s\n", qPrintable(cached.failedCards.join(QStringLiteral(", "))));
|
|
}
|
|
}
|
|
|
|
if (cacheDirArg.isEmpty()) {
|
|
std::printf("cache root: %s (temporary; pass --cache-dir <dir> to persist it for a warm rerun)\n",
|
|
qPrintable(rootDir));
|
|
} else {
|
|
std::printf("cache root: %s\n", qPrintable(rootDir));
|
|
}
|
|
std::printf("RESULT: %s\n", allPass ? "PASS" : "FAIL");
|
|
return allPass ? 0 : 1;
|
|
}
|