Skip to content

Commit 17a001b

Browse files
authored
refactor(benchmark): move recent messages app/bench to helper (#6815)
Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
1 parent c887461 commit 17a001b

File tree

5 files changed

+254
-221
lines changed

5 files changed

+254
-221
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
- Dev: Updated `pajlada-serialize` to v0.2.1. (#6797)
115115
- Dev: Updated `pajlada-signals` to v0.1.1. (#6797)
116116
- Dev: Balance IPv4 and IPv6 connection attempts. (#6804)
117+
- Dev: Factored out recent messages benchmark helper. (#6815)
117118
- Dev: Added `CHATTERINO_FORCE_LTO` CMake option to skip LTO check. (#6816)
118119

119120
## 2.5.4

benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(benchmark_SOURCES
1010
src/LimitedQueue.cpp
1111
src/LinkParser.cpp
1212
src/RecentMessages.cpp
13+
src/MessageBuilding.cpp
1314
# Add your new file above this line!
1415
)
1516

benchmarks/src/MessageBuilding.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// SPDX-FileCopyrightText: 2026 Contributors to Chatterino <https://chatterino.com>
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include "MessageBuilding.hpp"
6+
7+
#include "messages/Emote.hpp"
8+
9+
#include <QJsonDocument>
10+
#include <QJsonObject>
11+
12+
namespace {
13+
14+
using namespace Qt::Literals;
15+
16+
std::optional<QJsonDocument> tryReadJsonFile(const QString &path)
17+
{
18+
QFile file(path);
19+
if (!file.open(QFile::ReadOnly))
20+
{
21+
return std::nullopt;
22+
}
23+
24+
QJsonParseError e;
25+
auto doc = QJsonDocument::fromJson(file.readAll(), &e);
26+
if (e.error != QJsonParseError::NoError)
27+
{
28+
return std::nullopt;
29+
}
30+
31+
return doc;
32+
}
33+
34+
QJsonDocument readJsonFile(const QString &path)
35+
{
36+
auto opt = tryReadJsonFile(path);
37+
if (!opt)
38+
{
39+
_exit(1);
40+
}
41+
return *opt;
42+
}
43+
44+
} // namespace
45+
46+
namespace chatterino::bench {
47+
48+
MockMessageApplication::MockMessageApplication()
49+
: highlights(this->settings, &this->accounts)
50+
{
51+
}
52+
53+
MessageBenchmark::MessageBenchmark(QString name)
54+
: name(std::move(name))
55+
, chan(std::make_shared<TwitchChannel>(this->name))
56+
{
57+
const auto seventvEmotes =
58+
tryReadJsonFile(u":/bench/seventvemotes-%1.json"_s.arg(this->name));
59+
const auto bttvEmotes =
60+
tryReadJsonFile(u":/bench/bttvemotes-%1.json"_s.arg(this->name));
61+
const auto ffzEmotes =
62+
tryReadJsonFile(u":/bench/ffzemotes-%1.json"_s.arg(this->name));
63+
64+
if (seventvEmotes)
65+
{
66+
this->chan->setSeventvEmotes(std::make_shared<const EmoteMap>(
67+
seventv::detail::parseEmotes(seventvEmotes->object()["emote_set"_L1]
68+
.toObject()["emotes"_L1]
69+
.toArray(),
70+
false)));
71+
}
72+
73+
if (bttvEmotes)
74+
{
75+
this->chan->setBttvEmotes(
76+
std::make_shared<const EmoteMap>(bttv::detail::parseChannelEmotes(
77+
bttvEmotes->object(), this->name)));
78+
}
79+
80+
if (ffzEmotes)
81+
{
82+
this->chan->setFfzEmotes(std::make_shared<const EmoteMap>(
83+
ffz::detail::parseChannelEmotes(ffzEmotes->object())));
84+
}
85+
86+
this->messages =
87+
readJsonFile(u":/bench/recentmessages-%1.json"_s.arg(this->name));
88+
}
89+
90+
} // namespace chatterino::bench

benchmarks/src/MessageBuilding.hpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// SPDX-FileCopyrightText: 2026 Contributors to Chatterino <https://chatterino.com>
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#pragma once
6+
7+
#include "controllers/accounts/AccountController.hpp"
8+
#include "controllers/highlights/HighlightController.hpp"
9+
#include "mocks/BaseApplication.hpp"
10+
#include "mocks/EmoteController.hpp"
11+
#include "mocks/LinkResolver.hpp"
12+
#include "mocks/Logging.hpp"
13+
#include "mocks/TwitchIrcServer.hpp"
14+
#include "mocks/UserData.hpp"
15+
#include "providers/bttv/BttvBadges.hpp"
16+
#include "providers/chatterino/ChatterinoBadges.hpp"
17+
#include "providers/seventv/SeventvBadges.hpp"
18+
#include "providers/twitch/TwitchBadges.hpp"
19+
20+
#include <benchmark/benchmark.h>
21+
22+
namespace chatterino::bench {
23+
24+
class MockMessageApplication : public mock::BaseApplication
25+
{
26+
public:
27+
MockMessageApplication();
28+
29+
EmoteController *getEmotes() override
30+
{
31+
return &this->emotes;
32+
}
33+
34+
IUserDataController *getUserData() override
35+
{
36+
return &this->userData;
37+
}
38+
39+
AccountController *getAccounts() override
40+
{
41+
return &this->accounts;
42+
}
43+
44+
ITwitchIrcServer *getTwitch() override
45+
{
46+
return &this->twitch;
47+
}
48+
49+
ChatterinoBadges *getChatterinoBadges() override
50+
{
51+
return &this->chatterinoBadges;
52+
}
53+
54+
FfzBadges *getFfzBadges() override
55+
{
56+
return &this->ffzBadges;
57+
}
58+
59+
BttvBadges *getBttvBadges() override
60+
{
61+
return &this->bttvBadges;
62+
}
63+
64+
SeventvBadges *getSeventvBadges() override
65+
{
66+
return &this->seventvBadges;
67+
}
68+
69+
HighlightController *getHighlights() override
70+
{
71+
return &this->highlights;
72+
}
73+
74+
TwitchBadges *getTwitchBadges() override
75+
{
76+
return &this->twitchBadges;
77+
}
78+
79+
BttvEmotes *getBttvEmotes() override
80+
{
81+
return &this->bttvEmotes;
82+
}
83+
84+
FfzEmotes *getFfzEmotes() override
85+
{
86+
return &this->ffzEmotes;
87+
}
88+
89+
SeventvEmotes *getSeventvEmotes() override
90+
{
91+
return &this->seventvEmotes;
92+
}
93+
94+
IStreamerMode *getStreamerMode() override
95+
{
96+
return &this->streamerMode;
97+
}
98+
99+
ILinkResolver *getLinkResolver() override
100+
{
101+
return &this->linkResolver;
102+
}
103+
104+
ILogging *getChatLogger() override
105+
{
106+
return &this->logging;
107+
}
108+
109+
mock::EmptyLogging logging;
110+
AccountController accounts;
111+
mock::EmoteController emotes;
112+
mock::UserDataController userData;
113+
mock::MockTwitchIrcServer twitch;
114+
mock::EmptyLinkResolver linkResolver;
115+
ChatterinoBadges chatterinoBadges;
116+
FfzBadges ffzBadges;
117+
BttvBadges bttvBadges;
118+
SeventvBadges seventvBadges;
119+
HighlightController highlights;
120+
TwitchBadges twitchBadges;
121+
BttvEmotes bttvEmotes;
122+
FfzEmotes ffzEmotes;
123+
SeventvEmotes seventvEmotes;
124+
DisabledStreamerMode streamerMode;
125+
};
126+
127+
class MessageBenchmark
128+
{
129+
public:
130+
explicit MessageBenchmark(QString name);
131+
132+
virtual ~MessageBenchmark()
133+
{
134+
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
135+
}
136+
137+
MessageBenchmark(const MessageBenchmark &) = delete;
138+
MessageBenchmark(MessageBenchmark &&) = delete;
139+
MessageBenchmark &operator=(const MessageBenchmark &) = delete;
140+
MessageBenchmark &operator=(MessageBenchmark &&) = delete;
141+
142+
virtual void run(benchmark::State &state) = 0;
143+
144+
protected:
145+
QString name;
146+
MockMessageApplication app;
147+
std::shared_ptr<TwitchChannel> chan;
148+
QJsonDocument messages;
149+
};
150+
151+
} // namespace chatterino::bench

0 commit comments

Comments
 (0)