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 8599338
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 44 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
54 changes: 39 additions & 15 deletions scripts/Xvfb-bench-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,52 @@
#
# Usage: Xvfb-contour-run.sh <contour-args>

set -x
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}"
FB_DISPLAY="${FB_DISPLAY:-:99}"

if [[ "$TB_BIN" == "" ]]; then
echo "Usage: $0 <path-to-tb-executable>"
exit 1
fi

export TB_BIN=$(realpath $TB_BIN)
export DISPLAY=${FB_DISPLAY}
export LIBGL_ALWAYS_SOFTWARE="${LIBGL_ALWAYS_SOFTWARE:-true}"

function program_exit() {
local exit_code=$1
if [[ "$GITHUB_OUTPUT" != "" ]]; then
echo "exitCode=$exit_code" >> "$GITHUB_OUTPUT"
fi
exit $exit_code
}

function bench_terminal() {
printf "\033[1m==> Running terminal: $1\033[m\n"
local terminal_name=$(basename $1)
time "${@}" -e "${TB_BIN}" --output "${PWD}/${terminal_name}_results"
local exit_code=$?
printf "\033[1m==> Terminal exit code: $exit_code\033[m\n"
if [[ $exit_code -ne 0 ]]; then
program_exit $exit_code
fi
}

LIBGL_ALWAYS_SOFTWARE="${LIBGL_ALWAYS_SOFTWARE:-true}"
DISPLAY=:99
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
bench_terminal "${CONTOUR_BIN}" display ${DISPLAY}
bench_terminal "${KITTY_BIN}"
bench_terminal "${XTERM_BIN}" -display ${DISPLAY}
bench_terminal "${ALACRITTY_BIN}"

contour 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

if [[ "$GITHUB_OUTPUT" != "" ]]; then
echo "exitCode=$?" >> "$GITHUB_OUTPUT"
fi
program_exit 0
7 changes: 7 additions & 0 deletions tb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ 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
USES_TERMINAL
DEPENDS tb
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
28 changes: 17 additions & 11 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 @@ -104,6 +102,7 @@ int main(int argc, char const* argv[])
auto [width, height] = getTerminalSize();
size_t testSizeMB = 32;
bool nullSink = false;
bool columnByColumn = false;
std::string fileout {};

for (int i = 1; i < argc; ++i)
Expand All @@ -113,10 +112,15 @@ int main(int argc, char const* argv[])
cout << std::format("Using null-sink.\n");
nullSink = true;
}
else if (argv[i] == "--column-by-column"sv)
{
cout << std::format("Enabling column-by-column tests.\n");
columnByColumn = true;
}
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,14 +150,16 @@ 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));
for (size_t i = 0; i < Max_lines; ++i)
tb.add(contour::termbench::tests::sgr_line(i));
for (size_t i = 0; i < Max_lines; ++i)
tb.add(contour::termbench::tests::sgrbg_line(i));
if (columnByColumn)
{
constexpr size_t Max_lines { 200 };
for (size_t i = 0; i < Max_lines; ++i)
tb.add(contour::termbench::tests::ascii_line(i));
for (size_t i = 0; i < Max_lines; ++i)
tb.add(contour::termbench::tests::sgr_line(i));
for (size_t i = 0; i < Max_lines; ++i)
tb.add(contour::termbench::tests::sgrbg_line(i));
}

cout << "\033[8;30;100t";
cout.flush();
Expand Down

0 comments on commit 8599338

Please sign in to comment.