Skip to content

Commit

Permalink
termbench: Add crafted test case
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <[email protected]>
  • Loading branch information
christianparpart committed Mar 18, 2024
1 parent f2aead3 commit ea3a3b2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
38 changes: 38 additions & 0 deletions libtermbench/termbench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ void Benchmark::add(std::unique_ptr<Test> _test)
tests_.emplace_back(std::move(_test));
}

void Benchmark::updateWindowTitle(std::string_view _title)
{
auto const now = steady_clock::now();
if (now - lastWindowTitleUpdate_ < 100ms)
return;

lastWindowTitleUpdate_ = now;
std::cout << std::format("\033]2;{}\033\\", _title);
std::cout.flush();
}


void Benchmark::writeOutput(Buffer const& testBuffer)
{
auto const output = testBuffer.output();
Expand All @@ -82,8 +94,15 @@ void Benchmark::runAll()
test->setup(terminalSize_);

while (buffer->good())
{
test->fill(*buffer);

updateWindowTitle(std::format("{}: filling buffer {:.3}%",
test->name,
static_cast<double>(buffer->size())
/ static_cast<double>(1024 * 1024 * testSizeMB_)));
}

auto const beginTime = steady_clock::now();
writeOutput(*buffer);
buffer->clear();
Expand Down Expand Up @@ -199,6 +218,20 @@ namespace
writeChar(_sink, 'm');
}

class CraftedTest: public Test
{
public:
explicit CraftedTest(std::string name, std::string description, std::string text) noexcept:
Test(std::move(name), std::move(description)), _text { std::move(text) }
{
}

void fill(Buffer& _sink) noexcept override { _sink.write(_text); }

private:
std::string _text;
};

class ManyLines: public Test
{
public:
Expand Down Expand Up @@ -386,4 +419,9 @@ std::unique_ptr<Test> sgrbg_line(size_t N)
return std::make_unique<Line>(name, text);
}

std::unique_ptr<Test> crafted(std::string name, std::string description, std::string text)
{
return std::make_unique<CraftedTest>(std::move(name), std::move(description), std::move(text));
}

} // namespace termbench::tests
5 changes: 4 additions & 1 deletion libtermbench/termbench.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
#pragma once

#include <array>
#include <chrono>
#include <cstring>
#include <functional>
Expand Down Expand Up @@ -52,6 +51,7 @@ struct Buffer

void clear() noexcept { _data.clear(); }
bool empty() const noexcept { return _data.empty(); }
size_t size() const noexcept { return _data.size(); }

private:
std::string _data;
Expand Down Expand Up @@ -102,11 +102,13 @@ class Benchmark

private:
void writeOutput(Buffer const& testBuffer);
void updateWindowTitle(std::string_view _title);

std::function<void(char const*, size_t)> writer_;
std::function<void(Test const&)> beforeTest_;
size_t testSizeMB_;
TerminalSize terminalSize_;
std::chrono::steady_clock::time_point lastWindowTitleUpdate_;

std::vector<std::unique_ptr<Test>> tests_;
std::vector<Result> results_;
Expand All @@ -125,4 +127,5 @@ std::unique_ptr<Test> binary();
std::unique_ptr<Test> ascii_line(size_t);
std::unique_ptr<Test> sgr_line(size_t);
std::unique_ptr<Test> sgrbg_line(size_t);
std::unique_ptr<Test> crafted(std::string name, std::string description, std::string text);
} // namespace termbench::tests
46 changes: 43 additions & 3 deletions tb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <format>
#include <fstream>
#include <functional>
Expand Down Expand Up @@ -102,6 +103,7 @@ struct BenchSettings
size_t testSizeMB = 32;
bool nullSink = false;
bool stdoutFastPath = false;
std::vector<std::filesystem::path> craftedTests {};
bool columnByColumn = false;
std::string fileout {};
std::optional<int> earlyExitCode = std::nullopt;
Expand Down Expand Up @@ -144,7 +146,7 @@ BenchSettings parseArguments(int argc, char const* argv[], TerminalSize const& i
else if (argv[i] == "--help"sv || argv[i] == "-h"sv)
{
cout << std::format("{} [--null-sink] [--fixed-size] [--stdout-fastpath] [--column-by-column] "
"[--size MB] [--output FILE] [--help]\n",
"[--size MB] [--from-file FILE] [--output FILE] [--help]\n",
argv[0]);
return { .earlyExitCode = EXIT_SUCCESS };
}
Expand All @@ -153,6 +155,16 @@ BenchSettings parseArguments(int argc, char const* argv[], TerminalSize const& i
++i;
settings.fileout = argv[i];
}
else if (argv[i] == "--from-file"sv && i + 1 < argc)
{
++i;
if (!std::filesystem::exists(argv[i]))
{
cerr << std::format("Failed to open file '{}'.\n", argv[i]);
return { .earlyExitCode = EXIT_FAILURE };
}
settings.craftedTests.emplace_back(argv[i]);
}
else
{
cerr << std::format("Invalid argument usage.\n");
Expand All @@ -162,13 +174,38 @@ BenchSettings parseArguments(int argc, char const* argv[], TerminalSize const& i
return settings;
}

void addTestsToBenchmark(termbench::Benchmark& tb, BenchSettings const& settings)
std::string loadFileContents(std::filesystem::path const& path)
{
std::ifstream file { path };
if (!file)
return {};

std::string content;
std::size_t const fileSize = std::filesystem::file_size(path);
content.resize(fileSize);
file.read(content.data(), static_cast<std::streamsize>(fileSize));
return content;
}

bool addTestsToBenchmark(termbench::Benchmark& tb, BenchSettings const& settings)
{
tb.add(termbench::tests::many_lines());
tb.add(termbench::tests::long_lines());
tb.add(termbench::tests::sgr_fg_lines());
tb.add(termbench::tests::sgr_fgbg_lines());
tb.add(termbench::tests::binary());
// TODO: The above tests should also be configurable via command line (-mlfgb).

for (auto const& test: settings.craftedTests)
{
auto content = loadFileContents(test);
if (content.empty())
{
cerr << std::format("Failed to load file '{}'.\n", test.string());
return false;
}
tb.add(termbench::tests::crafted(test.filename(), "", std::move(content)));
}

if (settings.columnByColumn)
{
Expand All @@ -180,6 +217,8 @@ void addTestsToBenchmark(termbench::Benchmark& tb, BenchSettings const& settings
for (size_t i = 0; i < maxColumns; ++i)
tb.add(termbench::tests::sgrbg_line(i));
}

return true;
}

void changeTerminalSize(TerminalSize requestedTerminalSize)
Expand Down Expand Up @@ -233,7 +272,8 @@ int main(int argc, char const* argv[])
settings.testSizeMB, // MB per test
settings.requestedTerminalSize };

addTestsToBenchmark(tb, settings);
if (!addTestsToBenchmark(tb, settings))
return EXIT_FAILURE;

WithScopedTerminalSize {
initialTerminalSize,
Expand Down

0 comments on commit ea3a3b2

Please sign in to comment.