|
| 1 | +// SPDX-FileCopyrightText: 2026 Contributors to Chatterino <https://chatterino.com> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include "common/Literals.hpp" |
| 6 | +#include "controllers/filters/FilterSet.hpp" |
| 7 | +#include "MessageBuilding.hpp" |
| 8 | +#include "providers/recentmessages/Impl.hpp" |
| 9 | + |
| 10 | +#include <benchmark/benchmark.h> |
| 11 | +#include <QFile> |
| 12 | +#include <QJsonArray> |
| 13 | +#include <QJsonDocument> |
| 14 | +#include <QString> |
| 15 | + |
| 16 | +using namespace chatterino; |
| 17 | +using namespace Qt::Literals; |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +class FilterMessages : public bench::MessageBenchmark |
| 22 | +{ |
| 23 | +public: |
| 24 | + explicit FilterMessages(QString name, QStringList filters) |
| 25 | + : bench::MessageBenchmark(std::move(name)) |
| 26 | + , filterTexts(std::move(filters)) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + void run(benchmark::State &state) override |
| 31 | + { |
| 32 | + auto parsed = recentmessages::detail::parseRecentMessages( |
| 33 | + this->messages.object()); |
| 34 | + auto built = recentmessages::detail::buildRecentMessages( |
| 35 | + parsed, this->chan.get()); |
| 36 | + |
| 37 | + QList<QUuid> filters; |
| 38 | + for (qsizetype i = 0; i < this->filterTexts.size(); i++) |
| 39 | + { |
| 40 | + // ensure deterministic order |
| 41 | + auto id = QUuid(static_cast<uint>(i), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 42 | + auto filter = std::make_shared<FilterRecord>( |
| 43 | + QString::number(i), this->filterTexts.at(i), id); |
| 44 | + if (!filter->valid()) |
| 45 | + { |
| 46 | + qCDebug(chatterinoApp) << i << this->filterTexts[i]; |
| 47 | + assert(false); |
| 48 | + continue; |
| 49 | + } |
| 50 | + getSettings()->filterRecords.append(filter); |
| 51 | + filters.append(id); |
| 52 | + } |
| 53 | + assert(filters.size() == this->filterTexts.size()); |
| 54 | + FilterSet set(filters); |
| 55 | + assert(set.filterIds().size() == filters.size()); |
| 56 | + |
| 57 | + for (auto _ : state) |
| 58 | + { |
| 59 | + for (const auto &msg : built) |
| 60 | + { |
| 61 | + bool filtered = set.filter(msg, this->chan); |
| 62 | + benchmark::DoNotOptimize(filtered); |
| 63 | + benchmark::ClobberMemory(); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +private: |
| 69 | + QStringList filterTexts; |
| 70 | +}; |
| 71 | + |
| 72 | +void BM_FilterMessages(benchmark::State &state, QString channel, |
| 73 | + QStringList filters) |
| 74 | +{ |
| 75 | + FilterMessages bench(std::move(channel), std::move(filters)); |
| 76 | + bench.run(state); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace |
| 80 | + |
| 81 | +BENCHMARK_CAPTURE( |
| 82 | + BM_FilterMessages, nymn_modmessages, u"nymn"_s, |
| 83 | + { |
| 84 | + uR".(channel.name == "nymn" && author.badges contains "moderator")."_s, |
| 85 | + }); |
| 86 | + |
| 87 | +BENCHMARK_CAPTURE( |
| 88 | + BM_FilterMessages, nymn_mod_party, u"nymn"_s, |
| 89 | + { |
| 90 | + uR".((author.badges contains "moderator") && (message.content contains "forsenParty"))."_s, |
| 91 | + }); |
| 92 | + |
| 93 | +BENCHMARK_CAPTURE(BM_FilterMessages, nymn_len40_or_sub, u"nymn"_s, |
| 94 | + { |
| 95 | + uR".(message.length < 40 || author.subbed)."_s, |
| 96 | + }); |
| 97 | + |
| 98 | +BENCHMARK_CAPTURE(BM_FilterMessages, nymn_no_sub, u"nymn"_s, |
| 99 | + { |
| 100 | + uR".(!flags.sub_message)."_s, |
| 101 | + }); |
| 102 | + |
| 103 | +BENCHMARK_CAPTURE(BM_FilterMessages, nymn_with_color, u"nymn"_s, |
| 104 | + { |
| 105 | + uR".(!author.no_color)."_s, |
| 106 | + }); |
| 107 | + |
| 108 | +BENCHMARK_CAPTURE( |
| 109 | + BM_FilterMessages, nymn_complex_regex, u"nymn"_s, |
| 110 | + { |
| 111 | + uR".((message.content match r"^(?!.*(?:my|complex|(re.*x))).*$"))."_s, |
| 112 | + }); |
| 113 | + |
| 114 | +BENCHMARK_CAPTURE( |
| 115 | + BM_FilterMessages, nymn_big_or, u"nymn"_s, |
| 116 | + { |
| 117 | + uR".((author.subbed && author.sub_length >= 6) || flags.system_message || flags.first_message || flags.automod || flags.sub_message)."_s, |
| 118 | + }); |
| 119 | + |
| 120 | +BENCHMARK_CAPTURE( |
| 121 | + BM_FilterMessages, nymn_with_color_and_edm_single, u"nymn"_s, |
| 122 | + { |
| 123 | + uR".(!author.no_color && message.content contains "EDM")."_s, |
| 124 | + }); |
| 125 | + |
| 126 | +BENCHMARK_CAPTURE(BM_FilterMessages, nymn_with_color_and_edm_separate, |
| 127 | + u"nymn"_s, |
| 128 | + { |
| 129 | + uR".(!author.no_color)."_s, |
| 130 | + uR".(message.content contains "EDM")."_s, |
| 131 | + }); |
0 commit comments