Skip to content

Commit

Permalink
Merge pull request #12 from contour-terminal/improvement/tb-and-overa…
Browse files Browse the repository at this point in the history
…ll-run

Fixes an infinite loop in termbnech.cpp and add some overall improvements
  • Loading branch information
christianparpart authored Mar 17, 2024
2 parents f7ba82f + 1f364e8 commit 29f1461
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
- name: "Install kitty and xterm"
run: sudo apt install -y kitty xterm xvfb
- name: "run benchmarks"
run: ./scripts/Xvfb-bench-run.sh
run: ./scripts/Xvfb-bench-run.sh ./build/tb/tb
- name: "ls"
run: ls -la
- name: "cat contour_results"
Expand Down
85 changes: 40 additions & 45 deletions libtermbench/termbench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
#include <ostream>
#include <utility>

using std::function;
using std::make_unique;
using std::min;
using std::string_view;
using std::unique_ptr;

using namespace std::chrono;
using namespace std::string_view_literals;

Expand All @@ -48,11 +42,11 @@ namespace

using u16 = unsigned short;

Benchmark::Benchmark(function<void(char const*, size_t n)> _writer,
Benchmark::Benchmark(std::function<void(char const*, size_t n)> _writer,
size_t _testSizeMB,
unsigned short _width,
unsigned short _height,
function<void(Test const&)> _beforeTest):
std::function<void(Test const&)> _beforeTest):
writer_ { std::move(_writer) },
beforeTest_ { std::move(_beforeTest) },
testSizeMB_ { _testSizeMB },
Expand All @@ -61,14 +55,26 @@ Benchmark::Benchmark(function<void(char const*, size_t n)> _writer,
{
}

void Benchmark::add(unique_ptr<Test> _test)
void Benchmark::add(std::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_));
auto buffer = std::make_unique<Buffer>(std::min(static_cast<size_t>(64u), testSizeMB_));

for (auto& test: tests_)
{
Expand All @@ -78,30 +84,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);
buffer->clear();
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 Expand Up @@ -373,58 +368,58 @@ namespace
};
} // namespace

unique_ptr<Test> many_lines()
std::unique_ptr<Test> many_lines()
{
return make_unique<ManyLines>();
return std::make_unique<ManyLines>();
}

unique_ptr<Test> long_lines()
std::unique_ptr<Test> long_lines()
{
return make_unique<LongLines>();
return std::make_unique<LongLines>();
}

unique_ptr<Test> sgr_fg_lines()
std::unique_ptr<Test> sgr_fg_lines()
{
return make_unique<SgrFgColoredText>();
return std::make_unique<SgrFgColoredText>();
}

unique_ptr<Test> sgr_fgbg_lines()
std::unique_ptr<Test> sgr_fgbg_lines()
{
return make_unique<SgrFgBgColoredText>();
return std::make_unique<SgrFgBgColoredText>();
}

unique_ptr<Test> binary()
std::unique_ptr<Test> binary()
{
return make_unique<Binary>();
return std::make_unique<Binary>();
}

unique_ptr<Test> ascii_line(size_t N)
std::unique_ptr<Test> ascii_line(size_t N)
{
auto name = std::to_string(N) + " chars per line";
auto text = std::string(N, 'a') + std::string { "\n" };
return make_unique<Line>(name, text);
return std::make_unique<Line>(name, text);
}

unique_ptr<Test> sgr_line(size_t N)
std::unique_ptr<Test> sgr_line(size_t N)
{
auto name = std::to_string(N) + " chars with sgr per line";
std::string text {};
text += std::string { "\033[38;2;20;200;200m" };
text += std::string(N, 'a');
text += std::string { "\n" };
text += std::string { "\033[38;2;255;255;255m" };
return make_unique<Line>(name, text);
return std::make_unique<Line>(name, text);
}

unique_ptr<Test> sgrbg_line(size_t N)
std::unique_ptr<Test> sgrbg_line(size_t N)
{
auto name = std::to_string(N) + " chars with sgr and bg per line";
std::string text {};
text += std::string { "\033[38;2;20;200;200m\033[48;2;100;100;100m" };
text += std::string(N, 'a');
text += std::string { "\033[38;2;255;255;255m\033[48;2;0;0;0m" };
text += std::string { "\n" };
return make_unique<Line>(name, text);
return std::make_unique<Line>(name, text);
}

} // namespace contour::termbench::tests
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
73 changes: 57 additions & 16 deletions scripts/Xvfb-bench-run.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,71 @@
#!/usr/bin/env bash

#
# Usage: Xvfb-contour-run.sh <contour-args>
# Usage: Xvfb-bench-run.sh <path-to-tb-executable>

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}"

OUTPUT_DIR="${PWD}"

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

function require_bin() {
local tool="${1}"
if ! which "${tool}" >/dev/null; then
echo 1>&2 "$0: Could not find the required tool ${tool}."
exit 1
fi
}

require_bin Xvfb
require_bin "${TB_BIN}"
require_bin "${CONTOUR_BIN}"
require_bin "${KITTY_BIN}"
require_bin "${XTERM_BIN}"
require_bin "${ALACRITTY_BIN}"

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

LIBGL_ALWAYS_SOFTWARE="${LIBGL_ALWAYS_SOFTWARE:-true}"
DISPLAY=:99
export LIBGL_ALWAYS_SOFTWARE
export DISPLAY
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}" --fixed-size --stdout-fastpath --column-by-column --output "${OUTPUT_DIR}/${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
}

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}
)
Loading

0 comments on commit 29f1461

Please sign in to comment.