Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSON as a format for output file #20

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ if(NOT WIN32 AND NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release MinSizeRel RelWithDebInfo)
endif()

include(FetchContent)
FetchContent_Declare(
glaze
GIT_REPOSITORY https://github.com/stephenberry/glaze.git
GIT_TAG main
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(glaze)

set(MASTER_PROJECT OFF)
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
set(MASTER_PROJECT ON)
Expand Down
1 change: 1 addition & 0 deletions libtermbench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set_target_properties(termbench PROPERTIES

target_include_directories(termbench PUBLIC $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(termbench PUBLIC glaze::glaze)

install(TARGETS termbench
EXPORT termbench-targets
Expand Down
21 changes: 7 additions & 14 deletions libtermbench/termbench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ namespace termbench
#define leadingZeroBits(x) __builtin_clz(v)
#endif

namespace
{
std::string sizeStr(double _value)
{
if ((long double) (_value) >= (1024ull * 1024ull * 1024ull)) // GB
return std::format("{:7.3f} GB", _value / 1024.0 / 1024.0 / 1024.0);
if (_value >= (1024 * 1024)) // MB
return std::format("{:7.3f} MB", _value / 1024.0 / 1024.0);
if (_value >= 1024) // KB
return std::format("{:7.3f} KB", _value / 1024.0);
return std::format("{:7.3f} bytes", _value);
}
} // namespace

using u16 = unsigned short;

Benchmark::Benchmark(std::function<void(char const*, size_t n)> _writer,
Expand Down Expand Up @@ -117,6 +103,13 @@ void Benchmark::runAll()
}
}
//

void Benchmark::summarizeToJson(std::ostream& os)
{
std::string buffer = glz::write_json(results_);
os << buffer;
}

void Benchmark::summarize(std::ostream& os)
{
os << std::format("All {} tests finished.\n", results_.size());
Expand Down
36 changes: 36 additions & 0 deletions libtermbench/termbench.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <string_view>
#include <vector>

#include <glaze/glaze.hpp>

namespace termbench
{

Expand Down Expand Up @@ -95,6 +97,7 @@ class Benchmark
void runAll();

void summarize(std::ostream& os);
void summarizeToJson(std::ostream& os);

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

Expand All @@ -114,8 +117,41 @@ class Benchmark
std::vector<Result> results_;
};

inline std::string sizeStr(double _value)
{
if ((long double) (_value) >= (1024ull * 1024ull * 1024ull)) // GB
return std::format("{:7.3f} GB", _value / 1024.0 / 1024.0 / 1024.0);
if (_value >= (1024 * 1024)) // MB
return std::format("{:7.3f} MB", _value / 1024.0 / 1024.0);
if (_value >= 1024) // KB
return std::format("{:7.3f} KB", _value / 1024.0);
return std::format("{:7.3f} bytes", _value);
}

} // namespace termbench

namespace glz
{

template <>
struct meta<termbench::Result>
{
using T = termbench::Result;
static constexpr auto value = glz::object(
"name",
[](T const& result) { return result.test.get().name; },
"bytes written",
&T::bytesWritten,
"time",
[](T const& result) { return result.time.count(); },
"MB/s",
[](T const& result) {
double bytesPerSecond = double(result.bytesWritten) / (double(result.time.count()) / 1000.0);
return bytesPerSecond / 1024.0 / 1024.0;
});
};
} // namespace glz

// Holds a set of pre-defined terminal benchmark tests.
namespace termbench::tests
{
Expand Down
48 changes: 28 additions & 20 deletions scripts/plot_results.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using Pkg
Pkg.add("CairoMakie")
Pkg.add("JSON")
using CairoMakie
using JSON



function parse_line(line)
if count("MB",line) > 0
Expand All @@ -10,41 +14,45 @@ function parse_line(line)
end
end

function get_data(data, data_type)
function get_data(file_name)
return JSON.parsefile(file_name)
end

function get_values(data, data_type)
if data_type == :ascii
name = "chars per line:"
name = "chars per line"
elseif data_type == :sgr
name = "chars with sgr per line:"
name = "chars with sgr per line"
elseif data_type == :sgr_bg
name = "chars with sgr and bg per line:"
name = "chars with sgr and bg per line"
elseif data_type == :unicode
name = "unicode simple:"
name = "unicode simple"
elseif data_type == :diacritic
name = "unicode diacritic:"
name = "unicode diacritic"
elseif data_type == :diacritic_double
name = "unicode double diacritic:"
name = "unicode double diacritic"
elseif data_type == :fire
name = "unicode fire:"
name = "unicode fire"
elseif data_type == :fire_text
name = "unicode fire as text:"
name = "unicode fire as text"
elseif data_type == :flag
name = "unicode flag:"
name = "unicode flag"
end

lines = []
lines = Vector{Float64}()
for el in data
if occursin(name, el)
push!(lines, el)
if occursin(name, el["name"])
push!(lines, el["MB/s"])
end
end
return [ parse_line(val) for val in lines]
return lines
end

function insert_from_data(ax, file_name, marker_style, type)
data = split(open(io->read(io, String), file_name), '\n')
data = get_data(file_name)
terminal_name = split(file_name,"_")[1]

data_speed = get_data(data,type)
data_speed = get_values(data,type)
scatter!(ax, data_speed, label= terminal_name * "_ascii", marker = marker_style, markersize = 8)
end

Expand All @@ -57,13 +65,12 @@ function generate_for_terminal(file_name, prefix="results_")
fig = Figure()
ax = Axis(fig[1,1], title = "Results for "*terminal_name ,xlabel = "Length of line", ylabel = "throughput, MB/s")

data = split(open(io->read(io, String), file_name), '\n')
terminal_name = split(file_name,"_")[1]
data = get_data(file_name)

markers = [:circle :rect :cross :star4 :start5 :star6 :diamond]

get_data_l = (type) -> get_data(data,type)
speed = [ get_data_l(t) for t in types]
get_values_l = (type) -> get_values(data,type)
speed = [ get_values_l(t) for t in types]

marker_size = 8
for (ind,dat) in enumerate(speed)
Expand Down Expand Up @@ -94,6 +101,7 @@ function generate_comparison(type)
end
end


types = [:ascii :sgr :sgr_bg ]
generate_for_terminal("contour_results")
generate_for_terminal("alacritty_results")
Expand Down
2 changes: 1 addition & 1 deletion tb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ int main(int argc, char const* argv[])
cout << "Writing summary into " << settings.fileout << std::endl;
std::ofstream writerToFile;
writerToFile.open(settings.fileout);
tb.summarize(writerToFile);
tb.summarizeToJson(writerToFile);
}

#if defined(_WIN32)
Expand Down
Loading