Skip to content

Commit

Permalink
Fixes an infinite loop in termbnech.cpp and add some overall improvem…
Browse files Browse the repository at this point in the history
…ents

Signed-off-by: Christian Parpart <[email protected]>
  • Loading branch information
christianparpart committed Mar 17, 2024
1 parent f7ba82f commit b26e316
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
37 changes: 19 additions & 18 deletions libtermbench/termbench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ void Benchmark::add(unique_ptr<Test> _test)
tests_.emplace_back(std::move(_test));
}

void Benchmark::writeOutput(Buffer const& testBuffer)
{
auto const output = testBuffer.output();
auto remainingBytes = totalSizeBytes();
while (remainingBytes > 0)
{
auto const n = std::min(output.size(), remainingBytes);
writer_(output.data(), n);
remainingBytes -= n;
}
}

void Benchmark::runAll()
{
auto buffer = make_unique<Buffer>(min(static_cast<size_t>(64u), testSizeMB_));
Expand All @@ -78,30 +90,19 @@ void Benchmark::runAll()
test->setup(width_, height_);
test->run(*buffer);

auto const output = buffer->output();
auto const totalSizeBytes = testSizeMB_ * 1024 * 1024;
auto const beginTime = steady_clock::now();
auto remainingBytes = totalSizeBytes;
writeOutput(*buffer);
auto const diff = duration_cast<milliseconds>(steady_clock::now() - beginTime);
while (diff < 1s)
{
while (remainingBytes > 0)
{
auto const n = std::min(output.size(), remainingBytes);
writer_(output.data(), n);
remainingBytes -= n;
}
}

results_.emplace_back(Result { *test, diff, totalSizeBytes });
results_.emplace_back(*test, diff, totalSizeBytes());

buffer->clear();
test->teardown(*buffer);
if (buffer->empty())
continue;

writer_(buffer->output().data(), buffer->output().size());
buffer->clear();
if (!buffer->empty())
{
writer_(buffer->output().data(), buffer->output().size());
buffer->clear();
}
}
}
//
Expand Down
4 changes: 4 additions & 0 deletions libtermbench/termbench.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ class Benchmark

std::vector<Result> const& results() const noexcept { return results_; }

[[constexpr]] size_t totalSizeBytes() const noexcept { return testSizeMB_ * 1024 * 1024; }

private:
void writeOutput(Buffer const& testBuffer);

std::function<void(char const*, size_t)> writer_;
std::function<void(Test const&)> beforeTest_;
size_t testSizeMB_;
Expand Down
29 changes: 20 additions & 9 deletions scripts/Xvfb-bench-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,38 @@
#
# Usage: Xvfb-contour-run.sh <contour-args>

set -x


LIBGL_ALWAYS_SOFTWARE="${LIBGL_ALWAYS_SOFTWARE:-true}"
DISPLAY=:99

TB_BIN="${1:-${TB_BIN}}"
CONTOUR_BIN="${CONTOUR_BIN:-contour}"
KITTY_BIN="${KITTY_BIN:-kitty}"
XTERM_BIN="${XTERM_BIN:-xterm}"
ALACRITTY_BIN="${ALACRITTY_BIN:-alacritty}"

if [[ "$TB_BIN" == "" ]]; then
echo "Usage: Xvfb-contour-run.sh <path-to-tb-executable>"
exit 1
fi

TB_BIN=$(realpath $TB_BIN)

export LIBGL_ALWAYS_SOFTWARE
export DISPLAY

set -x

Xvfb $DISPLAY -screen 0 1280x1024x24 &
XVFB_PID=$!
trap "kill $XVFB_PID" EXIT

sleep 3

TB_BIN=$PWD/build/tb/tb

contour display ${DISPLAY} $TB_BIN --output contour_results
"${CONTOUR_BIN}" display ${DISPLAY} $TB_BIN --output contour_results
mv $HOME/contour_results .
kitty -e $TB_BIN --output kitty_results
xterm -display ${DISPLAY} -e $TB_BIN --output xterm_results
alacritty -e $TB_BIN --output alacritty_results
"${KITTY_BIN}" -e $TB_BIN --output kitty_results
"${XTERM_BIN}" -display ${DISPLAY} -e $TB_BIN --output xterm_results
"${ALACRITTY_BIN}" -e $TB_BIN --output alacritty_results

if [[ "$GITHUB_OUTPUT" != "" ]]; then
echo "exitCode=$?" >> "$GITHUB_OUTPUT"
Expand Down
6 changes: 6 additions & 0 deletions tb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ set_target_properties(tb PROPERTIES

install(TARGETS tb RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

add_custom_target(Xvfb-bench-run
COMMAND ${CMAKE_SOURCE_DIR}/scripts/Xvfb-bench-run.sh $<TARGET_FILE:tb>
VERBATIM
DEPENDS tb
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
5 changes: 1 addition & 4 deletions tb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

using std::cerr;
using std::cout;
using std::stoul;
using std::tuple;

using namespace std::string_view_literals;
using namespace std::placeholders;
Expand Down Expand Up @@ -116,7 +114,7 @@ int main(int argc, char const* argv[])
else if (argv[i] == "--size"sv && i + 1 < argc)
{
++i;
testSizeMB = static_cast<size_t>(stoul(argv[i]));
testSizeMB = static_cast<size_t>(std::stoul(argv[i]));
}
else if (argv[i] == "--help"sv || argv[i] == "-h"sv)
{
Expand Down Expand Up @@ -146,7 +144,6 @@ int main(int argc, char const* argv[])
tb.add(contour::termbench::tests::sgr_fg_lines());
tb.add(contour::termbench::tests::sgr_fgbg_lines());
tb.add(contour::termbench::tests::binary());
// tb.add(contour::termbench::tests::binary());
constexpr size_t Max_lines { 200 };
for (size_t i = 0; i < Max_lines; ++i)
tb.add(contour::termbench::tests::ascii_line(i));
Expand Down

0 comments on commit b26e316

Please sign in to comment.