1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00
luanti/src/benchmark/benchmark_ipc_channel.cpp

62 lines
1.7 KiB
C++
Raw Normal View History

2024-03-02 21:58:18 +01:00
/*
Minetest
Copyright (C) 2024 DS
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2024-10-04 03:19:06 +02:00
#include "catch.h"
2024-03-02 21:58:18 +01:00
#include "threading/ipc_channel.h"
#include <thread>
TEST_CASE("benchmark_ipc_channel")
{
2024-10-05 12:52:19 +02:00
auto end_a_thread_b_p = make_test_ipc_channel([](IPCChannelEnd end_b) {
// echos back messages. stops if "" is sent
2024-03-02 21:58:18 +01:00
for (;;) {
end_b.recv();
2024-10-06 15:45:15 +02:00
end_b.send(end_b.getRecvData());
if (end_b.getRecvData().size() == 0)
2024-03-02 21:58:18 +01:00
break;
}
});
2024-10-05 12:52:19 +02:00
// Can't use structured bindings before C++20, because of lamda captures below.
auto end_a = std::move(end_a_thread_b_p.first);
auto thread_b = std::move(end_a_thread_b_p.second);
2024-03-02 21:58:18 +01:00
2024-10-04 03:19:06 +02:00
BENCHMARK("simple_call_1", i) {
2024-03-02 21:58:18 +01:00
char buf[16] = {};
buf[i & 0xf] = i;
2024-10-06 15:45:15 +02:00
end_a.exchange({buf, 16});
return end_a.getRecvData()[i & 0xf];
2024-03-02 21:58:18 +01:00
};
2024-10-04 03:19:06 +02:00
BENCHMARK("simple_call_1000", i) {
char buf[16] = {};
buf[i & 0xf] = i;
for (int k = 0; k < 1000; ++k) {
buf[0] = k & 0xff;
2024-10-06 15:45:15 +02:00
end_a.exchange({buf, 16});
2024-10-04 03:19:06 +02:00
}
2024-10-06 15:45:15 +02:00
return end_a.getRecvData()[i & 0xf];
2024-10-04 03:19:06 +02:00
};
2024-03-02 21:58:18 +01:00
// stop thread_b
2024-10-06 15:45:15 +02:00
end_a.exchange({nullptr, 0});
REQUIRE(end_a.getRecvData().size() == 0);
2024-03-02 21:58:18 +01:00
thread_b.join();
}